Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting an object out of Equinox/Eclipse
    primarykey
    data
    text
    <p>I am facing a problem that is a combination of "plain" Java, Equinox, and the communication between the two. I have read other related questions (<a href="https://stackoverflow.com/questions/904050/how-to-run-an-osgi-framework-within-usual-java-code">here</a>, <a href="https://stackoverflow.com/questions/11413235/how-to-get-a-bundle-object-returned-by-a-service-out-of-the-equinox-framework">here</a>, and <a href="https://stackoverflow.com/questions/4673406/programatically-start-osgi-equinox">there</a> as well as other Web sites, such as <a href="http://wiki.osgi.org/wiki/Boot_Delegation" rel="nofollow noreferrer">this one</a> and <a href="http://www.macrobug.com/blog/wp-content/uploads/2009/01/osgiwrapperjava.html" rel="nofollow noreferrer">that one</a>) but could not find a satisfying (or working!) solution.</p> <p>I have a set of plug-ins: P1, P2, and P3. P1 export some classes, which are used by P2. P2 additionally exports other classes and interfaces, which are used by P3. In particular, P2 defines and exports <strong>interface MyInterface</strong> and its implementation <strong>class MyInterfaceImpl implements MyInterface</strong>. P3 is an application and, thus, contains a <strong>class Launcher implements IApplication</strong> and defines the <strong>public Object start(IApplicationContext)</strong> method. Everything compiles fine. When I run the Launcher as an Eclipse application from Eclipse, it runs fine. The launcher uses <strong>MyInterface</strong> and <strong>MyInterfaceImpl</strong> fine.</p> <p>Now, I programmatically run my application using the following (simple) code, which seems to be a "okay" way of running an Equinox/Eclipse application, according to various posts:</p> <pre><code>void callApplication() { final String[] args = new String[] { "-application", "Launcher", ... }; EclipseStarter.run(args, null); } </code></pre> <p>Again, this piece of code works fine and I can "see" my application run and produce its expected results.</p> <p>Now, here comes my problem: I would like to obtain an instance of <strong>MyInterfaceImpl</strong> built by my application, running in Equinox, back inside the "outer" Java code. My naïve solution was to return this object in the <strong>public Object start(IApplicationContext)</strong> method and modify my calling code as follows:</p> <pre><code>MyInterface callApplication() { final String[] args = new String[] { "-application", "Launcher", ... }; return (MyInterface) EclipseStarter.run(args, null); } </code></pre> <p>But this naïve solution does not work. I receive a <strong>java.lang.ClassCastException: MyInterfaceImpl cannot be cast to MyInterface</strong>. Thinking of it, it makes sense because, at the end of <strong>callApplication()</strong>, I have two "versions" of the pairs {<strong>MyInterface</strong>, <strong>MyInterfaceImpl</strong>}: one from the "outer" Java code, loaded by the JVM class-loader, and another from Equinox, loaded by Equinox class-loader. I modified my code to print the class-loaders of the returned object and of <strong>MyInterface</strong>: </p> <pre><code>final Object o = EclipseStarter.run(args, null); System.out.println(o.getClass().getClassLoader()); System.out.println(MyInterface.class.getClassLoader()); </code></pre> <p>and, indeed, I obtained: </p> <pre><code>org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@7e66458c[P2:1.5.0(id=413)] sun.misc.Launcher$AppClassLoader@1efc3d2 </code></pre> <p>Note that the class-loader of the returned object is said to come from P2, as expected, because P2 is the plug-in that defines and exports <strong>MyInterface</strong> and <strong>MyInterfaceImpl</strong>. </p> <p>I tried different ways to get "compatible" pairs of {<strong>MyInterface</strong>, <strong>MyInterfaceImpl</strong>} but with no luck so far (I always get the same <strong>java.lang.ClassCastException: MyInterfaceImpl cannot be cast to MyInterface</strong>):</p> <ul> <li><p>I tried setting the properties -Dorg.osgi.framework.bootdelegation=* -Dorg.osgi.framework.system.packages.extra=MyInterface,MyInterfaceImpl. </p></li> <li><p>I also tried intercepting the <strong>DefaultClassLoader</strong> in the <strong>start()</strong> method. </p></li> </ul> <p>So, my question is: <em>Is there a way for a Java program, which uses some classes defined in a project, which is also a plug-in and which export these classes, to exchange instances of these classes with an instance of Eclipse started programmatically?</em> Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload