OSGi Declarative Services, Eclipse Galileo Feature #2

OSGi Declarative Services, Eclipse Galileo Feature #2

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.

  • Posted at 9:32 am, June 24, 2009

    You find a little how-to guide for creating and consuming a declarative service here: http://www.vogella.de/articles/OSGi/article.html

  • Ian Bull
    Posted at 5:27 pm, June 24, 2009

    Thanks Lars,

    I keep stumbling upon your articles. They are very helpful.

  • Kamil Demecki
    Posted at 10:34 pm, June 24, 2009


    Declarative services is cool solution. I emphasize “declarative” word to express way of divide logic. But why using XML to define this spliting logic ?

    Dividing is fine, one code is responsible for services and other how these services inter-operate (like in Guice). Also Ejb3 come out from XML to Java and annotations.

    From I know, one strong point of XML is easy GUI wizards which operate on this XML, but one consistent solution in Java take advantage, specially speed of refactoring.

    Eclipse is cool, speed of searching dependencies in plugin.xml is ok in middle-size project, but why not Java Module to express this declarative logic ?

  • Víctor
    Posted at 10:03 am, June 30, 2009

    Hi Ian,

    nice articles regarding our beloved Galileo release train. Even for a day by day user, Eclipse keeps surprising with new cool features!

    Correct me if wrong, but I’ve used DS in Ganymede, and found a rather odd issue regarding Service contribution: these will only be contributed if the bundle is activated. Otherwise, they are not. I found that it killed the real power behind DS. I had t create my own support bundle that activated those bundles defining declarative services.

    I believe the PDE team was working to change this for 3.5, based on the latest OSGi spec update. Is DS lazy loading already supported in 3.5?


  • Posted at 1:35 pm, June 30, 2009

    Hi Victor. As of Eclipse, it’s possible to use DS in a lazy fashion due to the specification updates in OSGi r4.2

    PDE provides the latest tooling for DS files in Eclipse 3.5 and Equinox provides the latest DS runtime.

    Just remember that your component files need to be in the new DS 1.1 namespace…

    Also, ensure that the bundles are lazy started…
    Bundle-ActivationPolicy: lazy

    That should be it! Good luck!

  • Víctor
    Posted at 11:09 am, July 7, 2009

    Thanks for the Hint, Chris! 😀

  • Norm Heske
    Posted at 12:58 am, July 22, 2009

    Not sure if I was doing something non-standard, but I was having a hard time getting the OSGi Declarative Services Example provided in Galileo to run. Eventually, I explicitly added the org.eclipse.equinox.ds bundle and its associated dependent bundles and it worked. Seems to me that ds bundle is not flagged as required and the xml files are ignored.

  • Posted at 5:11 pm, July 22, 2009

    That’s correct Norm.

    You have to make sure that the DS bundle is added to your launch configuration and started (the default is for the bundle to be automatically started, but you have to add it to the launch configuration first).

  • Norm Heske
    Posted at 4:44 am, July 23, 2009

    Thanks Chris. Should the example template add that as a required bundle or have a comment to that effect? I suspect others will also have trouble running the example.

  • Posted at 10:54 pm, July 23, 2009

    Norm, it’s a bit complicated as your bundle doesn’t need to have any direct dependencies on DS.

    In most cases, you shouldn’t have a dependency to DS via an Import-Package or Require-Bundle. In the new version of DS that shipped with Eclipse 3.5, you can use POJOs with DS.

    However, I think we could do some extra validation in PDE when you validate a launch configuration and we come across a bundle that uses DS but DS isn’t in the list of bundles we launch against. Could you open a bug for this?

  • Norm Heske
    Posted at 1:44 pm, July 31, 2009


    I submitted Bug 285272.


  • Posted at 2:49 pm, May 28, 2011

    thanks for the useful info