This is the continuation of
- Sling OSGi Track pt 1: hand-rolled service bundle and
- Sling OSGi Track pt 2: using Felix' bundle plugin for manifest entries
In this part, I will show how to declare the SampleService instead of registering it explicitly by an activator
Aims:
- Show how to declare a service component
Ingredients:
Files:
- https://mhavelock-blog.s3.amazonaws.com/mh.studies.sling.osgitest-0.0.3.jar
- https://mhavelock-blog.s3.amazonaws.com/mh.studies.sling.osgitest-0-0-3-src.zip
Outline:
- Add the scr plugin to the pom
- Remove the activator
- Add scr tags
- Build, Install, Test
Execution:
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:
<Bundle-Activator>mh.osgitest.Activator</Bundle-Activator>
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;
2:3: /**
4: * @scr.component5: * @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:
Post a Comment