Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Using EcorePlugin in another Plugin
Using EcorePlugin in another Plugin [message #1097963] Fri, 30 August 2013 12:00 Go to next message
jonas b is currently offline jonas bFriend
Messages: 23
Registered: July 2013
Junior Member
Hello!

In my plugin I use and run code from a standalone Java application which I put on theclasspath as a jar. The jar uses some classes from eclipse as well, especially from EMF.

Now in the standalone jar there is this line:
EcorePlugin.getPlatformResourceMap().put("project", URI.createFileURI(umlFile.getParentFile().getAbsolutePath() + File.separator));
which maps the String "project" to the parent of the umlFile, ie something like this:
file:/C:/Users/Honk/myProject/src/test/uml/models/complex/

When I run the jar from my plugin, I get this exception:
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.core.internal.resources.ResourceException: Resource '/project/AlarmSystem.uml' does not exist.
where "AlarmSystem.uml" is the umlFile from above. So "project" should really point to the parent of "AlarmSystem.uml" but somehow it doesn't.

How to handle this?
Re: Using EcorePlugin in another Plugin [message #1098469 is a reply to message #1097963] Sat, 31 August 2013 05:15 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Jonas,

Comments below.

On 30/08/2013 2:00 PM, jonas b wrote:
> Hello!
>
> In my plugin I use and run code from a standalone Java application
> which I put on theclasspath as a jar. The jar uses some classes from
> eclipse as well, especially from EMF.
>
> Now in the standalone jar there is this line:
> EcorePlugin.getPlatformResourceMap().put("project",
> URI.createFileURI(umlFile.getParentFile().getAbsolutePath() +
> File.separator));which maps the String "project" to the parent of the
> umlFile, ie something like this:
> file:/C:/Users/Honk/myProject/src/test/uml/models/complex/
So isn't "myProject" really your project?
> When I run the jar from my plugin,
What URI are you using to load your initial resource? Does this initial
resource reference other resources? I'd expect you'd register
"file:/C:/Users/Honk/myProject/" as your project location, that you'd
call it "myProject" and that you'd use
platform:/resource/src/test/uml/models/complex/AlarmSystem.uml to load
the resource. It's also probably a bad idea to put it in your src
folder because the path a runtime will be different than the path in the
project at development time, i.e, these things will be copied from your
source folder to the bin folder and will likely appear at
test/uml/models/complex/ in your jar (on your classpath).
> I get this exception:
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException:
> org.eclipse.core.internal.resources.ResourceException: Resource
> '/project/AlarmSystem.uml' does not exist.where "AlarmSystem.uml" is
> the umlFile from above. So "project" should really point to the parent
> of "AlarmSystem.uml" but somehow it doesn't.
>
> How to handle this?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Using EcorePlugin in another Plugin [message #1099937 is a reply to message #1097963] Mon, 02 September 2013 12:11 Go to previous messageGo to next message
jonas b is currently offline jonas bFriend
Messages: 23
Registered: July 2013
Junior Member
Quote:
> Now in the standalone jar there is this line:
> EcorePlugin.getPlatformResourceMap().put("project",
> URI.createFileURI(umlFile.getParentFile().getAbsolutePath() +
> File.separator));which maps the String "project" to the parent of the
> umlFile, ie something like this:
> file:/C:/Users/Honk/myProject/src/test/uml/models/complex/
So isn't "myProject" really your project?

"myProject" only contains test resources, it is not the actual plugin project I am writing. Sorry for the confusion.
Quote:
What URI are you using to load your initial resource? Does this initial resource reference other resources?


In the standalone jar they load a resource which is specified at runtime by the user. The resource contains at least 1 UML model. The standalone jar also provides a custom profile which must be applied to those models. (Did you mean this by referencing other resources?)

So before they load the uml file, they do the following:
EcorePlugin.getPlatformResourceMap().put("project", URI.createFileURI(file.getParentFile().getAbsolutePath() + File.separator));
so that "project" points to the parent directory of the uml file.

later, when they load the resource:
Resource resource = resourceSet.getResource(URI.createPlatformResourceURI("/project/" + file.getName(), true), true);

This is where the error occurs: Resource '/project/AlarmSystem.uml' does not exist.

So the standalone jar defines a new project called "project" and this becomes the parent of the resource to load. But when the same jar is executed from within my plugin, the mapping does not work anymore.
Quote:
I'd expect you'd register "file:/C:/Users/Honk/myProject/" as your project location, that you'd call it "myProject" and that you'd use platform:/resource/src/test/uml/models/complex/AlarmSystem.uml to load the resource.

I think this is exactly what the jar does atm, no?
Re: Using EcorePlugin in another Plugin [message #1100429 is a reply to message #1099937] Tue, 03 September 2013 05:32 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Jonas,

Comments below.

On 02/09/2013 2:11 PM, jonas b wrote:
> Quote:
>> > Now in the standalone jar there is this line:
>> > EcorePlugin.getPlatformResourceMap().put("project",
>> > URI.createFileURI(umlFile.getParentFile().getAbsolutePath() +
>> > File.separator));which maps the String "project" to the parent of the
>> > umlFile, ie something like this:
>> > file:/C:/Users/Honk/myProject/src/test/uml/models/complex/
>> So isn't "myProject" really your project?
>
> "myProject" only contains test resources, it is not the actual plugin
> project I am writing. Sorry for the confusion.
> Quote:
>> What URI are you using to load your initial resource? Does this
>> initial resource reference other resources?
>
>
> In the standalone jar they load a resource which is specified at
> runtime by the user.
So the resources are in a jar? I don't see that you're specifying the
jar's location. Where is the jar located? What's the path to these
resources in that jar?
> The resource contains at least 1 UML model. The standalone jar also
> provides a custom profile which must be applied to those models. (Did
> you mean this by referencing other resources?)
Is there more than only UML resource? Do they cross reference each
other? What do the URIs of these cross references look like?
>
> So before they load the uml file, they do the following:
> EcorePlugin.getPlatformResourceMap().put("project",
> URI.createFileURI(file.getParentFile().getAbsolutePath() +
> File.separator)); so that "project" points to the parent directory of
> the uml file.
How does that specify the jar?
>
> later, when they load the resource:
> Resource resource =
> resourceSet.getResource(URI.createPlatformResourceURI("/project/" +
> file.getName(), true), true);
> This is where the error occurs: Resource '/project/AlarmSystem.uml'
> does not exist.
Set a breakpoint and to see why it's not finding it (and to see how the
URI is normalized).
>
> So the standalone jar defines a new project called "project" and this
> becomes the parent of the resource to load.
I've seen nothing in the URIs you've show that indicate something is in
a jar. A file: URI won't access something within a jar; you'd need an
archive URI...
> But when the same jar is executed from within my plugin, the mapping
> does not work anymore.
How is the location of the jar determined by your logic? In particular
how do you determine the value used for "file" above?
> Quote:
>> I'd expect you'd register "file:/C:/Users/Honk/myProject/" as your
>> project location, that you'd call it "myProject" and that you'd use
>> platform:/resource/src/test/uml/models/complex/AlarmSystem.uml to
>> load the resource.
>
> I think this is exactly what the jar does atm, no?
Jars don't really do anything, they just sit there to contain things...


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Using EcorePlugin in another Plugin [message #1100581 is a reply to message #1100429] Tue, 03 September 2013 09:39 Go to previous messageGo to next message
jonas b is currently offline jonas bFriend
Messages: 23
Registered: July 2013
Junior Member
Ok, I try to explain it differently.

I am writing a plugin. My plugin calls code from a specific jar. When I call the code, after some time the jar gets to the point where it uses EcorePlugin. Here it is:
private Resource loadUMLFile(File file, URI umlTcgProfile) {
	EcorePlugin.getPlatformResourceMap().put("project", URI.createFileURI(file.getParentFile().getAbsolutePath() + File.separator));
	EcorePlugin.getPlatformResourceMap().put("tcg_profile",	umlTcgProfile);
	return this.resourceSet.getResource(URI.createPlatformResourceURI("/project/" + file.getName(), true), true);
}


I debugged it in 2 different settings.
Setting 1: I debug the jar standalone (ie no eclipse runtime)
Setting 2: I debug my plugin (ie eclipse runtime exists when the jar is called)

The 2 scenarios fork here in class org.eclipse.emf.ecore.resource.impl.PlatformResourceURIHandlerImpl:

public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException
  {
    String platformResourcePath = uri.toPlatformString(true);
    if (workspaceRoot != null) //HERE!!!
    {
      return WorkbenchHelper.createPlatformResourceInputStream(platformResourcePath, options);
    }
    else
    {
      URI resolvedLocation = EcorePlugin.resolvePlatformResourcePath(platformResourcePath);
      if (resolvedLocation != null)
      {
        return getURIConverter(options).createInputStream(resolvedLocation, options);
      }

      throw new IOException("The path '" + platformResourcePath + "' is unmapped");
    }
  }


For the jar in standalone execution there is no workspaceroot and in the else part the path is expanded (the path in EcorePlugin resource map where "project" is the key)

For the jar being called by my plugin, there exists a workspaceroot because it's running inside eclipse. But then the path is never expanded and that's why an exception is thrown.

[Updated on: Tue, 03 September 2013 10:31]

Report message to a moderator

Re: Using EcorePlugin in another Plugin [message #1100841 is a reply to message #1100581] Tue, 03 September 2013 17:22 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Jonas,

Comments below.

On 03/09/2013 11:39 AM, jonas b wrote:
> Ok, I try to explain it differently.
>
> I am writing a plugin. My plugin calls code from a specific jar. When
> I call the code, after some time the jar gets to the point where it
> uses EcorePlugin. Here it is:
> private Resource loadUMLFile(File file, URI umlTcgProfile) {
> EcorePlugin.getPlatformResourceMap().put("project",
> URI.createFileURI(file.getParentFile().getAbsolutePath() +
> File.separator));
You mention a jar, but here you have a file. Eclipse might well
unpackage the jar to give you a file, but when running stand alone, the
question remain, where is the jar?
> EcorePlugin.getPlatformResourceMap().put("tcg_profile",
> umlTcgProfile);
> return
> m_resourceSet.getResource(URI.createPlatformResourceURI(s_projectPath
> + file.getName(), true), true);
> }
>
> I debugged it in 2 different settings.
> Setting 1: I debug the jar standalone (ie no eclipse runtime)
> Setting 2: I debug my plugin (ie eclipse runtime exists when the jar
> is called)
>
> The 2 scenarios fork here in class
> org.eclipse.emf.ecore.resource.impl.PlatformResourceURIHandlerImpl:
>
> public InputStream createInputStream(URI uri, Map<?, ?> options)
> throws IOException
> {
> String platformResourcePath = uri.toPlatformString(true);
> if (workspaceRoot != null) //HERE!!!
> {
> return
> WorkbenchHelper.createPlatformResourceInputStream(platformResourcePath, options);
>
> }
> else
> {
> URI resolvedLocation =
> EcorePlugin.resolvePlatformResourcePath(platformResourcePath);
> if (resolvedLocation != null)
> {
> return
> getURIConverter(options).createInputStream(resolvedLocation, options);
> }
>
> throw new IOException("The path '" + platformResourcePath + "' is
> unmapped");
> }
> }
>
> For the jar in standalone execution there is no workspaceroot and in
> the else part the path is expanded (the path in EcorePlugin resource
> map where "project" is the key)
>
> For the jar being called by my plugin, there exists a workspaceroot
> because it's running inside eclipse. But then the path is never
> expanded and that's why an exception is thrown.
Sorry, but I've asked you where the jar is located. You keep mentioning
a jar. But you only show a mapping to a file/folder and I don't see an
archive URI being constructed so whatever you're doing, it will not
resolve to a file in a jar.

If you have something in a jar and that jar is on the classpath, you
should use something like Class.getResource to construct a URl that
points at something in the jar. From that URL, you can determine the
URI for the jar itself, and that's likely the URI you want to use for
your project mapping. Perhaps have a look at
org.eclipse.emf.common.util.DelegatingResourceLocator.getBaseURL() to
see how the EMF runtime determines a URL based on the classpath.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Using EcorePlugin in another Plugin [message #1101290 is a reply to message #1097963] Wed, 04 September 2013 09:03 Go to previous messageGo to next message
jonas b is currently offline jonas bFriend
Messages: 23
Registered: July 2013
Junior Member
Hi Ed,
Thank you for your patience. About the jar: the method loadUmlFile() which I posted above belongs to a class inside the jar. It is a different project which I packed as a jar and put into my plugin. That's why in EcorePlugin there's no mapping to a jar but only to files because all the code I posted is located in the jar so from the jar's point of view there is no other jar. The reason why the author of loadUmlFile() put the mapping in EcorePlugin.getPlatformResourceMap() is because in the models (eg: AlarmSystem.uml) there is this line:
<appliedProfile href="../tcg_profile/tcg.profile.uml#_7hxR4IShEeGyf7wQIxhLDw"/>

So to be able to use relative references they put the mappings "project" and "tcg_profile" in the resource map and mapped "tcg_profile" to a location above the uml model.

However, I think I found a satisfying workaround which supports both standalone execution and execution within a plugin:

private static class LoadUriHandler2 extends URIHandlerImpl.PlatformSchemeAware {
	private final URI tcgProfile;

	public LoadUriHandler2(URI tcgProfile) {
		this.tcgProfile = tcgProfile;
	}

	@Override
	public URI resolve(final URI originalUri) {
		URI uri2 = originalUri;
		if (originalUri.toString().contains("/tcg_profile/"))
			uri2 = tcgProfile;
		uri2 = uri2.appendFragment(originalUri.fragment());
		return super.resolve(uri2);
	}
}

private Resource loadUMLFile(File file, URI tcgURI) {
	final Map<Object, Object> loadOptions = this.resourceSet.getLoadOptions();
	loadOptions.put(XMLResource.OPTION_URI_HANDLER, new LoadUriHandler2(tcgURI));
	return this.resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
}


Now the model is found and loaded as well as the profile which also is applied correctly. Now I have new problems where I'm not sure yet if they still relate to EMF (I think not). But it's a similar situation where in standalone execution it works and in plugin context some resolution fails. I have to investigate. Thank you for your help!

[Updated on: Wed, 04 September 2013 09:09]

Report message to a moderator

Re: Using EcorePlugin in another Plugin [message #1101378 is a reply to message #1101290] Wed, 04 September 2013 11:34 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Jonas,

Comments below.

On 04/09/2013 11:03 AM, jonas b wrote:
> Hi Ed,
> Thank you for your patience. About the jar: the method loadUmlFile()
> which I posted above belongs to a class inside the jar. It is a
> different project which I packed as a jar and put into my plugin.
> That's why in EcorePlugin there's no mapping to a jar but only to
> files because all the code I posted is located in the jar so from the
> jar's point of view there is no other jar.
Keep in mind that Eclipse will unpack a jar when there are nested jars,
so what works when running in Eclipse might well not work when running
stand alone...
> The reason why the author of loadUmlFile() put the mapping in
> EcorePlugin.getPlatformResourceMap() is because in the models (eg:
> AlarmSystem.uml) there is this line:
> <appliedProfile
> href="../tcg_profile/tcg.profile.uml#_7hxR4IShEeGyf7wQIxhLDw"/>
Keep in mind that relative URIs are resolved against the absolute URI of
the resource containing this reference, so the mapping you provided for
just the folder, is likely not enough (or not quite correct because this
URI navigates to the parent folder so likely expects there to be a
project called tcg_profile...
>
> So to be able to use relative references they put the mappings
> "project" and "tcg_profile" in the resource map and mapped
> "tcg_profile" to a location above the uml model.
I see.
>
> However, I think I found a satisfying workaround which supports both
> standalone execution and execution within a plugin:
>
> private static final class LoadUriHandler2 extends
> URIHandlerImpl.PlatformSchemeAware {
> private final URI tcgProfile;
>
> public LoadUriHandler2(URI tcgProfile) {
> this.tcgProfile = tcgProfile;
> }
>
> @Override
> public URI resolve(final URI originalUri) {
> URI uri2 = originalUri;
> if (originalUri.toString().contains("/tcg_profile/"))
> uri2 = tcgProfile;
> uri2 = uri2.appendFragment(originalUri.fragment());
> return super.resolve(uri2);
> }
> }
>
> private final Resource loadUMLFile(File file, URI tcgURI) {
> final Map<Object, Object> loadOptions =
> m_resourceSet.getLoadOptions();
> loadOptions.put(XMLResource.OPTION_URI_HANDLER, new
> LoadUriHandler2(tcgURI));
> return
> m_resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()),
> true);
> }
>
> Now the model is found and loaded as well as the profile which also is
> applied correctly. Now I have new problems where I'm not sure yet if
> they still relate to EMF (I think not). But it's a similar situation
> where in standalone execution it works and in plugin context some
> resolution fails. I have to investigate. Thank you for your help!


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:how can get all business object from diagram
Next Topic:EKeys usage
Goto Forum:
  


Current Time: Thu Apr 25 18:56:25 GMT 2024

Powered by FUDForum. Page generated in 0.03889 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top