Friday, August 29, 2008

Sling OSGi Track pt 3: replacing the Activator with DS

This is the continuation of

In this part, I will show how to declare the SampleService instead of registering it explicitly by an activator


  • Show how to declare a service component




  • Add the scr plugin to the pom
  • Remove the activator
  • Add scr tags
  • Build, Install, Test


Add the scr plugin to the pom

Add the following snippet to the build plugins:

  1:  <plugin>
  2:      <groupId>org.apache.felix</groupId>
  3:      <artifactId>maven-scr-plugin</artifactId>
  4:      <version>1.0.7</version>
  5:      <executions>
  6:          <execution>
  7:              <id>generate-scr-scrdescriptor</id>
  8:              <goals>
  9:                  <goal>scr</goal>
 10:              </goals>
 11:          </execution>
 12:      </executions>
 13:  </plugin> 
Remove the activator

As we no longer will use explicit activation, we remove the Activator from the sources and remove the following line from the pom:

Add scr tags

Now, we add the scr tags to the implementing class.

Line (4) declares the class as a component. This will provide a wrapped ManagedService Component in the OSGi container (I'll come to ManageServices later)

Line (5) declares the service we are offering. Actually, the interface attribute is superflous, as by default, all implemented Interfaces are used.

  1: package mh.osgitest;
  3: /**
  4:  * @scr.component
  5:  * @scr.service interface="SampleService"
  6:  */
  7: public class SampleServiceImpl implements SampleService {
  8:  public String sayHello() {
  9:   return "hello";
 10:  }
 11: }
Build, Install, Test

First, mvn clean package

To install the bundle, go to the system console at http://localhost:7402/system/console/list.

Then, call the URL http://localhost:7402/test .

For the detail of these two steps, have a look at the first post in this series: /2008/08/basic-osgi-service-for-sling.html

No comments: