How to make an OSGI bundle using maven bundle plugin

OSGI (Open systems Gateway Initiative) specification defines a complete and dynamic component model which can be remotely installed, started, updated, stopped and uninstalled without restarting JVM. I'm not going to discuss the specification details of OSGI. You can find a lot of information from www.osgi.org
The objective of this post is to create a simple OSGI bundle using Apache Felix maven bundle plugin.

Pre-requisites:
Apache Maven2

Step 1
First we need to create a simple java class and which must be included in a proper directory structure therefore we can use Maven2 to build the source.

Create the following directory structure in your file system.

Create the following interface.

package org.wso2;

public interface GreetingService {
public void sayGreeting(String s);
}

Create an Impl directory under wso2 and add the following class in there.

package org.wso2.Impl;

import org.wso2.GreetingService;

public class GreetingServiceImpl implements GreetingService {

public void sayGreeting(String s){
System.out.println("Welcome " +s);

}
}

Step 2

We have created an interface and its implementation. We have not done anything new for you so far.
Since we are going to create an OSGI bundle, we need to add a BundleActivator though it is not mandatory to have. Bundle Activator class is used to start and stop the bundle when it is installed on an OSGI framework such as Knopflerfish.
Lets create an implementation of BundleActivator at the same package where GreetingServiceImpl is placed.

package org.wso2.Impl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.wso2.Impl.GreetingServiceImpl;

public class Activator implements BundleActivator {
public void start(BundleContext bundleContext) throws Exception {
GreetingServiceImpl greetingServiceImpl = new GreetingServiceImpl();
greetingServiceImpl.sayGreeting("Charitha");

}

public void stop(BundleContext bundleContext) throws Exception {

}
}

Step3
Now we can create the pom.xml to compile above classes and make an OSGI bundle (Basically we need to generate manifest headers in MANIFEST.MF of the generated bundle jar)
Here is the complete pom.xml. Note the highlighted section.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>


<groupId>org</groupId>
<artifactId>wso2</artifactId>
<packaging>bundle</packaging>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<name>Simple OSGI example</name>
<description>A bundle to demonstrate maven bundle plugin.</description>


<properties>
<bundle.namespace>${pom.groupId}.${pom.artifactId}</bundle.namespace>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Private-Package>${bundle.namespace},${bundle.namespace}.Impl</Private-Package>
<Export-Package>${bundle.namespace}</Export-Package>
<Bundle-Activator>${bundle.namespace}.Impl.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

The <Export-Package> element tells the plugin which of the available packages to copy into the bundle and export. The <Private-Package> instruction indicates which of the available packages to copy into the bundle but not export. In other words, private packages are not exported by the bundle.
Here, we define our impl package, org.wso2.Impl as a private package.
These elements will be added as manifest headers in resulting bundle jar as we will see shortly.

Step 4
Save pom.xml at the root directory of the above folder tree. (e.g:- example directory) and run it with mvn clean install

Bundle will be created at the target directory. Open it and look at META-INF/MANIFEST.MF

Manifest-Version: 1.0

Built-By: Charitha

Created-By: Apache Maven Bundle Plugin

Bundle-Activator: org.wso2.Impl.Activator

Import-Package: org.osgi.framework;version="1.3",org.wso2

Bnd-LastModified: 1223869521406

Export-Package: org.wso2

Bundle-Version: 1.0

Bundle-Name: Simple OSGI example

Bundle-Description: A bundle to demonstrate maven bundle plugin.

Build-Jdk: 1.5.0_14

Private-Package: org.wso2.Impl

Bundle-ManifestVersion: 2

Bundle-SymbolicName: org.wso2

Tool: Bnd-0.0.238


Have a look at the Export-Package and Private-Package headers which were added to the manifest as we defined them in pom.xml.
Now our bundle is ready to deploy in an OSGI framework. We will see how this bundle can be deployed on Knopflerfish in a future blog post.

Comments

Unknown said…
Thanks Charitha. This is very useful. I am planning to use Knopflerfish and Maven2.... can the maven bundle plugin be easily modified for Knopflerfish? Are you planning to write your post on this soon?
Cheers,
David.
Anonymous said…
Thank you so much!!polo shirt men'ssweate,Burberry Polo Shirts lacoste sweater, ralph lauren Columbia Jackets,ski clothing. Free Shipping, PayPal Payment. Enjoy your shopping experience on mensclothingus.com.You can find the father who desire fashionable, intellectual mens clothing simultaneously.
http://community.fox2now.com/venusjj
http://cheappolos.blog.drecom.jp
http://d.hatena.ne.jp/crystal666
http://www2.atword.jp/pumaspeed
http://www.seriousblogging.com/basketspuma
Anonymous said…
Perfect!!You are a outstanding person!Have you ever wore chaussures puma,Here are the most popular puma CAT,Puma shoes store gives some preview of puma speed cat,and casual but no sweat puma basket.
http://blog.livedoor.jp/lljj332
http://shoes-puma.jugem.jp
http://poloshirts--myfashion.blogspot.com
http://blades.blogsome.com
http://gillettefusion.edublogs.org

Popular posts from this blog

Common mistakes to avoid in WSO2 ESB - 1 - "org.apache.axis2.AxisFault: The system cannot infer the transport information from the URL"

Working with HTTP multipart requests in soapUI

WS-Reliable messaging with WSO2 WSAS -1