Steps for Building Declarative Services
Revision as of 23:12, 24 January 2011 by Jordan.anastasiade (talk | contribs)
Main Page · Course Description · Course Topics · Schedule, Students, Teams · Course Resources · Course Projects
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();
}
}
3. Define the Service Descriptor
(the component.xml file)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"/>
<service>
<provide interface="cs.ecl.osgi.simple.declarativeservice.say.Sayable"/>
</service>
</component>
3. Define the Bundle Consumer Class
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) {
System.out.println(s.say());
} else {
ci.println("Error, No Service of type Sayable available");
}
}
@Override
public String getHelp() {
return "\n\t run - EXECUTE the Sayable service";
}
}