Showing posts with label Bundle Install. Show all posts
Showing posts with label Bundle Install. Show all posts

Tuesday, November 8, 2016

Apache Felix Gogo Shell Introduction

Apache Felix GoGo Shell provides the command line interface to manage bundle in the OSGi container. We can use Gogo shell to interact with OSGi bundles and we can manage its lifecycle like install bundles, start bundles, stop bundles and uninstall bundles. Apache Felix implementing the OSGi RFC 147, which describes a standard shell for OSGi-based environments.

More information


Felix Gogo Shell implemented as three bundles and the following are three bundles which should be available in OSGi container then only we can use Apache Felix Gogo shell.


runtime - implements the core command processing functionality.

shell - provides a simple textual user interface to interact with the command processor.

command - implements a set of basic commands.



When we start Apache Felix OSGi container Gogo shell automatically started and available for use. These three bundles are alredy deployed in auto deploy directory of Apache Felix, so that as soon as Apache Felix started then Gogo shell will be available.

The following three bundles, which are available in Apache Felix “bundles” directory and its auto deployment directory for Apache Felix OSGi container.




Once we started Apache Felix then Gogo shell, it will be available as command line interface where we can use defined commands to interact with OSGi container to manage bundles.

Once you started Apache Felix then you can see “Welcome to Apache Felix Gogo” message with g! , this is Gogo shell.

We can list available bundle in the OSGi container by using lb command.

The following are few important GoGo Shell commands which we can use regularly.



felix:headers : To see available Bundle headers in OSGi container

OR

headers : To see available Bundle headers in OSGi container



The following screen shows available bundles headers in the OSGi container



felix:help : To get help related to available commands. We can see all available commands in the shell.

OR

help : To get help related to available commands. We can see all available commands in the shell.



The following screen shows the help command




felix:install : Install OSGi bundles

OR

install : Install OSGi bundles

Note:

We need to specify the bundle path to install bundle. Once bundle is installed then it will show the bundle ID.



The following screen shows the install a bundle



felix:start : This command will use to start installed bundle in the OSGi container. it means activate bundle in the OSGi container

OR

start : This command will use to start installed bundle in the OSGi container. it means activate bundle in the OSGi container.

Note:
This start command will expect the bundle ID to start the bundle. When we list the bundles, we can see Bundle IDs or when we installed the bundle then it will return the current installed bundle ID.


The following screen shows the start bundle



felix:stop : This command will stop bundle or deactivate bundle from OSGi container

OR

stop : This command will stop bundle or deactivate bundle from OSGi container.

Note:

This stop command will expect the bundle ID to stop the bundle. When we list the bundle then we can see Bundle IDs. When bundle is deactivated then it goes to resolved state it means bundle is installed but not in use.


The following screen shows stop bundle




felix:uninstall : This command uninstall the bundle from container

OR

uninstall : This command uninstall the bundle from container

Note:

We need to specify the bundle path to uninstall bundle. Once bundle is uninstalled then it will disappear from the bundles list, it means it’s not available in the OSGi container.


The following screen show uninstall bundle


Apart from above commands we can find many commands in the Gogo shell and all we can see by using help command.

Liferay 7 already deployed Apache Felix Gogo Shell bundles in the OSGi container and it will start when we start Liferay 7 portal. We can see complete details about Apache Felix Gogo Shell usage in the Liferay 7 portal in the future articles.
Author

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

Recent Posts

Recent Posts Widget

Popular Posts