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.orgThe objective of this post is to create a simple OSGI bundle using
Apache Felix maven bundle plugin.
Pre-requisites:Apache Maven2Step 1First 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 2We 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 {
}
}
Step3Now 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 4Save pom.xml at the root directory of the above folder tree. (e.g:- example directory) and run it with
mvn clean installBundle will be created at the target directory. Open it and look at META-INF/MANIFEST.MF
Manifest-Version: 1.0Built-By: CharithaCreated-By: Apache Maven Bundle PluginBundle-Activator: org.wso2.Impl.ActivatorImport-Package: org.osgi.framework;version="1.3",org.wso2Bnd-LastModified: 1223869521406Export-Package: org.wso2Bundle-Version: 1.0Bundle-Name: Simple OSGI exampleBundle-Description: A bundle to demonstrate maven bundle plugin.Build-Jdk: 1.5.0_14Private-Package: org.wso2.ImplBundle-ManifestVersion: 2Bundle-SymbolicName: org.wso2Tool: Bnd-0.0.238Have 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.