OSGI classcast exception on felix

Posted by Nico on Stack Overflow See other posts from Stack Overflow or by Nico
Published on 2010-04-28T13:50:22Z Indexed on 2010/04/28 13:53 UTC
Read the original article Hit count: 401

Filed under:
|
|

Hi, i'm fairly new to osgi and am trying to get a functional proof of concept together.

The setup is that my common api is created in a bundle creatively named common-api.jar with no bundle activator, but it exports all it's interfaces. the one of interest in this situation is DatabaseService.java.

I then have a Second bundle called systemx-database-service. That implements the database service interface. this works fine as in the activator of the implementation bundle i test the connection to the database and select some arbitraty values. I also register the service i want to be available to the other bundle's like so:

   context.registerService(DatabaseService.class.getName(), new SystemDatabaseServiceImpl(context), new Properties());

The basic idea being when you look for a service reference for a Database service you'll get back the SystemDatabaseService implementation.

When i do a inspect service the output it this:

-> inspect s c 69
System Database Service (69) provides services:
----------------------------------------------
objectClass = za.co.xxx.xxx.common.api.DatabaseService
service.id = 39

which would lead me to believe that if i do this in a test bundle:

context.getService(context.getServiceReference(DatabaseService.class));

i should get back an instance of DatabaseService.class, but alas no such luck. it simply seems like it cannot find the service. stick with me here my story gets stranger.

figuring there is no where to go but up i wrote this monstrosity:

 for (Bundle bundle : bundles) {
        if (bundle.getSymbolicName().equals("za.co.xxx.xxx.database-service")) {
            ServiceReference[] registeredServices = bundle.getRegisteredServices();
            for (ServiceReference ref : registeredServices) {
                DatabaseService service = (DatabaseService) context.getService(ref);
               // use service here. 
               }
            }
        }
    }

now i can actually see the service reference, but i get this error

java.lang.ClassCastException: za.co.xxx.xxx.database.service.impl.SystemDatabaseServiceImpl cannot be cast to za.co.xxx.xx.common.api.DatabaseService

which is crazy since the implementation clearly implements the interface!

Any help would be appreciated. Please keep in mind i'm very new at the osgi way of thinking so my whole approach here might be flawed.

oh. if anyone wants the manifests i can post them. and i'm using the maven-bnd-plugin to build and executing on felix.

thanks

Nico

© Stack Overflow or respective owner

Related posts about java

Related posts about osgi