Tutorial OSGi

Creation d'un service dictionnaire

Nous allons créer un service qui nous permette de savoir si un mot est écrit correctement. La première étape consiste à écrire l'interface du service:

/*
* OSGi and Gravity Service Binder tutorial.
* Copyright (c) 2003 Richard S. Hall
* http://oscar-osgi.sourceforge.net
**/


package tutorial.example2.service;

/**
* A simple service interface that defines a dictionary service.
* A dictionary service simply verifies the existence of a word.
**/

public interface DictionaryService
{

/**
* Check for the existence of a word.
* @param word the word to be checked.
* @return true if the word is in the dictionary,
* false otherwise.
**/

public boolean checkWord(String word);
}

La deuxième étape consiste à créer un composant qui implémente ce service et qui l'enregistre au moment du démarrage du bundle. L'implémentation du service est réalisée par une classe interne, mais elle pourrait également se trouver en dehors de la classe Activator.

Il est important de noter que le service et son implémentation ne se trouvent pas dans le même package. La raison de ceci est que nous avons besoin d'exporter le package dans lequel le service est défini pour que d'autres bundles puissent y acceder, mais nous ne voulons pas rendre l'implémentation publique.

/*
* OSGi and Gravity Service Binder tutorial.
* Copyright (c) 2003 Richard S. Hall
* http://oscar-osgi.sourceforge.net
**/

package tutorial.example2;

import java.util.Properties;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceEvent;

import tutorial.example2.service.DictionaryService;

/**
* This class implements a simple bundle that uses the bundle
* context to register an English language dictionary service
* with the OSGi framework. The dictionary service interface is
* defined in a separate class file and is implemented by an
* inner class.
**/

public class Activator implements BundleActivator
{

/**
* Implements BundleActivator.start(). Registers an
* instance of a dictionary service using the bundle context;
* attaches properties to the service that can be queried
* when performing a service look-up.
* @param context the framework context for the bundle.
**/

public void start(BundleContext context)
{
Properties props = new Properties();
props.put("Language", "English");
context.registerService(
DictionaryService.class.getName(), new DictionaryImpl(), props);
}

/**
* Implements BundleActivator.stop(). Does nothing since
* the framework will automatically unregister any registered services.
* @param context the framework context for the bundle.
**/

public void stop(BundleContext context)
{

// NOTE: The service is automatically unregistered.
}

/**
* A private inner class that implements a dictionary service;
* see DictionaryService for details of the service.
**/

private static class DictionaryImpl implements DictionaryService
{

// The set of words contained in the dictionary.
String[] m_dictionary =
{ "welcome", "to", "the", "osgi", "tutorial" };

/**
* Implements DictionaryService.checkWord(). Determines
* if the passed in word is contained in the dictionary.
* @param word the word to be checked.
* @return true if the word is in the dictionary,
* false otherwise.
**/

public boolean checkWord(String word)
{
word = word.toLowerCase();

// This is very inefficient

for (int i = 0; i < m_dictionary.length; i++)
{
if (m_dictionary[i].equals(word))
{
return true;
}
}
return false;
}
}
}

Dans le fichier manifeste, nous devons spécifier que notre bundle exporte le package du service dictionnaire. Nous indiquons aussi que le bundle exporte le service DictionaryService, mais ceci n'est que pour information.

Bundle-Activator: tutorial.example2.Activator
Export-Package: tutorial.example2.service
Export-Service: tuorial.example2.service.DictionaryService
Bundle-Name: English dictionary
Bundle-Description: A bundle that registers an English dictionary service
Bundle-Vendor: Richard Hall
Bundle-Version: 1.0.0

Il faut maintenant suivre une séquence similaire à celle utilisée pour le premier bundle: compiler puis packager. Voici le fichier ant pour réaliser ceci: build_example2.xml

La cible example2.bundle donne lieu à un fichier example2.jar qui peut maintenant être installé dans le framework:


-> install file:/chemin_du_projet/bundle/example2.jar

-> ps

[  0] [Active     ] System Bundle
[  1] [Active     ] Oscar Shell Service
[  2] [Active     ] Shell TUI
[  3] [Active     ] Oscar Bundle Repository
[  4] [Active     ] Service listener example
[  5] [Installed  ] English Dictionary

-> start 5

Ex1: Service of type tutorial.example2.service.DictionaryService registered.

-> info 5 DictionaryService
 
Bundle 5, Service 0
------------------------
service.id = 20
Language = English
objectClass = tutorial.example2.service.DictionaryService

->

>

Le message qui apparait après le démarrage du bundle 5 provient du bundle que nous avons crée auparavant et qui écoute les évènements de service du framework.

En regardant les détails du service, nous retrouvons la propriété Language = English qui avait été utilisée lors de l'enregistrement du service.

A Faire: Un autre bundle, example2b qui sera une implémentation du service dictionnaire pour la langue française.

Retour au sommaire


Last update: 22 February 2003
contact: Humberto.Cervantes@imag.fr
Homepage