I am trying to create a web application that will be launched from the OSGi HTTP service , this application needs to use another OSGi service (db4o OSGi), for which I need a link to BundleContext. I tried using two different approaches to get the OSGi context in a web application:
- Save
BundleContext Activatorin a static field of the class that the web service can import and use. - Use
FrameworkUtil.getBundle(this.getClass()).getBundleContext()(as an thisinstance MainPage, a web application class).
I think the first option is completely wrong, but in any case I am having problems with class loaders in both cases. In the second, it calls LinkageError:
java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/felix/framework/ModuleImpl$ModuleClassLoader) previously initiated loading for a different type with name "com/db4o/ObjectContainer"
Also tried with Equinox, and I have a similar error:
java.lang.LinkageError: loader constraint violation: loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) previously initiated loading for a different type with name "com/db4o/ObjectContainer"
The code that throws the exception:
ServiceReference reference = context.getServiceReference(Db4oService.class.getName());
Db4oService service = (Db4oService)context.getService(reference);
database = service.openFile("foo.db");
An exception occurs in the last line, databaseclass is ObjectContainer, if I change the type of this variable to Object, an exception does not occur, but it is not useful as Object:)
Update . I tried to use other services instead of db4o, and they worked as expected. Maybe the db4o OSGi bundle does something weird when loading its own classes, or maybe I am not using it correctly. It also works if I use it from a non-web package.
source
share