Eclipse-free OSGi

how-to
Jul 21, 20083 mins

OSGi word is producing lots of buzz recently, so I assume we’re all going to use it sooner or later. Most articles on OSGi are based on Eclipse implementation, or at least refer to it in some way. I’m not a big Eclipse fan – had to use RAD at work a lot and really prefer Idea to it (fortunately they provide educational license). So, I was wondering how OSGi feels like without any Eclipse and decided to create a hello world bundle – FirstBundle. First of all I stumbled over the OSGi API. If one needs a pure OSGi API JAR file from the

OSGi alliance

he will need to register first, and then they will email the link to the API jar. Weird, don’t you think? Once osgi.core.jar is downloaded a simple Java class can be created against it. A plain implementation of BundleActivator will suffice our needs:


package test;

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

public class FBActivator implements BundleActivator {

	public void start(BundleContext bundleContext) throws Exception {
		System.out.println("hello world");
	}

	public void stop(BundleContext bundleContext) throws Exception {
		System.out.println("bye");
	}
}

We’ll keep it in src/test folder. Compilation is not a problem – we can do it with Ant. The problem with OSGi is packaging. To create a bundle means packaging of all classes/resources into a JAR file accompanied by a special manifest file. Manifest file creation is the most complicated thing, so one of OSGi guys created a tool – Bnd, that can be used with Ant. Bnd keeps bundle setting in a bnd file maintained by a developer. Then, at the bundle production stage, Bnd tool reads that file, analyses info from Java sources and packages required Java classes along with automatically produced manifest file into a JAR (which is already a usable bundle). Download the latest stable bnd.jar from

here

and put it somewhere. Then create a bnd file. In our simplest case FirstBundle.bnd file is:


Export-Package: test
Bundle-Activator: test.FBActivator

It tells OSGi container that FBActivator is the class to be activated upon bundle startup. Now compilation and packaging, all with Ant build.xml script:


<project name="FirstBundle">
	<target name="compile">
		<delete dir="classes" quiet="yes"/>
		<mkdir dir="classes"/>
		<javac srcdir="src" destdir="classes" classpath="osgi.core.jar"/>
	</target>
	<target name="build" depends="compile">
		<taskdef resource="aQute/bnd/ant/taskdef.properties"
				 classpath="bnd.jar"/>
		<bnd
				classpath="classes"
				sourcepath="src"
				failok="false"
				exceptions="true"
				files="FirstBundle.bnd"/>
	</target>
</project>

Given that paths to jars, sources, classes and bnd file are correct running “ant build” will produce a real OSGi bundle. Now, lets run it. Apache provides an OSGi container Felix. Not sure about its qualities, but it’s small and sufficient for us. Download

felix archieve

, unpack, go to its root directory. Type ‘java -jar bin/felix.jar’ – and you’re inside OSGi container console. Type ‘install file:/path/to/FirstBundle.jar’ and if you’re lucky it will reply to you with something like ‘Bundle ID: 4’. Now type ‘start 4’ and the hello world message will appear. Congratulations, now you have a skeleton to start development of your OSGi bundles in the Eclipse-free manner!