Monday, January 11, 2010

Step 0 - Simple OSGi

OSGi is a longstanding framework for building modular applications. I won't go into the details as I'm assuming you are somewhat interested in the framework to have found my blog. Two of the best resources I've found are:

Craig Walls' blog
Kirk Knoerschild's blog

Craig and Kirk are helping many (including myself) to grok the benefits of OSGi and dispel the myths around it's adoption. Specifically, Craig's post refuting the challenges with OSGi covers almost all of the issues usually brought up.

Now that that's settled, let's get a simple example up and running.

I'm going to be using Equinox for my examples. Primarily because it supports all of the features we'll need and also because it's bundled with Eclipse so you can easily find it and get it started.

Download equinox from here. You can just get by with the framework jar. You can start the eclipse container with

java -jar org.eclipse.osgi_3.5.1.R35x_v20090827.jar -console

Issuing a short status command with ss shows



Since we haven't built a bundle yet, the only thing available is the osgi core bundle. A bundle is simply no more than a jar file with a quirky META-INF/MANIFEST.MF file. Building the equivalent of Hello World can be done with this:

    1 package com.pillartech;
2
3 import org.osgi.framework.BundleActivator;
4 import org.osgi.framework.BundleContext;
5
6 public class SuperSimple implements BundleActivator {
7
8 public void start(BundleContext ctx) throws Exception {
9 System.out.println("Starting Up!");
10
}

11
12 public void stop(BundleContext ctx) throws Exception {
13 System.out.println("Shutting Down!");
14
}

15 }


This is just a java file though until we add the magic to a MANIFEST.MF file and jar the thing up. Here is a simple manifest to get started:

    1 Bundle-ManifestVersion: 2
2 Bundle-SymbolicName: com.pillartech.SuperSimple
3 Bundle-Name: SuperSimple
4 Bundle-Version: 1.0.0
5 Bundle-Activator: com.pillartech.SuperSimple
6 Import-Package: org.osgi.framework


As long as you include the symbolic name of the bundle it should work, but the rest of it gives you a feel of the meaning of the manifest. It provides all of the information used by other bundles to consume it, and also a list of the other bundles that it imports.

Compiling this class and jar-ing the class and manifest together allows you to deploy it and start it



From there you can see descriptive information about this bundle and it's manifest with the bundle and headers commands



Stopping the bundle will produce an equally productive message and return it to the INSTALLED state. Finally issuing an exit command will shutdown the container.

So with this example we've done the simplest thing we can to get a bundle installed, started, described, and stopped in equinox. These commands are the core to developing and manipulating bundles in a runtime with OSGi. We'll leverage them further in the subsequent examples to build our solution.

No comments: