Liferay Development

Liferay Consulting

Liferay Training

Your Trusted Liferay Solutions Partner

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

Thursday, November 17, 2016

Open Services Gateway Initiative (OSGi) Terminology

OSGi

Open Services Gateway Initiative is an architecture for modular application development and it’s also knowns as Dynamic Module System for java.

Modularity

Modularity is the way of developing or implementing software as number of unique modules rather than entire software as single unit.

Modules are communicating each other through Application Programming Gateway interface and finally it will form unique system.

OSGi responsibility is to make communication among these modules and manage the lifecycle of modules.

Module

Module is an independent small piece of software and it will fulfill defined business requirements and it can be reuse. Module can work independently or it will associate with other module to prepare complex system. Module consist java files and required configuration files.  

Component

Component is same like module. We can call module and component interchangeably. When compare with scope of component is limited and it’s less than module. A module can be collection of components.

Bundle

A bundle is collection of one or more components. According to the OSGi a bundle is smallest unit of modularization. We can say a bundle is packaged JAR file which consist java classes and required configuration files.

Plugin

Plugin and bundle are similar and in eclipse terminology we call it as plugin.

OSGi Container

OSGi container provides the run time environment to bundles its means it manage the lifecycle of bundles. It will responsible to activate and deactivate bundles and make communication among bundles.

MANIFEST

MANIFEST is configuration file for bundles and OSGi container will read bundle information form this file. It’s same like Deployment descriptor in web applications. It will provides the bundle general information and its dependency with other bundles or components.

OSGi Runtime

OSGi Runtime allows us to register services implementation and consume the existing services.

Service Registry

OSGi Runtime achieved through Service Registry. It maintain the information of services implementation and consume the existing services which are deployed in OSGi container.
OSGi services

OSGi Declarative Service

OSGI Declarative Services is a framework that provide the way to define and consume service by configuring details in the xml file without any dependency in source code to OSGi framework.

Service Component Runtime (SCR)

Service Component Runtime (SCR) is implementation of OSGi Declarative Service from Apache Felix.

OSGi Service Tracker

OSGi Service Tracker responsible to track or finding the services which are registered through service registry in the OSGi runtime.

Bundle Activator

Bundle activator is java class that implements the lifecycle of bundle.

Blueprint

Blueprint is framework form apache which provides the dependency injection nature for OSGi. Its design to deal with dynamic nature of OSGi. It’s similar to Declarative Services.

BnD Tools

BnD provide set of tools to develop OSGi bundles. It’s very easy way to develop OSGi bundles. It will manage bundle dependency and other configurations itself by tool rather developer.

enRoute


The OSGi enRoute project provides a programming model of OSGi applications. The OSGi specifications provide a powerful and solid platform for component oriented programming but by their nature lack ease of use, especially for newcomers to get started.

Author

Wednesday, November 16, 2016

Access Felix Gogo Shell in Liferay 7 from PuTTY Terminal

Felix Gogo shell is command line interface to manage bundles in the OSGi container. We have defined commands are available for Felix Gogo shell, form these we can interact with OSGi container and manage the bundles which are deployed in the OSGi container.

We can manage bundles lifecycle like install, start, stop and uninstall bundles from OSGi container.


Liferay 7 also have used Apache Felix Gogo shell to interact with Liferay Portal Module framework. We can deploy Liferay Application bundles and activate and deactivate bundles.
We have following ways to access Felix Gogo Shell

Access from Telnet client
Access from PuTTY Terminal

Access from Telnet client

The following is Article which describe Access Felix Gogo shell from Telnet client


Access from PuTTY Terminal

PuTTY:

PuTTY is open source terminal which support different protocols like SSH and Telnet.
The following are the steps to Access Felix Gogo shell from PuTTY in Liferay 7.

Download PuTTY
Start Liferay Portal
Access Felix Gogo Shell using PuTTY Terminal

Download PuTTY Software

The following is the place where you can download PuTTY software.


For windows download putty.exe file and place it in your desired location in the system.

Start Liferay 7 Portal

Go to Liferay 7 Portal Tomcat bin directory and start the server and it will take several minutes to start the portal.

The following is the article which describe the Install and Starting the Liferay 7 Portal


Access Felix Gogo Shell using PuTTY Terminal

Now go to the location where you place the putty.exe


Double click on the putty file then it will open run alert then click on run.


Once click on Run the it will open Window there we need to provide the following information of Felix Gogo shell


Host Name : 127.0.0.1
Port : 11311
Connection Type: Telnet




Note:

As we know that Felix Gogo shell running on the port 11311 and localhost that is 127.0.0.1.

Once click on open button then it will open Felix Gogo shell session



Now we can use all Gogo Shell commands to communicate with OSGi container and manage bundles.

List the bundles using lb command


Use help command to see all available Gogo Shell commands



Author

Popular Posts

Recent Posts

Recent Posts Widget