Showing posts with label apache Felix. Show all posts
Showing posts with label apache Felix. Show all posts

Monday, August 21, 2017

OSGi Sample Bundle Development with Eclipse

OSGi is java modularity development framework. We can divide complex application into multiple modules and OSGi will responsible to communicate all modules and make it as single application.

OSGi is framework provides the run time environment so that all these module can communicate each other. In OSGi terminology we can call each unit as bundle. Bundle is independent component in OSGi and it can run in OSGi container. OSGi container is responsible to handle lifecycle of bundle.

Follow below articles for Basics of OSGi and Bundles





In this article we are going to see develop basic bundle development using eclipse and will run in OSGi run time environment.

Basic OSGi bundle development without eclipse and run in Apache Felix OSGi run time environment was explained in below article.


Prerequisite.

Install Java 8 and set JAVA_HOME environment variable.

Download latest version of Eclipse from below link


We are using eclipse-jee-oxygen for this example.

Any bundle in OSGi need activator java class and it is responsible for managing bundle lifecycle in the OSGi container. So each activator class should extends “BundleActivator”.
When we create bundle with eclipse it will create basic template with Activator class and if we observed the activator class, that extends “BundleActivator” class.

Once you download the eclipse extract it local machine then you can find “eclipse” directory. Navigate to “eclipse” then click on eclipse icon then eclipse will be started and initially it will ask workspace for your projects then select your workspace directory.



Create OSGi sample bundle in Eclipse.

Go to File à New à Project

It will open project selection Dialog then select “Plugin-in Project” click on next.



Once click on next then you can see another dialog there we need to provide your project name then click on next.

Project Name: com.liferaysavvy.example.LiferaySavvyWorld
Plugin-in Targeted Run with Select OSGi framework à Standard



Once click on next we can see other dialog there it will ask information which will be used in the bundle MANIFEST file. Information like name, version, vendor and execution environment.

Click on Generate Activator class check box then give your Activator class name.


Once click on Next it will ask you select OSGi template and click on Crete Plugin using template then select “Hello OSGi” so that it will generate bundle with activator class with lifecycle methods



 Finally click on finish button then bundle will create with basic activator class.
The following is basic OSGi bundle project view in eclipse



Activator

Activator is java class which handle the bundle lifecycle management in the OSGi container. It will extends the “BundleActivator” class. We have different lifecycle methods as follows

start(--) :  This method will take the argument BundleContext and container will invoke this method to start bundle.

stop(--): This method also take BundleContext as argument and OSGi container will invoke this method to stop bundle.


package com.liferaysavvy.example.liferaysavvyworld;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

     /*
      * (non-Javadoc)
      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
      */
     public void start(BundleContext context) throws Exception {
           System.out.println("Say Liferay Savvy World!!");
     }
    
     /*
      * (non-Javadoc)
      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
      */
     public void stop(BundleContext context) throws Exception {
           System.out.println("Goodbye Liferay Savvy World!!");
     }

}


MANIFEST.MF:

MANIFEST.MF is deployment descriptor for the bundle and have more information and it will be used by OSGi container. This is core resource of the bundle.


Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: LiferaySavvyWorld
Bundle-SymbolicName: com.liferaysavvy.example.LiferaySavvyWorld
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.liferaysavvy.example.liferaysavvyworld.Activator
Bundle-Vendor: LIFERAYSAVVY
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: org.osgi.framework;version="1.3.0"


Bundle-ManifestVersion:

 This header tells that bundle follows the OSGi specification and value 2 specified, it is following SGi specification Release 4; if it is value 1 its means it is following the compliant with Release 3 or earlier.

Bundle-Name

Its human readable bundle name

Bundle-SymbolicName:

Its important header for bundle and it is unique name used to refereeing it in other bundles when it communicate to other bundles.

Bundle-Version:

Its simple version number for the bundle. We can start and run version specific bundles in the OSGi container.

Bundle-Activator

Bundle-Activator headers tells the Activator class name and it will used to manage life cycle of bundle by OSGi container.

Bundle-Vendor

The Bundle-Vendor header is just human readable name which tell the vendor who implemented the bundle.

Bundle-RequiredExecutionEnvironment:

Which tell the Jave environment is required to execute the bundle.

Import-Package

This header is very important and it tells which packages are required to execute the bundle. When the bundle depends on the other bundle then we have specify the bundle symbolic name so that it will be handled by the OSGi container. Basically each bundle must import OSGi implantation bundle it’s like “org.osgi.framework;”

Executing a bundle

Select bundle and right click on bundle

Run As à Run Configuration





Select your bundle and Select “org.eclipse.osgi” for Target platform then Apply à Run



You can see Output in the console as follows. Bundle will be started by OSGi container then it will invoke activator start method start() then we can see message in the console.





If you want execute bundle in Apache Felix environment then select Apache Felix OSGi implementation for Targeted platform.
Just type Felix in the filter text input then you can see Felix implementation jars.



If you want execute bundle in OSGi equinox then select targeted platform equinox

We can also export bundle and execute in standalone OSGi containers. Just export bundle as JAR file from eclipse and run jar file in other OSGi containers
Here is example how to run bundle in Apache Felix Environment. Look at “Install and Start bundle in OSGi” Section.


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


Recent Posts

Recent Posts Widget

Popular Posts