Monday, October 31, 2016

OSGi Hello World Bundle with Apache Felix

OSGi is framework that provides modularized application development environment. We can dynamically add or remove components. It provides OSGi container that will handles the lifecycle of each bundle or component.

This example uses the Apache Felix OSGi implementation to develop and run the bundle in Apache Felix OSGi container.

Note:

Bundle is a packaged JAR and it consists of java classes and required configuration files.

In OSGi terminology bundle is an independent component or piece of software component that developed by following OSGi specifications and it can be deployed into OSGi 
containers.

Required Steps

Launch Apache Felix
OSGi Bundle Development    
       
Launch Apache Felix

Liferay Savvy have given detailed information about Apache Felix. Please follow the below post to start Apache Felix.


OSGi Bundle Development

Developing OSGi bundle is very straight forward way as we are generally developing java applications.

OSGi bundle must needed two important artifacts.

Activator Java Class
Manifest File (MANIFEST.MF)

Activator Java Class

Activator Java class is simple java class and it should implements BundleActivator.java, BundleActivator is an interface consist of bundle lifecycle methods. Any of our custom activator class that mush implements the BundleActivator interface.

The BundleActivator class must have a public constructor that takes no parameters. The OSGi framework can create a BundleActivator object by calling Class.newInstance().
Whenever our bundle needs to notified when bundle is started or stopped then we should implements the following life-cycle methods.

Start Method:

Start method will be invoked when OSGi container start the bundle. The following is syntax of start method.


public void start(BundleContext context) throws Exception {
        System.out.println("Hello Liferay Savvy World");
    }


Stop Method:

This method will be invoked when bundle is stopped by OSGi container. The following method syntax.


public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye Liferay Savvy World ");
    }


The following is complete Activator Java Class


package com.liferaysavvy.osgi.helloworld;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {
    public void start(BundleContext context) throws Exception {
        System.out.println("Hello world");
    }
    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye World");
    }
}


Manifest File (MANIFEST.MF)

Manifest File is very important for any bundle and it consist all Meta Data that required by OSGi container. Manifesto file act as deployment descriptor for OSGi container. This file consist of set headers that defined by OSGi specifications and we need to provide header information in the manifest file.

The file name should be MANIFEST.MF when we deploy OSGi bundle then OSGi container looking for manifest file.

The following is sample manifesto files as per example


Manifest-Version: 1.0
Bundle-Name: Liferay Savvy Hello World
Bundle-Description: Liferay Savvy Hello World
Bundle-Vendor: Liferay Savvy
Bundle-Version: 1.0.0
Import-Package: org.osgi.framework
Bundle-Activator: com.liferaysavvy.osgi.helloworld.Activator


Note:

There are many headers defined for manifest file by OSGi specification. Please go through OSGi specification to know more information about headers.

Development and Start OSGi Bundle

Required Things

Editor (NotePad++)
Need to Install Java 1.8
Need to set CLASSPATH

Note:

The following development is legacy way of development and we have many tools to develop OSGi bundle.

Editor (NotePad++)

We required note pad editor to create and edit java class and manifest file.

Need to Install Java 1.8

Java should install in you machine before develop OSGi bundles.

Need to set CLASSPATH

We need to set CLASSPATH and there all required jar file should be placed. We must need felix.jar file in the class path and it will be used at the time of bundle compilation. If any other jar files required to our bundle then we need to place in the class path directory.

The following is command to set class path and its temporary. Before compile bundle you must set the class path


set CLASSPATH=classpath1;classpath2

Example:

set CLASSPATH=C:\MeerPrince\OSGi\OSGIWorkSpace\lib\*


Create OSGiWorkSpace Directory and create lib directory and place the felix.jar file
HelloWord directory which is our bundle directory.





Place felix.jar file in lib directory and it is available in Apache Felix bin directory.


HelloWorld is actual bundle directory and create bundle manifest file (MANIFESTO.MF) and Activator java class in the defined package.


Activator Java class in the package com.liferaysavvy.osgi.helloworld


Now we are ready with all required files

Compile Bundle

When we compile project then it will generate .class file for every java class so we need to specify the classes directory where all .class files will be generated after successful compilation.

Create classes directory in the workspace
 

 
 
 The following is command to compile bundle


javac -d path-to-classes-directory  *.java

Example:

javac -d C:\MeerPrince\OSGi\OSGIWorkSpace\classes com\liferaysavvy\osgi\helloworld\*.java



Open command prompt and hit the above command then java source file will be compiled and Activator.class file will be created inside C:\MeerPrince\OSGi\OSGIWorkSpace\classes directory with same source package structure

Create Bundle JAR File

In OSGi terminology bundle is package jar file consist of java classed and manifest file.
Use the following command to create bundle JAR file and it will be created in project directory that is HelloWorld directory.


jar cfm helloworlbundle.jar manifest.mf -C C:\MeerPrince\OSGi\OSGIWorkSpace\classes com\liferaysavvy\osgi\helloworld


Make sure you need to specify the manifest file and Activator.class file location


Bundle JAR file will be available in HelloWorld directory.

.
Install and Start bundle in OSGi

Now we have ready with bundle JAR file. We can use Apache GoGo Shell commands to install and start the bundle.

Install Bundle in OSGi Container

The following is command to install bundle.


felix:install file:/path/to/bundle/bundle.jar

Example:

felix:install file:/C:/MeerPrince/OSGi/OSGIWorkSpace/HelloWorld/helloworlbundle1.jar


Launch Apache Felix and enter above shell command then it will display bundle ID.
Start Bundle

Use following command to start bundle in OSGi container


start bundle-id

Example:

start 13



When we install bundle it will shows the bundle Id then we can start bundle with Id. Once bundle started then you can see output “Hello World” and when you uninstall bundle then you can see “Goodbye World” message.

When you list bundle with command “lb” then you can see Hello World bundle in the list



Download OSGi bundle

Download Hello World OSGi bundle from following location.



Author

Popular Posts

Recent Posts

Recent Posts Widget