I have defined a bundle A which contains all the common classes defining the model used by other bundles. This bundle contains an interface IDeviceHandler. This interface is implemented by services contained in other bundles. Each service registers as a IDeviceHandler.
The bundle that represents "my application" has the following code public void start(final BundleContext _Context) throws Exception { System.out.println("Starting \"Device Manager\" ..."); try { final ServiceReference[] _ServiceReference = ACl_Context.getServiceReferences( IDeviceHandler.class.getName(), null );
The main purpose is to find all the registered IDeviceHandler to register as a listener. But a ClassCastException has been triggered.
I suspect a problem of ClassLoader. I also tried to embedd all the classes of my model (bundle A) into the other bundles. Unfortunately, it does not work (anyway it was not an "OSGi" minded solution).
Could you give me the contents of the exception? The stack trace may be of some help too.
Thanks.
May 28, 2008 4:55am
alain.muse
9 posts
Hi John,
The problem is due to class loading in OSGi. If you use (let's say) 3 bundles that refer to the same bundle "ModelBundle", the OSGi framework will consider that each class loaded from "ModelBundle" by the 3 others is different.
To solve this, you have to add the attribute "Bundle-Classpath" in the manifest file:
By doing this, the framework will load only once ModelBundle and consider that the 3 bundles will use the same classes.