BUG Community

Welcome! Log In

Forums BUG SDK ClassCastException when tracking services

Subscribe to ClassCastException when tracking services  3 posts, 2 voices

Log in to reply to this topic
 
May 20, 2008 5:02am
Img_missing_medium alain.muse 9 posts

Hi all,

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 );

if (
( _ServiceReference != null ) &&
( _ServiceReference.length > 0 )
)
{
for ( int _Index = 0; _Index < _ServiceReference.length; _Index++ )
{
IDeviceHandler _DeviceHandler;
System.out.println("-> Activator.start()" + _ServiceReference[_Index]);

_DeviceHandler = (IDeviceHandler)_Context.getService(_ServiceReference[_Index]); <<<--- THE ERROR
System.out.println("--> Activator.start()" + _DeviceHandler);
_DeviceHandler.addDeviceHandlerListener(this);
}
}
}
catch (final Exception _Exception)
{
System.err.println("Activator.start:" + _Exception);
_Exception.printStackTrace();
}
}

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).

Any help would be appreciated,

Thanks a lot,

Alain
May 22, 2008 6:09am
Medium Bug Labs team jconnolly 285 posts

alain.muse
Hi all,

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

_DeviceHandler = (IDeviceHandler)_Context.getService(_ServiceReference[_Index]); <<<--- THE ERROR



Alain,

Could you give me the contents of the exception? The stack trace may be of some help too.

Thanks.
May 28, 2008 4:55am
Img_missing_medium 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.

Tricky isn't it ? ;)

Thanks for your help,

Alain
Log in to reply to this topic
Forums BUG SDK ClassCastException when tracking services

Powered by Community Engine