java - OSGI classcast exception on felix -
i'm new osgi , trying functional proof of concept together.
the setup common api created in bundle creatively named common-api.jar no bundle activator, exports it's interfaces. 1 of interest in situation databaseservice.java.
i have second bundle called systemx-database-service. implements database service interface. works fine in activator of implementation bundle test connection database , select arbitraty values. register service want available other bundle's so:
context.registerservice(databaseservice.class.getname(), new systemdatabaseserviceimpl(context), new properties());
the basic idea being when service reference database service you'll systemdatabaseservice implementation.
when inspect service output this:
-> inspect s c 69 system database service (69) provides services: ---------------------------------------------- objectclass = za.co.xxx.xxx.common.api.databaseservice service.id = 39
which lead me believe if in test bundle:
context.getservice(context.getservicereference(databaseservice.class));
i should instance of databaseservice.class, alas no such luck. seems cannot find service. stick me here story gets stranger.
figuring there no go wrote monstrosity:
(bundle bundle : bundles) { if (bundle.getsymbolicname().equals("za.co.xxx.xxx.database-service")) { servicereference[] registeredservices = bundle.getregisteredservices(); (servicereference ref : registeredservices) { databaseservice service = (databaseservice) context.getservice(ref); // use service here. } } } }
now can see service reference, error
java.lang.classcastexception: za.co.xxx.xxx.database.service.impl.systemdatabaseserviceimpl cannot cast za.co.xxx.xx.common.api.databaseservice
which crazy since implementation implements interface!
any appreciated. please keep in mind i'm new @ osgi way of thinking whole approach here might flawed.
oh. if wants manifests can post them. , i'm using maven-bnd-plugin build , executing on felix.
thanks
nico
the test bundle must resolve same import of databaseservice interface systemdatabaseserviceimpl. if not occur, getservicereference documents return null if service found. locating bundle manually , attempting locate service , cast, you're showing why getservicereference behaves in way: if returned arbitrary services, java casts fail.
i recommend printing databaseservice.class.getclassloader() in both impl bundle , test bundle prove if they're same bundle. if they're not, need adjust osgi manifest.mf metadata ensure have consistent view of interface class.
for example, databaseservice interface included in both test , impl bundles? if yes, need move interface either impl bundle (and export-package) or third interface bundle , export-package. then, adjust other bundles import-package.
Comments
Post a Comment