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

To build the bundle jar file and run the tests, you can just launch
%mvn clean install

After downloading and installing Felix, you can also start the HTTP service manually as shown below. The following steps are known to work with the Felix version 3.0.9. You will need to replace <version> with the current Jersey version, and <repository> with either snapshots or releases based on whether you depend on a snapshot or stable release version of Jersey respectively, in the install commands below.

      
%unzip ~/Downloads/org.apache.felix.main.distribution-3.0.9.zip
%cp bundle/target/bundle*jar felix-framework-3.0.9
%cd felix-framework-3.0.9
%java -jar bin/felix.jar
____________________________
Welcome to Apache Felix Gogo

g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (3.0.9)
    1|Active     |    1|Apache Felix Bundle Repository (1.6.2)
    2|Active     |    1|Apache Felix Gogo Command (0.8.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.8.0)
    4|Active     |    1|Apache Felix Gogo Shell (0.8.0)
g! install http://repo2.maven.org/maven2/org/apache/felix/org.apache.felix.configadmin/1.2.8/org.apache.felix.configadmin-1.2.8.jar
Bundle ID: 5
g! install http://repo1.maven.org/maven2/org/apache/felix/org.apache.felix.eventadmin/1.2.10/org.apache.felix.eventadmin-1.2.10.jar
Bundle ID: 6
g! 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: 7
g! install  https://maven.java.net/service/local/artifact/maven/redirect?r=<repository>&g=com.sun.jersey&a=jersey-core&v=<version>&e=jar
Bundle ID: 8
g! install https://maven.java.net/service/local/artifact/maven/redirect?r=<repository>&g=com.sun.jersey&a=jersey-server&v=<version>&e=jar
Bundle ID: 9
g! install https://maven.java.net/service/local/artifact/maven/redirect?r=<repository>&g=com.sun.jersey&a=jersey-servlet&v=<version>&e=jar
Bundle ID: 10
g! install file:bundle-1.9.jar
Bundle ID: 11 
g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (3.0.9)
    1|Active     |    1|Apache Felix Bundle Repository (1.6.2)
    2|Active     |    1|Apache Felix Gogo Command (0.8.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.8.0)
    4|Active     |    1|Apache Felix Gogo Shell (0.8.0)
    5|Installed  |    1|Apache Felix Configuration Admin Service (1.2.8)
    6|Installed  |    1|Apache Felix EventAdmin (1.2.10)
    7|Installed  |    1|OPS4J Pax Web - Jetty Bundle (0.7.1)   
    8|Installed  |    1|jersey-core (1.9.0)
    9|Installed  |    1|jersey-server (1.9.0)
   10|Installed  |    1|jersey-servlet (1.9.0)
   11|Installed  |    1|OSGi HttpService - Jersey Sample Bundle (1.9.0)
g! start 5 6 7 8 9 10 11
Mar 22, 2011 10:04:30 PM com.sun.jersey.samples.osgihttpservice.Activator start
INFO: STARTING HTTP SERVICE BUNDLE
Mar 22, 2011 10:04:30 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets
INFO: JERSEY BUNDLE: REGISTERING SERVLETS
Mar 22, 2011 10:04:30 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets
INFO: JERSEY BUNDLE: HTTP SERVICE = org.ops4j.pax.web.service.internal.HttpServiceProxy@1d25d06e
Mar 22, 2011 10:04:30 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.9 09/02/2011 12:48 PM'
Mar 22, 2011 10:04:30 PM com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder 
INFO: Instantiated the Application class com.sun.jersey.samples.osgihttpservice.JerseyApplication
Mar 22, 2011 10:04:31 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets
INFO: JERSEY BUNDLE: SERVLETS REGISTERED
Mar 22, 2011 10:04:31 PM com.sun.jersey.samples.osgihttpservice.Activator start
INFO: HTTP SERVICE BUNDLE STARTED
Now the Jersey resource should become available at http://localhost:8080/jersey-http-service/status

Troubleshooting

HTTP Proxy

Behind a HTTP proxy, Apache Felix returns java.net.ConnectException: Connection timed out when installing bundles from the Internet. You will need to setup the following system properties:

These system properties can be set directly on the command line when starting the JVM using the standard "-D<prop>=<value>" syntax or you can put them in the lib/system.properties file of your Felix installation; see documentation on configuring Felix for more information.