How to use an OSGi service from a web application?

Posted by Jaime Soriano on Stack Overflow See other posts from Stack Overflow or by Jaime Soriano
Published on 2010-03-29T15:52:43Z Indexed on 2010/03/29 15:53 UTC
Read the original article Hit count: 496

Filed under:
|
|

I'm trying to develop a web application that is going to be launched from a HTTP OSGi service, this application needs to use other OSGi service (db4o OSGi), for what I need a reference to a BundleContext. I have tried two different approaches to get the OSGi context in the web application:

  1. Store the BundleContext of the Activator in an static field of a class that the web service can import and use.
  2. Use FrameworkUtil.getBundle(this.getClass()).getBundleContext() (being this an instance of MainPage, a class of the web application).

I think that first option is completely wrong, but anyway I'm having problems with the class loaders in both options. In the second one it raises a 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 provokes the exception is:

ServiceReference reference = context.getServiceReference(Db4oService.class.getName());
Db4oService service = (Db4oService)context.getService(reference);
database = service.openFile("foo.db");

The exception is raised in the last line, database class is ObjectContainer, if I change the type of this variable to Object exception is not raised, but It's not useful as an Object :)

© Stack Overflow or respective owner

Related posts about java

Related posts about osgi