| Free tutorials for Java, Eclipse and Web programming |
Declarative services (DS) allow to define and consume services via metadata (XML). Via DS you can define OSGi services without having any dependency to the OSGi platform, e.g. services can be defined as POJO's. This allows that these services can be tested independently of the OSGi runtime.
The "OSGi service component" is responsible for starting the service (service component). For the service consumer it is not visible if the service has been created via declarative service or via other means.
Service Components consists out of a XML description (Component Description) and an object (Component Instance). The component description contains all information about the service component, e.g. the class name of the component instance and the service interface.
A reference to the component description is maintained in the "MANIFEST.MF" file. This file is read by the OSGi runtime and if a component description is found the corresponding service is created. The component description in "MANIFEST.MF" looks for example like the following.
Service-Component: component.xml, OSGI-INF/component.xml
The following will define a DS service based on the quote example. It is therefore required that you have created the "de.vogella.osgi.quote" project which contains the interface definition.
Create a new plugin project "de.vogella.osgi.ds.quoteservice". Do not use a template, do not create an activator. Import package "de.vogella.osgi.quote" in MANIFST.MF on the tab "dependencies".
Create the Folder "OSGI-INF" in your project. Select the new folder, right-click on it and select New -> Other -> Plug-in Development -> Component Definition. The wizard will also add the "Service-Component" entry to "MANIFEST.MF".


This should open the service editor.

In component.xml switch to the service tab and press "Add" under "Provided Services" and select IQuoteService.

Create now the class "QuoteService" which implements the interface IQuoteService.
package de.vogella.osgi.ds.quoteservice;
import java.util.Random;
import de.vogella.osgi.quote.IQuoteService;
public class QuoteService implements IQuoteService {
@Override
public String getQuote() {
Random random = new Random();
// Create a number between 0 and 2
int nextInt = random.nextInt(3);
switch (nextInt) {
case 0:
return "Ds: Tell them I said something";
case 1:
return "Ds: I feel better already";
default:
return "Ds: Hubba Bubba, Baby!";
}
}
}
You have successfully defined a service via DS.
Go back to component.xml and select the "Source". The declaration of the service looks like the following.
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="de.vogella.osgi.ds.quoteservice">
<implementation class="de.vogella.osgi.ds.quoteservice.QuoteService"/>
<service>
<provide interface="de.vogella.osgi.quote.IQuoteService"/>
</service>
</scr:component>
This means that there is a component called "de.vogella.osgi.ds.quoteservice" which provides a service to the OSGI Service Registry under the interface "de.vogella.osgi.quote.IQuoteService", .and the component is implemented by the class "de.vogella.osgi.ds.quoteservice.QuoteService.
Check now the MANIFEST.MF. Here you find the entry: Service-Component: OSGI-INF/component.xml"
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Quoteservice Bundle-SymbolicName: de.vogella.osgi.ds.quoteservice Bundle-Version: 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Import-Package: de.vogella.osgi.quote Service-Component: OSGI-INF/component.xml
This defines that the plugin provides this services.
Declaratives service require a few more bundles. In addition to "org.eclipse.osgi" you also require.
org.eclipse.equinox.util
org.eclipse.equinox.ds - Declarative service
Copy the "org.eclipse.equinox.ds*.jar", "org.eclipse.osgi.services.jar" and "org.eclipse.equinox.util*.jar" from your Eclipse/plugin installation directory into a folder, e.g. "C:\temp\bundles\plugins" and install the bundle into your OSGi runtime via.
install file:c:\temp\bundles\plugins\org.eclipse.equinox.ds.jar install file:c:\temp\bundles\plugins\org.eclipse.equinox.util.jar install file:c:\temp\bundles\plugins\org.eclipse.osgi.services.jar
Export your bundle to "de.vogella.osgi.ds.quoteservice.jar". and install it via:
install file:c:\temp\bundles\plugins\de.vogella.osgi.ds.quoteservice.jar
To check if your service was registered use the command "services". This will list all installed and available services.
If you stop / uninstall the old service provider and start the new one your service should be picked up by the consumer.
