Starting with OSGI with Maven and Spring, and understanding OSGI dependency problems

In this article, we first cover the Open Services Gateway Initiative(OSGI). Then we setup an OSGI project with Maven. We make sure Maven adds the correct manifest when building a bundle and we automatize testing our bundles inside an OSGI environment. After that we look into understanding OSGI dependency problems.

OSGI

The Open Services Gateway Initiative (OSGI) makes it possible to break your application into multiple modules which can be independently deployed from each other, making it easier to manage cross-dependencies.
The different modules (coming in the form of bundles/jars for deployment) can be remotely installed, started, stopped, updated, and uninstalled without requiring a reboot.

OSGI Bundles

Each bundle is a tightly-coupled, dynamically loadable collection of classes, jars and configuration files that explicitly declares its external dependencies (if any).
Each bundle contains a detailed manifest MANIFEST.MF file, which includes different OSGI tags, and which is what physically makes an OSGI bundle different from a non-OSGI Java jar.

Hence, building an OSGI jar instead of a regular jar comes down to setting the values in this manifest file correctly.

The first 5 tags in the example are obvious.

The 3 last ones are the more interesting ones:Bundle-Activator: Indicates the class name to be invoked once a bundle is activated.Export-Package: Expresses what Java packages contained in a bundle will be made available to the outside world.Import-Package: Indicates what Java packages will be required from the outside world, in order to fulfill the dependencies needed in a bundle.

Activator

The Bundle-Activator BundleActivator class, declared in the manifest file, will be invoked once a bundle is activated. A typical OSGI jar, which contains services that should be registered with the OSGI container, contains such a Bundle-Activator declaration.
However, the declaration of a Bundle-Activator is optional. Some jars will only export certain packages and will not need a bundle activator/not register any services. The OSGI jar for the Spring framework, for example, does not require a Bundle Activator. All it does is exporting packages for other OSGI bundles to use.

For brevity, we do not include the AccountService class here. To compile this, you will also need the osgi-core lib on your classpath. If you want to experiment with the code, please download the Maven project made available in the last section of this article.

OSGI Lifecycle

The OSGI life cycle layer introduces dynamics that are normally not part of an application.

Every bundle within the OSGI container can be in one of the following states thanks to this mechanism:

Installed: The bundle has been successfully installed.Resolved: All Java classes that the bundle needs are available. This state indicates that the bundle is either ready to be started or has stopped.Starting: The bundle is being started, the BundleActivator.start method will be called, and this method has not yet returned. When the bundle has an activation policy, the bundle will remain in the Starting state until the bundle is activated according to its activation policy..Active: The bundle has been successfully activated and is running; its Bundle Activator start method has been called and returned.Stopping: The bundle is being stopped. The BundleActivator.stop method has been called but the stop method has not yet returned.Uninstalled: The bundle has been uninstalled. It cannot move into another state.

OSGI containers

There are 3 popular OSGI container implementations: Knopflerfish, Equinox, and Apache Felix. Concierge seems to be receiving a lot of adaptation on mobile and embedded devices too, due to its limited file footprint of about 80 kBytes.

Adding the correct manifest to your bundle with Maven

To make sure Maven adds the correct manifest to our bundle, we use the org.apache.felix maven-bundle-plugin. This plugin generates the correct manifest for us, based on the configuration we pass, configured through Maven properties.
We also use the maven-dependency-plugin to be able to embed libraries and we configure the maven-jar-plugin to use the generated manifest file.

Testing OSGI bundles with Spring

When building OSGI bundles, it is probably a good idea to check whether the built bundles are valid. We are going to check this by writing unit tests that extend from AbstractConfigurableBundleCreatorTests, from the spring-osgi-mock jar. This test will start an OSGI container and run the test in it. Tests like these can catch OSGI dependency problems automatically, requiring no manual deploy, allowing easy OSGI integration testing.

To start, add the following dependencies section to the Maven pom.xml:

These tests will run within the Felix OSGI container. The two first tests just write out some info about the OSGI environment itself. The last test fetches a reference to this service and then calls a method on it.

Troubleshooting OSGI dependency problems

When starting with OSGI, you need to get used to not having a global classpath, but instead, take into account which packages a bundle exports or imports.

You might run into problems because of this. In what follows, we cover some common import/export package mistakes. Different OSGI containers can have different implementations, so the error messages you see because of these mistakes might differ a bit. One OSGI container might fail to resolve a bundle because of a conflict, while another one might just load one of two conflicting bundles and report a ClassNotFoundException when a class residing in a discarded or unknown bundle is used. In certain containers, it is possible to get away with making one of the mistakes covered here, in certain circumstances. Still, they are errors against the specification and should always be avoided.

If you want to experiment with these errors to better understand them, please download this OSGI test project. It is in a correct starting state, but in what follows, we will indicate how to produce every covered error using this project.
Any of these errors will cause the unit test in the com.integratingstuff.osgi.test module to fail.

Failing to export a package needed by another bundle

This error can be produced by changing the project.build.export.package Maven property in the pom.xml of the com.integratingstuff.osgi.services1 module to “com.test.model” instead of “*”. Then com.test.service of the com.integratingstuff.osgi.services1 module will not be exported then(not declared in the Export-Package section of the manifest), and the AccountService class will not be available to other modules that need it, such as the com.integratingstuff.osgi.services2 module.

Failing to import a required package

This error can be produced by changing the project.build.import.package Maven property in the pom.xml of the com.integratingstuff.osgi.services2 module to “com.test.model” instead of “*”. Then com.test.service of the com.integratingstuff.osgi.services1 module will not be imported then(not declared in the Import-Package section of the manifest), and the AccountService class will be unknown to the com.integratingstuff.osgi.services2 module.

Exporting conflicting packages

This error can be produced by renaming the com.test.person.service package in the com.integratingstuff.osgi.services2 module to com.test.service, so it conflicts with the com.test.service package of the com.integratingstuff.osgi.services1 module.

Depending on the container, bundles importing the conflicting package will not resolve(“package uses conflict” errors) or only one of the two conflicting packages will be known by the container, resulting in ClassNotFoundExceptions.

In an OSGI environment, two bundles should not export a package with the same name.

Comments

5-25-14

Horia says:

Well, nice tips and tricks, but it would be even better if you would supply a working example.