Tuesday, November 8, 2016

Java Package Manager (JPM)

Java package manager is managing libraries and application regardless platform. We can easily install application and libraries with help of JPM. Jpm4j is java implementation which use the power of java so that we can write once and deploy anywhere.

We can develop java libraries and application, publish in internet then notify to JPM4J so that we can deploy from anywhere in any platform with help of Jpm4j.

Install JPM4J in windows

We have two options to install JPM4J in windows

Using JPM4J JAR File
Using JPM4J Installer

Prerequisite:

Before install JPM4J we must install Java in our machine. If not please download JAVA from oracle then install in your machine. JDK 1.7 or JDK 1.8 is recommended.

Using JPM4J JAR File

You can use following download link to download JPM4J JAR file. Once you have downloaded then we can use java jar command to start JPM4J application.


You can Place downloaded JAR file in your desired location


Open command prompt and navigate to the directory where you have placed the JAR file.

Use following command JAVA JAR command.


java -jar biz.aQute.jpm.run.jar [options] init

We can pass following parameters

-u/--user: Install in the user's home directory (e.g. ~\.jpm\windows).

-g/--global: Install in c:\JPM4J, where c: is the system drive.


We can use –g so that it will be installed in C:\JPM4J directory.
Use following installation jar command in command prompt


java -jar biz.aQute.jpm.run.jar -g init




Once we run the above command then it will be created JPM4J directory and it consisted few sub directories.


Bin directory having all the commands which we can use to install java libraries and applications.

Now navigate to bin directory using command prompt then we can use all jpm4j commands.
Use following help command to see all available commands in JPM4J


jpm help





jpm install

The above command will be used to install other artifacts like java libraries and applications.

Example:

jpm install bnd

The above command will install bnd tools in our machine.


Liferay 7 need JPM4J to install BLADE CLI to develop Liferay 7 modules and applications. We will going through more details about BLADE CLI in the next articles.

The following command will install "BLADE CLI"


jpm install com.liferay.blade.cli


Setting PATH Environment variable for JPM4J

In the above case to run JPM4J commands we need to navigate JPM4J bin directory then have to use available commands.

To use these commands from anywhere from commands prompt then we have to set PATH environment variable for JPM4J bin directory.

Go to system properties


Click on Advanced System Setting then it will open window pop-up where we need to click on Environment Variables



Now select path variable and click on edit then add following JPM4J bin path at end of the string. Finally click on OK then JPM4J path will set as environment variable.


;C:\JPM4J\bin;



Once we completed the above steps, now we can run commands from anywhere and we don’t need to navigate to bin directory from command prompt.



Using JPM4J Installer

JPM4J is available as windows installer so that we can simply download and install .exe file as we generally doing in windows.

The following is Install Download link


Once you download installer just double click on file then follows steps to complete the installation. Once installation successful then it will create JPM4J directory. We need to set PATH environment variable for JPM4J then we can use all available commands from command prompt.

Reference Links

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