RSS

Search Engine

Sunday, July 18, 2010

Declarative Services

7.1. Overview

Declarative services (DS) allow to define and consume services via metadata (XML). DS allow to 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 declarative services which are also called service components. For the service consumer it is not visible if the service has been created via declarative service or via code. The service appears the same to the service consumers.

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. This is read by the OSGi runtime and the service is created.

For example the reference look like the following.

    
Service-Component: component.xml, OSGI-INF/component.xml

7.2. Define a declarative service

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 Definitioin

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.

7.3. Review the result

Go back to component.xml and select the "Source". The declaration of the service looks like the following.

    








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.ServiceComponent" .

Check now the MANIFEST.MF. Here you find the entry: Service-Component: component.xml, 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.

7.4. Run declarative 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.

0 comments:

Post a Comment