[Solved] How to share a singleton between "Services.java" and other plugins ? [message #1790860] |
Tue, 19 June 2018 08:06  |
Stéphane Tzvetkov Messages: 2 Registered: June 2018 |
Junior Member |
|
|
Hi,
First, thank you for your great work with Sirius, this is a very usefull and interesting framework.
I'm still a beginner with this framework, so I may be using Sirius in a strange way, if so: please explain me how to do it right.
I'm using Sirius inside an Eclipse RCP application, so far everything was working great.
My Eclipse RCP applications (like all Eclipse apps) is based on the OSGi specification, wich is implemented by the Equinox framework. So in pratice I'm developping plugins that are interpreted as bundles from the OSGi point of view.
This is important because those plugins will all be related to the OSGi classloader at runtime.
On the other hand, Sirius provides a class "Services.java" that allow the execution of external (i.e. user-side) java code. For example: if I write a "run()" method in this class, I could call this method on an element of my Sirius diagram by writing "aql:self.run()" in the proper associated aql field.
My problem is: the methods inside the "Services.java" class do not depend on the OSGi classloader, but on the sun.misc classloader.
So, for example : I would like to share a singleton between the Services.java class and the rest of my Eclipse app, to share information across plugins. But because this singleton would be instanciate by two different classloaders (OSGi and sun.misc) there would be two different instances of my singleton: one in the OSGi classloader and the other one in the sun.misc classloader.
I already tried to write a classloader-safe singleton (surguy.net/articles/communication-across-classloaders.xml) (stackoverflow.com/questions/15156840/singleton-class-with-several-different-classloaders).
But without success...
My question is: is there a way to tell the "Services.java" class to use the OSGi classloader ? Or a way to tell OSGi to expose his bundles to sun.misc ? More generally: a way to avoid this classloaser issue ?
I don't know why the Services.java class is outside OSGi, maybe because it uses some reflection methods at some point ?
But I think this problem is also due to the OSGi classloader which may be not linked to the sun.misc classloader in any way. Therefore OSGi may forbid any bundles to be seen outside OSGi.
I found a way to expose some sun.misc related classes to OSGi (spring.io/blog/2009/01/19/exposing-the-boot-classpath-in-osgi/) (help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fruntime-options.html)
(in particular by adding those arguments to the VM arguments : -Dorg.osgi.compatibility.bootdelegation=true -Dorg.osgi.framework.bootdelegation=sun.misc.* -Dorg.osgi.framework.system.packages.extra=my.own.project.*)
But this is usless, I would like to achieve the opposite: in this case I would like to expose some OSGi related classes to sun.misc... This way Services.java could access other plugins.
I also tried to bypass the classloader issue by trying to get a bundle from the Services.java class: "Bundle bundle = FrameworkUtil.getBundle(MySingleton.class);" (in my "run()" method of Services.java I'm trying to call the "MySingleton.java" singleton) but it returns a null object;
I tried a workaround:
try {
Bundle bundle = InternalPlatform.getDefault().getBundleContext().getBundle();
System.out.println(">> " + bundle.getClass().getClassLoader().toString());
Class<?> clazz = bundle.loadClass(MySingleton.class.getName());
MySingleton instance = (MySingleton) clazz.newInstance();
instance.setAThing(42);
instance.runAThing();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
But I get a ClassNotFoundException at "bundle.loadClass(MySingleton.class.getName());"
As an other workaround, I could share information by reading/writing through temp files, but I'd rather prefer avoid this kind of solution...
Did someone faced this kind of problem before ? Any idea on how to resolve it ?
EDIT:
Marked as solved, see below. Thanks again Florian.
[Updated on: Fri, 22 June 2018 13:52] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01903 seconds