Showing posts with label Liferay OSGi Bundle. Show all posts
Showing posts with label Liferay OSGi Bundle. Show all posts

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

Monday, March 28, 2016

Liferay 7 Features

Liferay 7 is exciting release from Liferay Inc. and it’s definitely a promising product, will have many new functional features and many architectural improvements.

Liferay is really putting lot of efforts towards to Liferay 7 development. Liferay 7 Expedition is program there we can see all new features and updates about Liferay 7.

Liferay completed alpha and beta releases and they already released the Liferay 7 RC version. Couple of months they are going to release Liferay 7 GA release and no doubt it’s really a promising product for developers and customers as well.


The following are the Liferay 7 features

OSGi Support
Bootstrap 3.0 Look and Feel
Java 8 Support
Alloy Editor
Elastic Search
Support Integration Testing through Arquillian
Application Display Templates Support for Login, Language and Breadcrumb portlets.
Improvements in Web content management
Singe Page Application Development
Document Management storages extracted as modules      
Service Builder code now uses Declarative Services instead of Spring for dependency injection

OSGi Support

Liferay 7 has supported OSGi modular based environment. Open Services Gateway initiative is standards for modular based application development.

OSGi will provide ability to create application in several components and all components together work as application and each component is independent and much decoupled from other components of course sometimes there is dependency among components.

OSGi provide container and it will responsible of components lifecycle so that we won’t need to concentrated on underlying things about component such as components lifecycle and its dependency related things.

OSGi will get rid of you from class loader issues and as for OSGi several components together became as bundle and each bundle have its own class loader. All bundles class loaders will be managed by OSGi container so that we cannot get any class loader issued and especially you can get rid of from ClassNotFoundException and NoClassDefFoundError

OSGi is dynamic modular development architecture so that we can deploy and un-deploy bundles without down the application. Another advantage is same time we can run different versions for same application such way OSGi container developed.

Traditional web application have Application server and we deploy application into application servers when we move application to another environment such as from UAT to production we need to take care many configurations and other related things.

OSGi have different way of implementation to provide web application runtime support so that each thing we can consider as service. To run web application in OSGi we need to integrate or deploy HTTP service so that we can run web applications.

Liferay 7 has implemented these OSGi support so that we can develop Liferay application as bundles or components. Running these bundles or deploy and un-deploy definitely going to be very easy when compared with before architecture.

We don’t worry about portlet, hook, Ext, theme and layout and everything going to be bundle. These bundles we can activate, run, and deactivate and we don’t need to down the application.

Liferay 7 has introduced new way of Liferay application development that is bundles. We cannot differentiate development for portlet, hook, Ext, theme and layout. We can forget about Liferay Plugins SDK environment and we can start developing Liferay Applications as OSGi bundles.

Liferay is developing application development environment and its tool called BLADE Tools which will provide way to develop Liferay Applications as OSGi bundles.
BLADE tools offer you MAVEN and GRADLE based build to create and deploy Liferay Applications as OSGi bundles.

BLADE Tools for Liferay


Liferay also started developing Liferay IDE and its used BLADE Tools to create and deploy bundles. Now it’s in progress and for now we can directly use BLADE Tools Command Line Interface to create Liferay Applications as OSGi bundles.


Liferay 7 supports to use Service builder tool in OSGi bundles, it’s also provide way to call Liferay API inside OSGi bundles and its provide support to use JSP in OSGi bundlels.
Liferay 7 have used features from Apache Felix and Eclipse Equinox OSGi implementation. It provide OSGi console (Apache GOGO) and Command Line Interface to deploy, activate and de-activate bundles in OSGi container.


Bootstrap 3.0 Look and Feel

Liferay 6.2 have used Bootstrap 2.x and Now Liferay 7 have used Bootstrap 3.0 so that look and feel for portal and its applications are really going to be good. All Liferay AUI components have used Bootstrap 2.x before and using Bootstrap 3.0 in Liferay 6.2 is really challenging and once we use bootstrap 3.0 UI developer need to put lot effort to adjust the things in theme level or AUI components level.

Now with bootstrap 3.0 all Liferay AUI components already used it and it won’t be problem to create themes with bootstrap 3.0.


Java 8 Support

Liferay 8 completely computable with Java 8. Oracle have stopped updates for Java 7 and Java 6. So now Liferay 7 completely considered only Java 8 and they implemented Liferay 7 to run on Java 8.

Java 8 have many features so it will be valued added to Liferay Application development like Lambdas and Functional Interfaces, Default and Static Methods in interfaces ,Stream API .Date Time API Improvements for Time Zones and Nashorn, JavaScript Engine are really pretty cool features in Java 8. We cannot see java.lang.OutOfMemoryError: PermGen space in Liferay 7 any more.


Alloy Editor

Alloy Editor is one of project from Liferay and it has provided better way of edit and create web content in Liferay. Liferay previous versions have used CKEditor and Now Liferay 7 have used Alloy Editor.

Alloy Editor Build on top CKEditor and its provided better and good look feel option to edit or create content. This editor we use extensively for WCM, Blogs, Wikis and Message boards in Liferay and also we can use it for custom portlets.

Nice feature is that it provides inline content edit so that edit control will available for each elements in content like text, images, headers and links. So that we can directly click on elements can see the edit control.


Elastic Search

Liferay before it’s used the Lucene search engine to search content in Liferay and Liferay 7 integrated Elastic Search as default search.


Support Integration Testing through Arquillian

One of the challenging thing Liferay is testing of application and there is no specific way to test and run test cases in Liferay. Generally we use Junit for unit testing.
Liferay have come up with better testing support to test and execute Liferay applications and Liferay 7 have given flexibility to use integration testing for portlets and OSGi plugins using Arquillian integration framework. This really cool for test Liferay applications such as OSGi bundles and portlet.


Application Display Templates Support for Login, Language and Breadcrumb portlets.

ADT is great way of customize the look and feel of existed portlets in Liferay through Free Marker Templates. In Liferay 6.2 have given ADT support for few portlets such as Asset Publisher, Blogs, Wikis, Navigation and Category Navigation  and it added few more portlet in the list such as Login, Language and Breadcrumb portlets.


Improvements in Web content management

Liferay 7 brings some of cool improvements in Web content management system such as Alloy UI editor to create and edit web content and ability to create mail templates for multiple languages and its changed CKFile browser to Liferay own file browser in side Alloy Editor.

Singe Page Application (SPA) Development

Now trends is Single Page Application using MVC based java script libraries with awesome look and feel with bootstrap. Liferay 7 have bring support for SPA application using SennaJs. Now all portlet become as Single Page Application so that user can navigate to anywhere without reloading entire portal page.


Document Management storages extracted as modules

Previous Liferay version have different ways of documents storage systems such as Jackrabbit (JCR) , DB Store and Advanced Filesystem when we want switch among these stores we need to do configuration in portal properties files and to effect the changes we need to stop server and start it.

Now Liferay 7 have introduced these features as OSGi modules so that we can dynamically active or deactivate bundles so that we can easily switch among these storage systems.
Liferay 7 have introduced following types of document storage as bundles such as Advanced Filesystem, CMIS, DB, Filesystem, JCR and S3.

Service Builder code now uses Declarative Services instead of Spring for dependency injection

Declarative Services is one of run time service provided by OSGi container to specify dependency services to other components and it make available these services to consumer bundle/component.

These dependency services declared as XML configuration so that when we deploy bundle into OSGi container then its resolves the all the dependency services otherwise bundle won’t be activate and its responsibility taken care by Service Component Runtime, we can use either Equinox Declarative Service or Apache Felix SCR to provide feature in OSGi container. Liferay 7 have used default OSGi Declarative Service model than Spring dependency. Generally Declarative Service model service will be available or activates when it started using otherwise it won’t be start but Spring Decency, the spring container will prepare the object based on XML configuration it will inject all objects and make it available complex Object in the spring container even we use it or not.

Due to Declarative Service we can resolve all dependency services resolution before bundle activated so that there is no run time exceptions or errors with respect dependency services.

Challenges

Liferay 7 is really good exciting but migration from older Liferay version to Liferay 7 is really challenging task because changes made at articular level.

One good thing is that Liferay 7 still used the traditional application server to run Liferay core portal than completely switching to OSGi HTTP based service. This really sign that we can migrate older versions to Liferay 7.

Due to Liferay 7 still used the Traditional application server so that we have more possibility to deploy all Liferay applications which are developed for previous versions will work and run in Liferay 7

Of course Liferay 7 already focused on conversion process from old Liferay plugins to OSGi based bundles.

Developer really need to habituate to OSGi based bundles development for Liferay 7 so its take some time for developer to understand development process. Because several years developers habituated to Liferay Plugins SDK and different types Liferay application like hooks, portlet, theme, layout and Ext and next we need to look at in the context that everything is OSGi bundle.

Sources:



Author

Recent Posts

Recent Posts Widget

Popular Posts