The "One Laptop Per Child" project has a great device ready to ship, but there's no Java on there. Let's think about working together to put Java on OLPC!
Welcome to the much-delayed next installment of the "Getting Started with OSGi" series. This installment is quite exciting, because we're going to start experimenting with Declarative Services.
The Declarative Services (or "DS") specification is one of the newest parts of OSGi, and it came about as a result of some of the issues with wiring together service across bundles. It's not that this task is difficult – as I hope my previous tutorials showed – but it does require a fair amount of boilerplate code. It also requires you to be cautious about threading issues, which means you can easily shoot yourself in the foot.
An early attempt to resolve this problem was a tool called Service Binder, developed by
Humberto Cervantes
and
Richard Hall
. Service Binder introduced
automated service dependency management
, allowing developers to focus on the job of writing their services. The wiring between services was handled declaratively, and the declarations were made in XML.
The Declarative Services specification evolved from Service Binder, and it is now a standard part of OSGi versions 4.0 and above. So, lets see what it can do for us.
As I mentioned in the previous installment of the tutorial, I'm tired of doing everything on the command line: from this point onwards, we will use the Eclipse SDK. Please remember though that none of what I'm introducing really depends on Eclipse. Although Eclipse helps a lot with what we're about to do, there's no black magic going on, so anything you see here is perfectly possible in NetBeans, IntelliJ or even good old vi.
The first thing we need to do is download the Equinox implementation of Declarative Services. Assuming you're using the current stable version of Eclipse, which is 3.2.2 at time of writing, the direct link is
here
. If you're using a different version, you need to find that version from the top-level Equinox download page
here
and get the file
org.eclipse.equinox.ds_x.x.x_xxxxx.jar
. Once it's downloaded, drop it into the
plugins
directory of your Eclipse installation, and restart Eclipse. Actually if you know a little about Eclipse plug-in development already, you should put this JAR into your Target Platform folder. If you don't know what a "Target Platform" is, then don't worry about it, just drop the JAR into the
plugins
folder.
Now we'll create a new bundle. To do this, we create a project in Eclipse using the Plug-in Project wizard:
From the main menu, select File -> New -> Project.
Select Plug-in Project and hit Next.
Enter the name of the project,
SampleExporter
.
At the bottom, below the text "This plug-in is targeted to run with", select "an OSGi framework" and then select "standard" from the drop-down. This step isn't absolutely essential, it just prevents us from accidentally using features that are not available out-of-the-box on other OSGi framework implementations.
Click Next. On the following page of the wizard, deselect "Generate an activator...", if it is checked. Then click Finish.
We now have an empty bundle project, so we need to add some code. To keep things as simple as possible, we're going to offer a service using an interface that's already available on every Java runtime:
java.lang.Runnable
. Using a handy little Eclipse shortcut, just copy the following code from your browser, and then select the
src
folder inside the
SampleExporter
project and hit Edit -> Paste. Eclipse will create the package and source file for you.
So far we haven't seen anything new, but here's where it gets interesting: we're going to create an XML file to declare
SampleRunnable
as a service. Create a folder at the top level of the project called
OSGI-INF
and copy the following into a file in that folder called
samplerunnable.xml
:
This is one of the simpler declarations we will see with DS. It says that there is a component called "samplerunnable" which provides a service to the OSGI Service Registry under the interface
java.lang.Runnable
, and the component is implemented by the class
org.example.ds.SampleRunnable
.
The final step is to tell the Declarative Services runtime about the existence of this XML file. We do this by adding a field to the MANIFEST.MF for the bundle. So, open the manifest (try right-clicking on the project and selecting PDE Tools -> Open Manifest) and in the editor, skip to the tab labeled "MANIFEST.MF", as this allows us to edit the text contents of the manifest directly. Add the following line:
Service-Component: OSGI-INF/samplerunnable.xml
Then save the file. As in previous installments, the blank line at the end of the manifest is VERY important, but unlike previously, you will now get an error message from Eclipse if you forget.
Before going any further, lets run Equinox to check that this works. Go to the Run menu and select "Run...". When the dialog opens, select "Equinox OSGi Framework" in the tree on the left, and then hit the New button. If you're an experienced Eclipse user but have not done OSGi development, this launcher dialog might look a little bit strange. The first tab, labeled Bundles, allows us to choose which bundles should be included in the launch configuration, and whether they should be initially started or not. To get the minimal set, click Deselect All and then place a tick next to the following bundles only:
These are the bundles we are interested in, but they do have dependencies, so click Add Required Bundles to add those dependencies back in. Also it's a good idea to select "Validate bundles automatically prior to launching". Finally, click Run, and in a few moments the
osgi>
prompt should appear in the Eclipse console. The OSGi framework is running, and you can interact with it using the same commands you learned previously.
So is our service registered? We can check by typing the command
services
. Somewhere in the list of services – probably the bottom – you should see the following:
{java.lang.Runnable}={component.name=samplerunnable, component.id=1, service.id=22}
Registered by bundle: initial@reference:file:..../SampleExporter/ [5]
No bundles using service.
Yes, it's registered! And notice one important thing: it appears to have been registered by our bundle,
not
by the Declarative Services bundle. In fact DS has registered it
on behalf of
our bundle. We'll look at how it does that a little later on, but for the purposes of this lesson, it's enough to note that consumers of our service don't need to do anything special, in fact they need not even know that we are using Declarative Services. Those consumers can use Declarative Services as well if they wish to, but they can also use straightforward OSGi code.
Another thing to note is that in our bundle, there is no OSGi-specific Java code. In other words, the class we wrote is a POJO, and this is a major feature of Declarative Services.
That's enough for this installment, and I'm sorry that this lesson is a little basic. Next time we'll look at how to consume services in various ways with DS, which should really start to show the power and convenience that it offers.
Re: Getting Started with OSGi: Introducing Declarative Services
Thanks for the nice tutorials, they are really useful... =)
I guess you'll cover my question in the next installment but I'm eager to know...
Let's say we have our service bundle installed and resolved. How do I make sure this bundle/service is started when another bundle needs it without creating a dependency to it?
As I see it one would want to code services because they are easy to relate to and you can publish functionality without creating dependencies to the implementation in a very clean and efficient way. But...how do I make sure the service is started when someone needs it without using IStartup etc...?
Re: Getting Started with OSGi: Introducing Declarative Services
Hi Patrik,
Thanks for your comment. You're right, I will be covering this exact subject shortly. However, the answer is a little complicated, so I'd rather not get into it here. Instead I beg you to be patient and read the full tutorial installment when it comes out.
Re: Getting Started with OSGi: Introducing Declarative Services
Something is wrong.
In eclipse-SDK-3.2.2-win32.zip,
when I use button "Validate Plug-in Set" I have received:
Plug-in "org.eclipse.equinox.ds" will be disabled
Plug-in "org.eclipse.osgi" is missing
And when I inqulude "org.eclipse.equinox.ds", run and at OSGi prompt enter services
have received the result:
{org.eclipse.osgi.framework.console.CommandProvider}={service.ranking=2147483647, service.id=18}
Registered by bundle: System Bundle [0]
Bundles using service:
System Bundle [0]
{java.lang.Runnable}={name=splashscreen, service.id=19}
Registered by bundle: System Bundle [0]
No bundles using service.
Nothing like:
{java.lang.Runnable}={component.name=samplerunnable, component.id=1, service.id=22}
Registered by bundle: initial@reference:file:..../SampleExporter/ [5]
No bundles using service.
Re: Getting Started with OSGi: Introducing Declarative Services
Hi Neil,
I've been following your articles from the beginning with great interest and I have been eagerly waiting for the last installment on Declarative Services. I've come across a problem though when updating a bundle.
I followed your instructions to the letter and I see the component in the "services" listing. However, when I try to uninstall and reinstall the SampleExporter after exporting it to a JAR in my target directory, it no longer shows as a component.
These are the steps I perform after exporting the plug-in:
osgi> install file:///plugins/SampleExporter_1.0.0.jar
Bundle id is 62
osgi> refresh 62
osgi> start 62
osgi> services
And it is no longer in the list. Should the service be automatically registered again?
Re: Getting Started with OSGi: Introducing Declarative Services
Hi Neil,
It helps to include the OSGI-INF directory in the "Build" tab before exporting the bundle. The solution became obvious when I compared the output to "bundle SampleExporter".
Re: Getting Started with OSGi: Introducing Declarative Services
I followed the instructions as stated, but i am unable to see the "org.example.ds" under the target platforms when I try to run the application. As a result, I do not see our service when I use services command. What might be the problem? Any ideas?
Re: Getting Started with OSGi: Introducing Declarative Services
hey... loved the article.
one of the questions i was left with was:
i assumed that when any plugin requests a service that has been declared the declaring plug-in would be activated if not already active.
i have pluginA that declares a service. pluginB creates a tracker to consume that service. when pluginB asks for the service pluginA isn't activated and the service isn't found.
am i wrong in my assumption of how this should work?
Getting Started with OSGi: Introducing Declarative Services
At 6:54 PM on Jun 3, 2007, Neil Bartlett
wrote:
The Declarative Services (or "DS") specification is one of the newest parts of OSGi, and it came about as a result of some of the issues with wiring together service across bundles. It's not that this task is difficult – as I hope my previous tutorials showed – but it does require a fair amount of boilerplate code. It also requires you to be cautious about threading issues, which means you can easily shoot yourself in the foot.
An early attempt to resolve this problem was a tool called Service Binder, developed by Humberto Cervantes and Richard Hall . Service Binder introduced automated service dependency management , allowing developers to focus on the job of writing their services. The wiring between services was handled declaratively, and the declarations were made in XML.
The Declarative Services specification evolved from Service Binder, and it is now a standard part of OSGi versions 4.0 and above. So, lets see what it can do for us.
As I mentioned in the previous installment of the tutorial, I'm tired of doing everything on the command line: from this point onwards, we will use the Eclipse SDK. Please remember though that none of what I'm introducing really depends on Eclipse. Although Eclipse helps a lot with what we're about to do, there's no black magic going on, so anything you see here is perfectly possible in NetBeans, IntelliJ or even good old vi.
The first thing we need to do is download the Equinox implementation of Declarative Services. Assuming you're using the current stable version of Eclipse, which is 3.2.2 at time of writing, the direct link is here . If you're using a different version, you need to find that version from the top-level Equinox download page here and get the file
org.eclipse.equinox.ds_x.x.x_xxxxx.jar. Once it's downloaded, drop it into thepluginsdirectory of your Eclipse installation, and restart Eclipse. Actually if you know a little about Eclipse plug-in development already, you should put this JAR into your Target Platform folder. If you don't know what a "Target Platform" is, then don't worry about it, just drop the JAR into thepluginsfolder.Now we'll create a new bundle. To do this, we create a project in Eclipse using the Plug-in Project wizard:
SampleExporter.We now have an empty bundle project, so we need to add some code. To keep things as simple as possible, we're going to offer a service using an interface that's already available on every Java runtime:
java.lang.Runnable. Using a handy little Eclipse shortcut, just copy the following code from your browser, and then select thesrcfolder inside theSampleExporterproject and hit Edit -> Paste. Eclipse will create the package and source file for you.package org.example.ds; public class SampleRunnable implements Runnable { public void run() { System.out.println("Hello from SampleRunnable"); } }So far we haven't seen anything new, but here's where it gets interesting: we're going to create an XML file to declare
SampleRunnableas a service. Create a folder at the top level of the project calledOSGI-INFand copy the following into a file in that folder calledsamplerunnable.xml:This is one of the simpler declarations we will see with DS. It says that there is a component called "samplerunnable" which provides a service to the OSGI Service Registry under the interface
java.lang.Runnable, and the component is implemented by the classorg.example.ds.SampleRunnable.The final step is to tell the Declarative Services runtime about the existence of this XML file. We do this by adding a field to the MANIFEST.MF for the bundle. So, open the manifest (try right-clicking on the project and selecting PDE Tools -> Open Manifest) and in the editor, skip to the tab labeled "MANIFEST.MF", as this allows us to edit the text contents of the manifest directly. Add the following line:
Then save the file. As in previous installments, the blank line at the end of the manifest is VERY important, but unlike previously, you will now get an error message from Eclipse if you forget.
Before going any further, lets run Equinox to check that this works. Go to the Run menu and select "Run...". When the dialog opens, select "Equinox OSGi Framework" in the tree on the left, and then hit the New button. If you're an experienced Eclipse user but have not done OSGi development, this launcher dialog might look a little bit strange. The first tab, labeled Bundles, allows us to choose which bundles should be included in the launch configuration, and whether they should be initially started or not. To get the minimal set, click Deselect All and then place a tick next to the following bundles only:
SampleExporter(underneath Workspace)org.eclipse.equinox.ds(underneath Target Platform)These are the bundles we are interested in, but they do have dependencies, so click Add Required Bundles to add those dependencies back in. Also it's a good idea to select "Validate bundles automatically prior to launching". Finally, click Run, and in a few moments the
osgi>prompt should appear in the Eclipse console. The OSGi framework is running, and you can interact with it using the same commands you learned previously.So is our service registered? We can check by typing the command
services. Somewhere in the list of services – probably the bottom – you should see the following:{java.lang.Runnable}={component.name=samplerunnable, component.id=1, service.id=22} Registered by bundle: initial@reference:file:..../SampleExporter/ [5] No bundles using service.Yes, it's registered! And notice one important thing: it appears to have been registered by our bundle, not by the Declarative Services bundle. In fact DS has registered it on behalf of our bundle. We'll look at how it does that a little later on, but for the purposes of this lesson, it's enough to note that consumers of our service don't need to do anything special, in fact they need not even know that we are using Declarative Services. Those consumers can use Declarative Services as well if they wish to, but they can also use straightforward OSGi code.
Another thing to note is that in our bundle, there is no OSGi-specific Java code. In other words, the class we wrote is a POJO, and this is a major feature of Declarative Services.
That's enough for this installment, and I'm sorry that this lesson is a little basic. Next time we'll look at how to consume services in various ways with DS, which should really start to show the power and convenience that it offers.
11 replies so far (
Post your own)
Re: Getting Started with OSGi: Introducing Declarative Services
Thanks for the nice tutorials, they are really useful... =)I guess you'll cover my question in the next installment but I'm eager to know...
Let's say we have our service bundle installed and resolved. How do I make sure this bundle/service is started when another bundle needs it without creating a dependency to it?
As I see it one would want to code services because they are easy to relate to and you can publish functionality without creating dependencies to the implementation in a very clean and efficient way. But...how do I make sure the service is started when someone needs it without using IStartup etc...?
Br
Patrik Schalin
Re: Getting Started with OSGi: Introducing Declarative Services
Hi Patrik,Thanks for your comment. You're right, I will be covering this exact subject shortly. However, the answer is a little complicated, so I'd rather not get into it here. Instead I beg you to be patient and read the full tutorial installment when it comes out.
Regards
Neil
Re: Getting Started with OSGi: Introducing Declarative Services
Something is wrong.In eclipse-SDK-3.2.2-win32.zip,
when I use button "Validate Plug-in Set" I have received:
Plug-in "org.eclipse.equinox.ds" will be disabled
Plug-in "org.eclipse.osgi" is missing
And when I inqulude "org.eclipse.equinox.ds", run and at OSGi prompt enter services
have received the result:
{org.eclipse.osgi.framework.console.CommandProvider}={service.ranking=2147483647, service.id=18}
Registered by bundle: System Bundle [0]
Bundles using service:
System Bundle [0]
{java.lang.Runnable}={name=splashscreen, service.id=19}
Registered by bundle: System Bundle [0]
No bundles using service.
Nothing like:
{java.lang.Runnable}={component.name=samplerunnable, component.id=1, service.id=22}
Registered by bundle: initial@reference:file:..../SampleExporter/ [5]
No bundles using service.
Boris Starchev
Teacher
Re: Getting Started with OSGi: Introducing Declarative Services
Hi Boris,Did you click "Add Required Bundles" before launching?
Regards
Neil
Re: Getting Started with OSGi: Introducing Declarative Services
You are quite right. I have missed "Add Required Bundles"!Thanks
Re: Getting Started with OSGi: Introducing Declarative Services
Hi Neil,I've been following your articles from the beginning with great interest and I have been eagerly waiting for the last installment on Declarative Services. I've come across a problem though when updating a bundle.
I followed your instructions to the letter and I see the component in the "services" listing. However, when I try to uninstall and reinstall the SampleExporter after exporting it to a JAR in my target directory, it no longer shows as a component.
These are the steps I perform after exporting the plug-in:
osgi> install file:///plugins/SampleExporter_1.0.0.jar
Bundle id is 62
osgi> refresh 62
osgi> start 62
osgi> services
And it is no longer in the list. Should the service be automatically registered again?
Thanks,
Seamus
Re: Getting Started with OSGi: Introducing Declarative Services
Hi Neil,It helps to include the OSGI-INF directory in the "Build" tab before exporting the bundle. The solution became obvious when I compared the output to "bundle SampleExporter".
Thanks,
Seamus
Re: Getting Started with OSGi: Introducing Declarative Services
I followed the instructions as stated, but i am unable to see the "org.example.ds" under the target platforms when I try to run the application. As a result, I do not see our service when I use services command. What might be the problem? Any ideas?Re: Getting Started with OSGi: Introducing Declarative Services
It's equinox.ds, not example.ds. Secondly, there's a link at the top of the article that tells you where to download it from.Alex.
Re: Getting Started with OSGi: Introducing Declarative Services
Thanks,I got it all wrong the last time and I was careless. It seems to be working now.
Re: Getting Started with OSGi: Introducing Declarative Services
hey... loved the article.one of the questions i was left with was:
i assumed that when any plugin requests a service that has been declared the declaring plug-in would be activated if not already active.
i have pluginA that declares a service. pluginB creates a tracker to consume that service. when pluginB asks for the service pluginA isn't activated and the service isn't found.
am i wrong in my assumption of how this should work?