0
votes

I have two projects.

  1. An osgi bundle (eclipse plugin project).
  2. A simple web application (deployable in tomcat).

I have started the felix container from tomcat with no problem by following the link below

http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html#using-the-servlet-bridge

Now I am stuck how to call the classes of the bundle (already installed in the felix container) from a servlet. It is throwing class not found error as the bundle project is not in the classpath but in the following location /WEB-IN/bundles.(I have to use this location for the bundles). So how it could be achieved?

1

1 Answers

0
votes

The BundleContext of the framework bundle is put into the servlet context in ProvisionActivator:

servletContext.setAttribute(BundleContext.class.getName(), context);

This means that you can access the BundleContext via the servletContext (that is available in all of your ordinary servlets) like this:

BundleContext context = servletContext.getAttribute(BundleContext.class.getName());

You can access the classes inside the embedded OSGi container by:

  • Asking for the bundle that contains the class via the bundleContext
  • Getting the class with the bundle.loadClass("myClass") method. You will be able to use that class only if you cast to the interface available from the ordinary WAR or with reflection

This is the way to do it, however you should not use this method :). Design your solution in the way that in your bundles you register OSGi services that implement interface(s) coming from the WAR classpath. After that you should use the bundleContext object to retrieve the OSGi service objects.

Other good solution is if you implement the logic you need within bundles instead of classes coming from the WAR.