Difference between revisions of "Steps for Building Declarative Services"

From CDOT Wiki
Jump to: navigation, search
(Created page with '{{Ecl_menu}}__NOTOC__ <h4>1. Study the Interfaces</h4> * [http://www.osgi.org/javadoc/r4v42/org/osgi/framework/Bundle.html Bundle] * [http://www.osgi.org/javadoc/r4v42/org/osgi…')
Line 3: Line 3:
<h4>1. Study the Interfaces</h4>
<h4>1. Study the Interfaces</h4>
* [http://www.osgi.org/javadoc/r4v42/org/osgi/framework/Bundle.html Bundle]
* [http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/osgi/framework/console/package-tree.html Command Interpreter]
* [http://www.osgi.org/javadoc/r4v42/org/osgi/framework/BundleContext.html BundleContext]
* [http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/osgi/framework/console/package-tree.html Command Provider]
* [http://www.osgi.org/javadoc/r4v42/org/osgi/framework/BundleActivator.html BundleActivator]
<h4>2. Define the Bundle Service Interface</h4>
<h4>2. Define the Bundle Service Interface</h4>

Revision as of 00:08, 25 January 2011

1. Study the Interfaces

2. Define the Bundle Service Interface

package cs.ecl.osgi.simple.declarativeservice.say;

public interface Sayable {
	String say();

An implementation of this interface could be:

package cs.ecl.osgi.simple.declarativeservice.say.internals;
import java.util.Date;
import cs.ecl.osgi.simple.declarativeservice.say.Sayable;

public class TodaySay implements Sayable {
	public String say() {
		return " Declarative Service: Today is " + new Date();

The relationships between the interface that is exposed to the client and the implementation that is hidden, must be defined in a xml file:

<?xml version="1.0"?>
<component name="sayable">
	<implementation class="cs.ecl.osgi.simple.declarativeservice.say.internals.TodaySay"/>
		<provide interface="cs.ecl.osgi.simple.declarativeservice.say.Sayable"/>

The bundle consumer must implement the CommandProvider interface

package cs.ecl.osgi.simple.declarativeservice.consumer;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;

import cs.ecl.osgi.simple.declarativeservice.say.Sayable;

public class SaySingleConsumer implements CommandProvider {

	private Sayable s;

	public synchronized void bindSayable(Sayable s) {
		this.s = s;

	public synchronized void unbindSayable(Sayable s) {
		this.s = null;

	public synchronized void _run(CommandInterpreter ci) {
		if (s != null) {
		} else {
			ci.println("Error, No Service of type Sayable available");

	public String getHelp() {
		return "\n\t run - EXECUTE the Sayable service";