OSGI class class exception on felix

I am new to osgi and trying to get a functional proof of the concept together.

The setup is that my general api is created in a bundle, with the creative name common-api.jar without a package activator, but it exports all its interfaces. interest in this situation is DatabaseService.java.

Then I have a second kit called systemx-database-service. This implements the database service interface. this works fine, since in the activator of the implementation package I am testing a database connection and selecting some arbitration values. I also register the service that I want to receive in another bundle, for example:

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

The basic idea is that when you look for a service reference for a database service, you will return a SystemDatabaseService implementation.

When I perform the check, output it like this:

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

which would make me believe that if I do this in a test suite:

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

I need to return an instance of DatabaseService.class, but, alas, this was unlucky. it just seems like he can't find a service. stick with me here my story is getting weirder.

Believing there’s nowhere to go, but I wrote this monster:

 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 see the service link, 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 explicitly implements the interface!

Any help would be greatly appreciated. Please keep in mind that I am very new to osgi, so my whole approach here may be wrong.

about. if anyone wants a manifest, I can post them. and I use maven-bnd-plugin to build and run on felix.

thank

Niko

+3
1

DatabaseService, SystemDatabaseServiceImpl. , getServiceReference , null, . , , getServiceReference : , Java .

DatabaseService.class.getClassLoader() impl, , , . , OSGi MANIFEST.MF, , .

, DatabaseService , ? , impl ( Export-Package) Export-Package. Import-Package.

+2

Source: https://habr.com/ru/post/1743182/


All Articles