Liferay Development

Liferay Consulting

Liferay Training

Your Trusted Liferay Solutions Partner

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

Saturday, October 29, 2016

Apache Felix Introduction

Apache Felix is one of the implementation for OSGi framework. We can download implementation and it will provides the OSGi container to provide runtime environment for OSGi bundles. We can develop bundles and install, start in Apache Felix OSGi container. These bundles should follow the OSGi specification then only it can be deployed into OSGi container such as the implementation provided by Apache Felix.

Liferay 7 have used Apache Felix implementation to support modularity application development while developing Liferay 7 Applications.

Perquisite:

Need to install Java 1.7 or Java 1.8 in your machine. Java 1.8 is recommended.

Working with Apache Felix

Download Apache Felix
Directories Information:
Start Apache Felix
Install and Start OSGi bundles.

Download Apache Felix

Apache Felix implementation can be download from following location.



You can download binary distribution that is zip or tar.gz and it based on your operating system.

Once you downloaded the Apache Felix binary distribution then you can extract it in any of your local directory.

You can see the following directories once you extracted the zip or tar file.


Directories Information

bin

This directory contained actual OSGi implementation jar file (felix.jar).
Another directory called felix-cache this will provide cache for bundles and its default directory and we can change cache folder from properties file.


bundle

Bundle directory is auto deployment directory for OSGi container. Bundles which are deployed in this directly automatically started and running when OSGi container started.
Whenever the bundles are mandatory and that should needed for our environment then we can place these OSGi bundles in bundle directory so that it will be started and running automatically by OSGi container when it launched.

Apache Felix is providing the Command Line Interface (CLI) to manage or handle bundles and this implementation called Apache GoGo Shell. These bundles should start along with OSGi container that is why these bundles are placed in bundle directory.


conf

Conf directory contains configuration properties that should needed for OSGi container.
We have file called config.properties and it will contains all the required properties. We have list of properties that can be available to configure and all are listed in the document apache-felix-framework-configuration-properties and it’s available in doc directory.



doc
Doc directory contains all documents about Apache Felix and there are html pages you can directly open and read the documentations.


Start Apache Felix

Open command prompt and navigate Apache Felix home directory from there run the following command then Apache Felix will launch


java –jar bin/felix.jar


Once it started you can see Apache GoGo Shell command line interface from there we can manage bundles life-cycle.


Note:

Apache GoGo already available in default deployment directory that is why when you launch Apache Felix then Apache GoGo bundles also started then it will be available for use.

Once Apache Felix stated then you can see Apache GoGo shell command as follows



Type help command in the GoGo shell then we can see list of commands available.



Type lb or felix:lb command it list the available bundles in the OSGi container and shows its states(Active, Installed, DeActive)



Install and Start OSGi bundles.

Once we developed the OSGi bundle by following the framework specification then we can deploy these bundles into OSGi container.

We can start bundle and stop bundle using Apache GoGo shell command with commands.
First need to install bundle then start the bundle. We can install bundle from any location by proving bundle path.

The following is the way to install, start and stop bundles.

Install Bundle


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

OR

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


List Bundles


lb

OR

felix:lb


Start Bundle


start bundle-id-in-list

OR

felix:start bundle-id-in-list



Stop Bundle


stop bundle-id-in-list

OR

felix:stop bundle-id-in-list


The following is screen to shows the bundle life-cycle



Note:

OSGi bundle is packaged jar file.

Apache Felix used default port 8080 for http service and to change this port we have property in config.properties file.


org.osgi.service.http.port=8080


Reference:


Author


Popular Posts

Recent Posts

Recent Posts Widget