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:
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:
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.