Spring OSGi integration: ClassCastException when getting osgi service in springContext

158 Views Asked by At

New to OSGi. I'm trying to get a service from osgi in Spring. Git link for the code : https://github.com/shinevs/SpringIntegrationTest Getting ClassCastException.

java.lang.ClassCastException: com.bundle.Activator cannot be cast to com.myInterface.BundleInterface
    at com.osgi.OSGiLauncher.lambda$0(OSGiLauncher.java:39)
    at org.apache.felix.framework.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:915)
    at org.apache.felix.framework.EventDispatcher.fireEventImmediately(EventDispatcher.java:834)
    at org.apache.felix.framework.EventDispatcher.run(EventDispatcher.java:1147)
    at org.apache.felix.framework.EventDispatcher.access$000(EventDispatcher.java:54)
    at org.apache.felix.framework.EventDispatcher$1.run(EventDispatcher.java:102)
    at java.lang.Thread.run(Thread.java:748)

I'm trying to initialize an OSGi bundle jar from spring service. OSGi bundle register a service called Activator, once OSGi bundle initialized and Activator created, trying to access the Activator from Spring.

Note : OSGi bundle is another module, it is added part of this project for testing purpose. Spring App only need a OSGi bundle as a jar.

How to run : curl http://localhost:8080/osgi

Issues facing : Spring App while trying to access Activator, it is throwing ClassCastException

Is this due to Different class loaders in spring and osgi. I tried to implement fragment, but that also didn't work.

1

There are 1 best solutions below

0
wilx On

It seems to me you are trying to exfiltrate a type (com.myInterface.BundleInterface) from inside the OSGi environment to outside into the Spring environment. But the BundleInterface is loaded by the OSGi loader and it unknown to the Spring environment loader. If you have added the test bundle into Spring Boot environment than it will have different class loader than the OSGi environment instance and thus you get the cast failure.

I suggest you avoid trying to use the OSGi environment inside Spring Boot application. I see only pain and suffering in that direction.