OSGi Declarative Services, Eclipse Galileo Feature #2

June 23, 2009 | 3 min Read

As many people have already said, Galileo is available for Friends of Eclipse. As Chris pointed-out, one of the reasons Eclipse is able to ship quality software, consistently on-time, is because of the modularity offered by OSGi.

Using OSGi, developers are able to work independently on their piece and the final product assembled separately.  In addition to the componentization, OSGi enables lazy loading. Each bundle can contribute “services” and the bundles are only activated if the services are used. This means that an embedded OSGi application (with 10 bundles)  feels as responsive as a fully featured IDE with over 1,000 bundles. This lazy, service based, programming model is extremely powerful, however, very hard to get right. Eclipse 3.5 has completely changed this by making OSGi Declarative Services a first class citizen– Number 2 On My Top 10 List of Galileo Features.

Declarative Services (DS) is an OSGi specification to make it easier to wire services together across bundles. Using DS, developers can specify what services their bundle provides and what services are required.  This is all specified “declaratively” using XML, and the DS bundle is responsible for wiring everything together.  Huge kudos go out to ProSyst for this work, with special thanks to Stoyan.

In the example above, this particular component provides a “CommandProvider” and requires a single instance of the “Dictionary Service”.  The Dictionary service is provided in the bundle activator using the Service Tracker:

// register the service
context.registerService(DictionaryService.class.getName(), service, props);

// create a tracker and track the service
dictionaryServiceTracker = new ServiceTracker(context, DictionaryService.class.getName(), null);

// have a service listener to implement the whiteboard pattern
fContext.addServiceListener(this, "(objectclass=" + Dictionary.class.getName() + ")");

When launched, assuming both the Command Provider and the Dictionary are available, the user can look-up a word using the OSGi console:

Like many of the best Eclipse run-time technologies, the Eclipse tooling is what catapults it into the mainstream.  In Eclipse 3.5, the Bundle Development Environment (PDE) introduced top notch tooling for working with DS. Using the DS tooling, you can craft component.xml files using a form based editor.  A big thank-you goes to Chris Aniszczyk and Rafael Nobrega for the tooling.  Rafael was a Google Summer of Code student!

As well as an excellent Run-time and top notch tooling, Chris also put together a template to help you get started.  Simply select New Plug-in Project, and choose the OSGi Declarative Services Example.

Eclipse continues to lead the way by providing Open Source, reference implementations, for a number of key specifications. By providing open implementations, organizations benefit by:

  1. Sharing the development cost
  2. Having a very large user base test their implementation
  3. Having a very large user base exposed to their technology
  4. Having a variety of different people (with different background) contribute to the work

For more information on DS and OSGi in general, checkout the new book by Jeff McAffer, Paul VanderLei and Simon Archer.

Thanks to ProSyst, Chris, Rafael and everyone else who worked on this implementation.  Another shining example of the win-win situation enabled by open source software development.

Ian Bull

Ian Bull

Ian is an Eclipse committer and EclipseSource Distinguished Engineer with a passion for developer productivity.

He leads the J2V8 project and has served on several …