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.

1 Answer
1

The test bundle must resolve to the same import of the DatabaseService interface as the SystemDatabaseServiceImpl. If this does not occur, then getServiceReference documents that it will return null even if a service is found. By locating the bundle manually and attempting to locate the service and cast, you're showing why getServiceReference behaves in this way: if it returned arbitrary services, Java casts would fail.

I would recommend printing DatabaseService.class.getClassLoader() in both the impl bundle and test bundle to prove if they're the same bundle. If they're not, then you need to adjust your OSGi MANIFEST.MF metadata to ensure that they have a consistent view of the interface class.

For example, is the DatabaseService interface included in both the test and impl bundles? If yes, you need to move that interface to either the impl bundle (and Export-Package) or to a third interface bundle and Export-Package. Then, adjust the other bundles to Import-Package.

Getting "class A cannot be cast to class A" is a classic Java issue when multiple class loaders are involved and OSGi has a classloader per bundle.
–
SteveDApr 28 '10 at 14:15

spot on bkail. when i ran a sysout on the class loaders i got [ 69.0 = classloader for target service 74.0 = classloader for this class ]. the common-api is in a separate bundle. does that mean that i need to exclude it from the jars that i'm creating for the implementation and test bundles?
–
NicoApr 29 '10 at 12:44

thanks. excluded the common api from everything and BAM. works like a charm
–
NicoApr 29 '10 at 13:11