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\" ...");
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);
catch (final Exception _Exception)
System.err.println("Activator.start:" + _Exception);

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,