Question

Objective

I'm currently adapting an existing Java application, by transforming it into a plugin-based system. This is a Spring MVC-based web service which providers various services. I intend to make these services pluggable, provided by OSGi bundles. I already have all OSGi bundles providing those services, created with the maven-bundle-plugin.

What I have done

I decided to embed an OSGI framework (Apache Felix) into my application using the OSGi R4 API, by adding it as a Maven dependency:

<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.framework</artifactId>
    <version>4.2.1</version>
</dependency>

initializing it:

FrameworkFactory frameworkFactory = ServiceLoader
    .load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
config.put(FelixConstants.LOG_LEVEL_PROP, Integer.toString(Logger.LOG_DEBUG));
this.framework = frameworkFactory.newFramework(config);
this.framework.init();
this.context = framework.getBundleContext(); // keep ref to context

installing all bundles through the context:

context.installBundle(jarPath)

and starting the framework:

framework.start();

I then check the state of all bundles and all except the framework have a INSTALLED state. The framework bundle is ACTIVE.

Obviously the installed bundles were not resolved.

Question

When is Felix suppose to attempt to resolve bundles? From what I understood from the OSGi life-cycle, resolution would happen automatically. But I can't tell if Felix is even attempting to resolve the bundles. It does not print any log, even though I configured log level to DEBUG. It would be easy to debug my set up if I could see why a certain bundle is not able to resolve (i.e. can Felix tell me which packages are missing?).

Was it helpful?

Solution

Felix's behaviour is correct, because you have not started any of the bundles. If you do not start them, then there is no need to resolve them, and therefore they stay in the INSTALLED state.

In order for your application to actually do anything, you will need to start the bundles. You should do this after you have installed all of the bundles... that is, you should install all bundles before you start any bundles. The easiest way to do this is to accumulate the Bundle objects returned by installBundle() into a list, and then do a second loop over this list calling Bundle.start().

OTHER TIPS

First thing that comes to mind is the start level. I can't tell from what I see here what the start level of your bundles is, but that might be something for you to look into.

As to your question, the OSGI spec (version 4.3, that's what I have handy right now) describes this in section 4.4.2 Bundle State. If your bundles haven't been started (either through start level or manually), they'll be in the installed state.

It might be helpful if you install the Felix gogo shell and inspect the bundle's start level and if all requirements are fulfilled. Hope that helps.

EDIT: Turns out I was wrong, removed this line: "I believe Felix will automatically attempt to resolve the bundles during installation time, but I have nothing to back this up right now."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top