Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:
JavaWorld Daily Brew

Eclipse-free OSGi

Tags:

 

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!

I'm an Eclipse fan, but I

I'm an Eclipse fan, but I even agree that it's a shame that a lot of OSGi articles guide you in using the PDE and not in OSGi itself.

I'm doing a bit of writing about OSGi myself and I'm stressing how to do things without a specific IDE or editor. You can use Eclipse if you want to, but it's not a requirement. I *do* however use Equinox as the OSGi framework of choice because, at the moment, Felix doesn't support bundle fragments.

What's wrong with PDE?

PDE loves you too :)

Nice tutorial, simple and clean

Thanks for this tutorial, there is all one needs to start working with Osgi, without having to use eclipse, which is useful to avoid the assumption:
osgi = eclipse or osgi = equinox
Very simple yet complete.
Congratulations.

more on building with Ant

When building a lot of bundles with Ant, you might want to try this wrapper, which is also based on Bnd but allows you to build a bundle from Ant without requiring another file (per bundle):

https://opensource.luminis.net/confluence/display/SITE/OSGi+Bundle+Ant+T...

It could be a little easier with maven

Hi,

Although Ant is enough for most of the development chores, the configuration files and the bnd file developed in this article just to let Ant work have scared me a little bit. I'd go with Maven for such a simple task as creating a bundle and use Bundle Plugin for Maven [1] so even less would be necessary and the article would get even shorter. See the article of mine about OSGi, Maven and maven-bundle-plugin - Tworzenie pakietów OSGi z Apache Maven 2 [2] (it's in Polish though)

[1] http://felix.apache.org/site/maven-bundle-plugin-bnd.html
[2] http://www.jaceklaskowski.pl/wiki/Tworzenie_pakiet%C3%B3w_OSGi_z_Apache_...

Jacek

Maven easier?

Jacek,

I made several attempts to set up a Maven repository, but never could finish that task - it took me too much time.
So I'm not using Maven for my projects until I find either more time for that, or some volunteer to set up a Maven repository for our company :)

Oleg

Easier just for the task at hand...sure

I'm not saying setting up a maven repo is an easy task. It might be, but I don't really know as I haven't done it myself before. What I said was that in this particular use case where you tried to create an osgi bundle with ant beside creating the bundle you had to know much more about ant which I think would be easy to achieve with Maven (some Maven knowledge is surely required). Don't get me wrong - Ant has its place in java developer's toolbox and so does Maven. Just for the task at hand, maven would be better (=easier and faster in terms of how much time it takes to achieve the goal). Why should I bother about setting up a m2 repo if all I need is out there?

Jacek

Jacek, I don't neglect

Jacek,

I don't neglect Maven2, it's a very advanced tool.
If you use it you will never (need to) know where OSGi API JAR is actually coming from, how to compile against it, how does Manifest.mf get created, and lots of other things you can live without.
That's the first part of my answer.

The second is: I've read your first link/reference where they describe a 'simple OSGi bundle POM for Maven2', and the POM excerpt takes one screen of XML.
Doesn't seem much easier :)

Oleg

Maven repository

Hi Oleg,

You don't need to set up a maven repository, since maven will automatically just cache things on your own home directory. It's not ideal, but it just means the first time someone else in your company builds an artifacts on their machine it'll take a little longer because it has to download resources from the internet instead of your local network.

When it comes time to deploy your own artifacts it's very convenient to have a local repository, but even without it you are not off worse than you would be with ant!

I wrote an elaborate and somewhat complicated introduction to OSGi that relies on maven. It's a bit lengthy so I need to work on simplifying it - http://notehive.com/wp/2008/03/23/introduction-to-osgi/ (I do not cover setting up a maven repository :-)

-Hans

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.