By Steve Robinson

steve.robinson@themiddlewareshop.com

http://www.themiddlewareshop.com

Contents

Exercise 12 – OSGi    1

Introduction    2

OSGI Technology Overview    2

Project Structure    2

Product Lifecycle    2

Step 1: Creating the business interface    3

Result    8

Step2: Creating the Business Implementations    8

Creating a new package to contain implementations    11

Creating an implementation class    13

Export the bundle    17

Deploy a bundle using WAS console    18

Complete the remaining 2 bundles    21

Example V2 Error    26

Example V3 Error    26

Set the manifests    27

Import in to WAS (Deploy)    30

Result    30

Step 3: Creating the Servlet bundle    30

Creating the OSGICalculatorServlet    35

Modify the doGet() method    39

Step 4: Creating the OSGI Application    41

Result    42

Result of the manifest update    44

Export the application    44

Deploy Assets    45

Create a new BLA    47

Add assets to the BLA    48

Start the aplication    50

Step 5: Testing the OSGi Application    52

Step 6: Rolling back the configuration    53

Move the application to the latest Composition unit    54

Result:    57

Summary    58

Introduction

In this example we look at a simple OSGI application & bundles along with deploying these assets to WAS.

Resources:

OSGi Examples

OSGI Technology Overview

OSGI or Open Services Gateway Initiative is a common way to implement Service Oriented programming inside a single JVM, this form of programming was available for J2SE programming only until the introduction of the OSGI feature pack for WebSphere Application Server 7.5 and is lately fully available in WebSphere 8.0.

This tutorial will not try to explain why OSGI programming model is good or any best practices of how to implement it correctly, what it will explain is how to create your first OSGI “Hello World – Type” application and understand the basic capabilities inside the OSGI implementation.

Project Structure

Let’s first discuss the structure of the application which we want to build for

illustrating capabilities.

Enterprise Assets (BLA’s) :

  1. OSGICalculatorInterface (symName: OSGICalculatorInterface,version 1.0)
  2. OSGICalculatorImplV1 (symName: OSGICalculatorImpl, version 1.0)
  3. OSGICalculatorImplV2 (symName: OSGICalculatorImpl, version 2.0)
  4. OSGICalculatorImplV3 (symName: OSGICalculatorImpl, version 3.0)
  5. OSGICalculatorServlet (symName: OSGICalculatorServlet, version 1.0)

Business Level Applications (EBA)

  1. OSGICalculatorAPP (symName: OSGICalculatorAPP, version 1.0)

Product Lifecycle

OSGI is all about promoting modularity, loose coupling and code reuse, what we will do in this demo project , is to create three different versions of the same OSGI bundle (OSGICalculatorImpl) and we will deploy at as software assets into the internal OSGI registry as assets, then we will develop an application which will consume one of those assets.

Once the application is done and tested we will discover that our implementation was incorrect and we will need to change to some previous version of the implementation prior to the time the defect was introduced. In a non OSGI environment this will need to include rebuilding the application with different code and redeploying the application which will force an application restart. In an OSGI environment things will be quite different.

Step 1: Creating the business interface

Creating a business interface called OSGICalculatorInterface within a new OSGI Bundle Project

Make sure you uncheck “Application Membership”

Click Next, Next

Make a note of the following screen and noted own the Symbolic Name Field which is the Bundles ID and also the version qualifier which is the version of the bundle we will want to build.

Symbolic Name (ID): OSGICalculatorInterface

Version: 1.0.0.qualifier

Click Finish, then using the project explorer create a new package called calc

Create a new interface named as OSGICalculatorInterface

Click Finish

Open the manifest editor as seen below

Navigate to the runtime tab and export the interface by clicking add and selecting calc package

Then select again and add version 1.0 to it’s version as seen below.

Result

Step2: Creating the Business Implementations

Create a new OSGI Bundle using the process we just completed with the following properties

Project Name = OSGICalculatorImpl

Application Memebership = unchecked

Symbolic Name = OSGICalculatorImpl

Version = 1.0.0.qualifier

Finish

Creating a new package to contain implementations

Create a new package called calc.impl

Edit the manifest as before

Using the Manifest Editor, add a dependency as an imported package.

Save

Creating an implementation class

Create a new class in the calc.impl package

Call the class OSGICalculator and make sure it implements the OSGIInterface as seen below

Using Quick Fix import the messing package ie the Interface

Using Quick fix add the unimplemented methods as declared by the interface, use right-mouse click

Or use CTRL-1 to see quick-fix

Ensure your class has the following code:

/**
* Exercise 12 OSGI

*/

package calc.impl;

import calc.OSGICalculatorInterface;

/**

* @author
Steve Robinson

*

*/

public
class
OSGICalculator
implements OSGICalculatorInterface {

    @Override

    public
int add(int x, int y) {

        // TODO Auto-generated method stub

        int result = x + y;

        System.out.println(“OSCICalaculator V1 was [Add] invloked!”);

        

        return result;

    }

    @Override

    public
int mult(int x, int y) {

        // TODO Auto-generated method stub

        int result = x * y;

        System.out.println(“OSCICalaculator V1 was [MULTIPLY] invloked!”);

        

        return result;

    }

}

As you can see in the code above, is contains basic implementations of an Add and Multiply operations. This is the only implementation that is correct in this demo, all other examples will be wrong. This will enable us to demo OAGI capabilities for correcting an application.

Open the manifest editor and make sure to add the calculator implementation you have just created, to the exported packages as seen below.

Save

Export the bundle

Export your bundle to a suitable location is c:\temp or temp etc

Deploy a bundle using WAS console

Ensure WAS is running and log into the console for example: http://localhost:9061/ibm/console/

Navigate to Environment -> OSGi bundle repositories -> Internal bundle respository

Click New

Browse for your JAR file:

Click OK, then Save

The result will be that you see the OSGICalculatorImpl V1 bundle

Complete the remaining 2 bundles

Using the exact same process, create two more Implementation Bundles. Make sure each on has an error, ensure that you set the Version to be 2.0.0.qualifier, 3.0.0.qualifer so we can identify the version 2 and 3 which contains errors.

Also double check that you use the same Implementation name : OASICalculatorImpl.

Result:

Set up the classes as required, edit the manifest and exports as per V1 earlier.

Example V2 Error

/**
* Exercise 12 OSGI

*/

package calc.impl;

import calc.OSGICalculatorInterface;

/**

* @author
Steve Robinson

*

*/

public
class OSGICalculator implements OSGICalculatorInterface {

    @Override

    public
int add(int x, int y) {

        // TODO Auto-generated method stub

        int result = x + x;

        System.out.println(“OSCICalaculator V1 was [Add] invloked!”);

        

        return result;

    }

    @Override

    public
int mult(int x, int y) {

        // TODO Auto-generated method stub

        int result = x * x;

        System.out.println(“OSCICalaculator V1 was [MULTIPLY] invloked!”);

        

        return result;

    }

}

Example V3 Error

/**
* Exercise 12 – OSGI

*/

package calc.impl;

import calc.OSGICalculatorInterface;

/**

* @author Steve Robinson

*

*/

public class OSGICalculator implements OSGICalculatorInterface {

    @Override

    public int add(int x, int y) {

        // TODO Auto-generated method stub

        int result = y + y;

        System.out.println(“OSCICalaculator V1 was [Add] invloked!”);


        return result;

    }

    @Override

    public int mult(int x, int y) {

        // TODO Auto-generated method stub

        int result = y * y;

        System.out.println(“OSCICalaculator V1 was [MULTIPLY] invloked!”);


        return result;

    }

}

Your project explorer should look like this:

Set the manifests

Make sure your manifests are set for identifying versions when exported

Export and import separate JAR files (Bundles) using the process for V1.

Import in to WAS (Deploy)

Import into WAS as per the version on import process.

Result

Result after imports:

Tip: When exporting you can add timestamps to further identify versions.

Step 3: Creating the Servlet bundle

Create a new bundle project called OSGICalculatorServlet

Add an OSGI Dependency as an imported package, but adding the manifest

Result:

Note: the calc.impl is used without an explicit version in order to enable accessing the implementation which is currently active (not static with a version). To remove an actual vision, click bundle -> properties to remove the version number.

Creating the OSGICalculatorServlet

Create a new package called servlets in the OSGICalculatorServlet Project.

Create a new Servlet called OSGICalculatorServlet in the servlets package

Take note of the mappings for later

/OSGICalculatorSevlet

Generate doGet()

Modify the doGet() method

Modify the doGet() Method to as seen below.

Here is the code you need to enter:

protected
void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        OSGICalculator calc = new OSGICalculator();

        int result = calc.add(12, 13);

        System.out.println(“result=” + result);

    }

Note: There is no mentioning of a specific version! During the call to V1, V2, V3 etc.

Step 4: Creating the OSGI Application

Now we have our modular OSGi bundles, we an create an OSGi application.

Create a new OSGi Application Project called OSGICalculatorApp

Next

In the Contained OSGi Bundles and Composite Bundles screen select the required bundles. In this example we are including the V3 defect bundle i.e. where we have y+y and y*y which is the wrong implemented business logic.

OSGICalculatorInterface

OSCICalculatorImpl 3.0.0

OSGICalculatorServlet

Click Finish

Result

Result as seen in Project Explorer

If you look at the OSGICalulatorApp’s Manifest you can see the following:

In order for the application to use previous versions of bindles, click the OSGICalculatorImpl – > properties and set the minimum version to match.

Result of the manifest update

Verify your manifest is updated as required.

Save

Export the application

Export the OSGi App

Deploy Assets

Log into the Administrative Console of WAS,

Navigate to Applications -> Application Types -> Assets

Click Import

Click Next until end

Save

Result:

Create a new BLA

Navigate to Applications -> Business Level Applications – New

Apply, Save

Add assets to the BLA

Click Next, Next, not the Context Root of the application’s servlet

Click Next, Next then Finish

Click Save

Start the aplication

Start the application and check SystemOut.log

Result:

[07/11/12 13:23:49:952 GMT] 00000059 StepStartBLA A CWWMH0196I: Business-level application “WebSphere:blaname=OSGICalculatorApp” was started successfully.

Step 5: Testing the OSGi Application

http://localhost:9081/OSGICalculatorServlet/OSGICalculatorServlet

Result:

Logs:

[07/11/12 13:29:29:389 GMT] 00000033 servlet I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [AsynchronousServletEAR] [/AsynchronousServletEARWeb] [AsyncServlet]: Initialization successful.
[07/11/12 13:29:29:545 GMT] 00000061 SystemOut O AsyncWorker:Class: ENTER run

[07/11/12 13:29:29:545 GMT] 00000037 SystemOut O Event completed.

[07/11/12 13:30:10:764 GMT] 00000036 servlet I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [OSGICalculatorApp..1.0.0.201211071302..OSGICalculatorServlet..1.0.0.201211071302.war] [/OSGICalculatorServlet] [sevlets.OSGICalculatorServlet]: Initialization successful.

[07/11/12 13:30:10:764 GMT] 00000036 SystemOut O OSCICalaculator V1 was [Add] invloked!

[07/11/12 13:30:10:764 GMT] 00000036 SystemOut O result=26

We can see that we are getting the result [26] y=13 + y=13, we want x=12 + y=13 [25]

We need to roll back the application to a previous implementation in order to get the correct intended results.

Step 6: Rolling back the configuration

Navigate to Application -> Application Types -> Assets -> OSGICalculatorApp.eba -> update bundle versions for this application.

Click Preview, then Create

Save

We have only updated the assets.

No changes were made to the running application, until we move the application to the latest composition unit.

Move the application to the latest Composition unit

Navigate to Applications -> Business-level Applications

Select the OSGICalculatorApp, and locate the EBA.

Locate and click “Update to latest deployment ..”

Review, then click OK

Save changes

Invoke the Servlet again

Result:

[07/11/12 13:41:29:249 GMT] 00000035 servlet I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [OSGICalculatorApp..1.0.0.201211071302..OSGICalculatorServlet..1.0.0.201211071302.war] [/OSGICalculatorServlet] [sevlets.OSGICalculatorServlet]: Initialization successful.
[07/11/12 13:41:29:264 GMT] 00000035 SystemOut O OSCICalaculator V1 was [Add] invloked!

[07/11/12 13:41:29:264 GMT] 00000035 SystemOut O result=25

Congratulations, you have now completed the basics of OSGi Development, Deployment & Management.

Summary

The tutorial has introduced the concepts of OSGi and the flexibility with the programming model it provides. We have learned that we can modularize our components in to composite units (assets) and use the WebSphere Application Server’s Business Level Application concepts mage assets versions. OSGi also supports roll back in very easy to understand process. We have now harnessed the power of both OSGi and WAS. OSGi offer significant power and flexibility to those who choose to use it.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply