Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding OSGi framework: when does bundle resolution should be done?

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?).

like image 912
Boj Avatar asked Dec 19 '25 16:12

Boj


1 Answers

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().

like image 186
Neil Bartlett Avatar answered Dec 22 '25 04:12

Neil Bartlett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!