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.
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.
Related
Does some one already managed to setup a Vaadin (19 to 21) Web application using Spring Boot and Osgi (as bundle) ?
I've managed to have a Vaadin application running under Osgi container (Karaf).
I've managed to have a Spring Boot Application running under Osgi container (Karaf).
But not both at the same time.
In fact, when trying, no route are being registered and FixedVaadinServlet is never called.
Thanks for help.
I have to say that I don't exactly understand your question since Spring is not OSGi compatible (it just has no OSGi manifest) so I don't see how it may work at all.
Now I'm going to guess....
I've managed to have a Vaadin application running under Osgi container (Karaf).
So your application is built as an OSGi bundle and you have deployed it to Karaf (OSGI container) and it works.
I've managed to have a Spring Boot Application running under Osgi container (Karaf).
You have built something (either WAR or a Spring boot Jar) which is a Web application and you have deployed it to Karaf AS A WEB Application.
This feature provided by Karaf : it's not pure OSGi container. It allows to deploy WEB applications to it.
These Web Applications have no any relation to OSGi.
So you have no Spring application which works in OSGi: if you use another OSGi container (which doesn't support extra features like Web app deployment) then you won't be able to use Spring there since as I said Spring is not OSGi compatible.
So I don't see how it's possible to use Spring + OSGi at all.
We are using plugin architecture for one of our projects and we decided to use Spring pf4j for the same.
When we load the plugin via extensions - the application context is not able to find the beans created using #component in the plugin project- but the beans declared in #configuration classes are injected properly when we configure the plugin using register method - Is there anyway to scan and load the spring components in the plugin?
#Component in plugin is registered to main ApplicationContext via SpringExtensionFactory, make sure you set it up correctly in DefaultPluginManager correctly.
If you are going to use pf4j in SpringBoot, I would suggest you take a look sbp project. It is built upon pf4j and provides better integration with SpringBoot.
I have a Spring Boot+React application that is packaged as a bundled WAR.
This works well for most cases, but we need to be able to drop-in functionality in some cases that is not part of the bundle (such as via a JAR).
I knew OSGI exists for this case, but not sure of any usage with Spring Boot. Is there another way to do this as well?
If your use case is a kind of small plugin functionality for your spring boot app then you could start an OSGi framework inside spring boot and load bundles from a separate directory. You plugins could then offer their service via an interface that is provided by the spring boot app and an OSGi service with that interface.
You need some good OSGi knowledge for this to work.
My project is using MDHT(Model Driven Health Tools) Runtime Api Library which is based on Eclipse Modeling Framework(EMF). It works when servers in Spring MVC project. But for some reason the EMF package registry is not getting loaded properly and therefore it cannot deserialize as a Consol CCD document when try to servers in Sprint Boot project.
Code Sample:
Mu2consolPackage.eINSTANCE.unload();
ConsolPackage.eINSTANCE.eClass();
CDAUtil.load(in, result);
Link:
MDHT(https://github.com/mdht/mdht-models)
User Documentation (https://www.projects.openhealthtools.org/sf/wiki/do/viewPage/projects.mdht/wiki/User%20Documentation)
Edit:
After changing to use external tomcat to deploy instead of spring boot embedded servlet container. It works, but still cannot resolve using embedded container.
I have a Spring-enabled OSGi bundle. I'd like this bundle to export a factory-type OSGi service which client software can use to create multiple instances of the application context defined in this bundle.
By default the Spring DM library bundles will automatically scan and create an instance of an application context from any Spring XML configuration found under "META_INF/spring". To avoid this I moved the Spring XML configuration files under a different folder and then tried creating the application context programmatically on demand from the factory class. Unfortunately I ran into issues with Spring schema files not being available on the bundle classpath. I really don't want to embed the required Spring jars within my bundle just to get access to those schemas.
Is there a simpler way to clone Spring application contexts under an OSGi environment?
I do not understand your problem in detail but if you just want to load the application-context from a different location than META-INF/spring you can define this in the MANIFEST.MF file using 'Spring-Context', e.g. for files in the root folder
Spring-Context: /application-context-core.xml,/application-context-osgi.xml
See the documentation for further information.