Register Service Demo

The Register Service demo is a simple bundle which registers a Time Service in the OSGi framework. The Time Service provides the current system time.

Demo Components

The Register Service demo bundle is packed in the regservice.jar in the demo/bundles directory of the Framework Professional Edition Package .

The Java source files of the demo are in the The Java source files of the demo are in the demo/framework/basics/regservice directory.

The mkrs.bat recompiles and repacks the demo. Use this file if you have changed something in the source files of the demo.

Starting the Demo

Using an Install Script

The Register Service demo is included in the demo/framework/basics/install.txt install script. For more information about installing demos via install scripts, refer to General Rules for Demos.

Activating All Necessary Components

To start the Register Service demo:

  1. Start the OSGi framework or make sure it is on.
  2. Install and start the Register Service demo using its JAR regservice.jar in demo/bundles.

Inside the Demo

The Register Service demo consists of the following components:

The TimeService abstract class represents the Time service to the other bundles running in the OSGi framework. This class has only one method - getCurrentTime, which returns the current system time as a String.

The TimeServiceSimple extends TimeService and overrides the getCurrentTime method. It calls the toString method of TimeService passing java.util.GregorianCalendar as argument. toString represents the current time in the following format <day_of_week>, <dd> <mm> <yyyy> <hh>:<mm>:<ss> <time_zone>.

public class TimeServiceSimple extends TimeService {
  GregorianCalendar calendar;
  public TimeServiceSimple() {
    calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
  }
         
  public String getCurrentTime() {
    return toString(calendar);
  }
}
Listing 1: Service implementation.

When the bundle is started, the start method of the bundle activator is called. The Register Service bundle registers the Time Service under the TimeService abstract class with an implementation object TimeServiceSimple and a service property TimeService.TIME_ZONE equal to "GMT".

    Hashtable props = new Hashtable();
    props.put(TimeService.TIME_ZONE, "GMT");
    timeServReg = bc.registerService(TimeService.class.getName(), 
new TimeServiceSimple(),
props);
Listing 2: Registering a service

Basic Demos