OSGi HttpService Example

This example demonstrates how to develop a simple OSGi bundle registering a Jersey based RESTful web service as an OSGi HttpService

Contents

The example bundle (see the bundle module) consists of just one Jersey resource:

com.sun.jersey.samples.osgihttpservice.StatusResource
that produces a textual response to an HTTP GET

The mapping of the URI path space is presented in the following table:

URI path Resource class HTTP methods
/status StatusResource GET

Running the Example

To run the example, you would need to build the OSGi bundle in the bundle module and install it to an OSGi runtime (e.g. Apache Felix) together with other OSGi bundles. Look at the attached functional-test module for details on the programmatical runtime configuration

After downloading and installing Felix, you can also start the HTTP service manually as shown bellow. The following steps are known to work with the Felix version 2.0.4:

      
%java -jar bin/felix.jar

Welcome to Felix
================

-> ps
START LEVEL 1
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (2.0.4)
[   1] [Active     ] [    1] Apache Felix Bundle Repository (1.4.3)
[   2] [Active     ] [    1] Apache Felix Shell Service (1.4.2)
[   3] [Active     ] [    1] Apache Felix Shell TUI (1.4.1)
-> install ftp://www.ibiblio.org/pub/mirrors/apache/felix/org.apache.felix.configadmin-1.2.4.jar
Bundle ID: 4
-> install ftp://www.ibiblio.org/pub/mirrors/apache/felix/org.apache.felix.eventadmin-1.2.2.jar
Bundle ID: 5
-> install http://repo2.maven.org/maven2/org/ops4j/pax/web/pax-web-jetty-bundle/0.7.1/pax-web-jetty-bundle-0.7.1.jar
Bundle ID: 6
-> install http://download.java.net/maven/2/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar
Bundle ID: 7
-> install http://download.java.net/maven/2/com/sun/jersey/jersey-core/1.3-SNAPSHOT/jersey-core-1.3-SNAPSHOT.jar
Bundle ID: 8
-> install http://download.java.net/maven/2/com/sun/jersey/jersey-server/1.3-SNAPSHOT/jersey-server-1.3-SNAPSHOT.jar
Bundle ID: 9
-> install file:bundle-1.3-SNAPSHOT.jar
Bundle ID: 10
-> ps
START LEVEL 1
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (2.0.4)
[   1] [Active     ] [    1] Apache Felix Bundle Repository (1.4.3)
[   2] [Active     ] [    1] Apache Felix Shell Service (1.4.2)
[   3] [Active     ] [    1] Apache Felix Shell TUI (1.4.1)
[   4] [Resolved   ] [    1] Apache Felix Configuration Admin Service (1.2.4)
[   5] [Resolved   ] [    1] Apache Felix EventAdmin (1.2.2)
[   6] [Resolved   ] [    1] OPS4J Pax Web - Jetty Bundle (0.7.1)
[   7] [Resolved   ] [    1] jsr311-api (1.1.1)
[   8] [Resolved   ] [    1] jersey-core (1.2.0.SNAPSHOT)
[   9] [Resolved   ] [    1] jersey-server (1.2.0.SNAPSHOT)
[  10] [Resolved   ] [    1] OSGi HttpService - Jersey Sample Bundle (1.2.0.SNAPSHOT)
-> start 4 5 6 7 8 9 10
DEBUG: EventAdmin: org.apache.felix.eventadmin.CacheSize=30
DEBUG: EventAdmin: org.apache.felix.eventadmin.ThreadPoolSize=20
DEBUG: EventAdmin: org.apache.felix.eventadmin.Timeout=5000
DEBUG: EventAdmin: org.apache.felix.eventadmin.RequireTopic=true
DEBUG: EventAdmin: org.apache.felix.eventadmin.CacheSize=30
DEBUG: EventAdmin: org.apache.felix.eventadmin.ThreadPoolSize=20
DEBUG: EventAdmin: org.apache.felix.eventadmin.Timeout=5000
DEBUG: EventAdmin: org.apache.felix.eventadmin.RequireTopic=true
Apr 20, 2010 12:37:48 PM com.sun.jersey.samples.osgihttpservice.Activator start
INFO: STARTING HTTP SERVICE BUNDLE
Apr 20, 2010 12:37:48 PM com.sun.jersey.samples.osgihttpservice.Activator start
INFO: HTTP SERVICE BUNDLE STARTED
-> Apr 20, 2010 12:37:48 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets
INFO: JERSEY BUNDLE: REGISTERING SERVLETS
Apr 20, 2010 12:37:48 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets
INFO: JERSEY BUNDLE: HTTP SERVICE = org.ops4j.pax.web.service.internal.HttpServiceProxy@7114460
Apr 20, 2010 12:37:48 PM com.sun.jersey.server.impl.application.WebApplicationImpl initiate
INFO: Initiating Jersey application, version 'Jersey: 1.3-SNAPSHOT 04/28/2010 05:49 PM'
Apr 20, 2010 12:37:48 PM com.sun.jersey.server.impl.application.DeferredResourceConfig getApplication
INFO: Instantiating the Application class, named com.sun.jersey.samples.osgihttpservice.JerseyApplication
Apr 20, 2010 12:37:49 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets
INFO: JERSEY BUNDLE: SERVLETS REGISTERED
Now the Jersey resource should become available at http://localhost:8080/jersey-http-service/status