Sunday, July 18, 2010

Build an osgi service bundle and a consumer bundle using Maven Pax plugin and Equinox

In order to start Maven should be setup in your system.
In my machine i'm using Maven 2.2.1 and the OS is Windows XP.

Steps:
1. Go the parent directory where you want to creat the project in command prompt.
>cd\
>D:
>cd D:\NewBeginning\osgi

2. Generate the project

>mvn org.ops4j:maven-pax-plugin:create-project -DgroupId=example -DartifactId=com.xyz -Dversion=1.0-SNAPSHOT

You will find a folder which has a name simillar to provided artifactId above in your parent directory.

3.Go to the project folder in command prompt

>cd com.xyz

4.Start Equinox Container

>mvn pax:provision -Dframework=equinox

There you will get the Equinox prompt. Type "ss" to get a reports of summary status of all installed bundles.
Then type "exit" to get out of Equinox container.

5. Create OSGI service bundle

>mvn pax:create-bundle -Dpackage=com.myExample -Dname=service -Dversion=1.0-SNAPSHOT

There get created a bundle inside the current folder as "com.myExample" and inside you will see
a generated clsses of a sample service.
In order to make our own service bundle we will modify the code as follows:

• ExampleService.java ->(rename) MyFirstService.java

package com.myExample;

/**
* Public API representing an example OSGi service
*/
public interface MyFirstService
{
// public methods go here...

String sayHello( String text );
}

• ExamplServiceImpl.java ->(rename)MyFirstServiceImpl.java

package com.myExample.internal;

import com.myExample.MyFirstService;

/**
* Internal implementation of our example OSGi service
*/
public final class MyFirstServiceImpl
implements MyFirstService
{
// implementation methods go here...

public String sayHello( String text )
{
return("hi"+ text);

}
}

ExampleActivator.java->(rename)ServicePublisher.javapackage com.myExample.internal;

package com.myExample.internal;

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

import com.myExample.MyFirstService;

/**
* Extension of the default OSGi bundle activator
*/
public final class ServicePublisher
implements BundleActivator
{
/**
* Called whenever the OSGi framework starts our bundle
*/
public void start( BundleContext bc )
throws Exception
{
System.out.println( "STARTING com.myExample" );

System.out.println( "REGISTER com.myExample.MyFirstService" );

// Register our example service implementation in the OSGi service registry
bc.registerService( MyFirstService.class.getName(), new MyFirstServiceImpl(), null );
}

/**
* Called whenever the OSGi framework stops our bundle
*/
public void stop( BundleContext bc )
throws Exception
{
System.out.println( "STOPPING com.myExample" );

// no need to unregister our service - the OSGi framework handles it for us
}
}

• Change the osgi.bnd file found in com.myExample as follows:

Bundle-Activator: ${bundle.namespace}.internal.ServicePublisher

6. Delpoy OSGI bundle to OSGI runtime environment

>mvn install pax:provision -Dframework=equinox

Yo will get the Equinox prompt as follows:

osgi> STARTING com.myExample
REGISTER com.myExample.ExampleService

This means your service is deloyed. Do "ss" and look at the installed bundle summery

>ss

Framework is launched.

id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.1.R35x_v2
1 ACTIVE com.myExample_1.0.0.SNAPSHOT

As you can see our service is deployed and in active state.
Now type "stop 1" to resolve our service and u will get

osgi> stop 1
STOPPING com.myExample

Type "exit" to return from Equinox container.

7. Create OSGI consumer bundle

>mvn pax:create-bundle -Dpackage=com.myExample.user -Dname=consumer -Dversion=1.0-SNAPSHOT

Since this is the consumer bundle we do not need a generated ExampleService and ExampleServiceImpl classes. Therefore these two classes need to be deleted.

• ExampleActivator.java->(rename)Consumer.java

package com.myExample.user.internal;

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

import com.myExample.MyFirstService;

/**
* Extension of the default OSGi bundle activator
*/
public final class Consumer
implements BundleActivator
{
/**
* Called whenever the OSGi framework starts our bundle
*/
public void start( BundleContext context )
throws Exception
{
MyFirstService myFirstService= getService(context);
System.out.println(myFirstService .sayHello(" world"));

}

/**
* Called whenever the OSGi framework stops our bundle
*/
public void stop( BundleContext context )
throws Exception
{

}

private MyFirstService getService(BundleContext context){

ServiceReference ref = context.getServiceReference(MyFirstService.class.getName());
MyFirstService myFirstService=(MyFirstService) context.getService(ref);
return myFirstService;

}
}

• Change the osgi.bnd file found in com.myExample as follows:

Bundle-Activator: ${bundle.namespace}.internal.Consumer


Before deloy the consumer bundle in OSGI runtime we need to add the Service bundle dependency to the consumer. TO do that you need to go inside the newly created consumer bunle.

>cd com.myExample.user

Then impot the service bundle as follows:

>
mvn pax:import-bundle -DgroupId=example.com.xyz -DartifactId=com.myExample -Dversion=1.0-SNAPSHOT

Now go back to the parent directory.

>cd..

8.Delpoy OSGI bundle to OSGI runtime environment

>mvn install pax:provision -Dframework=equinox


You will see “hi world” in your Equinox prompt.
That means our Consumer bundle has successfully communicated with the Service Bundle.
Type “ss” and you can get the deployed bundle summery.

Now it's your turn to try this.

No comments: