Archive

Posts Tagged ‘REST’

RESTEasy and OSGi: Perfect match

March 7, 2010 1 comment

I used to develop Eclipse plugins which have been implemented since version 3.0 as OSGi bundles. Also, it is possible to run OSGi bundles inside a web container allowing these bundles to provide services and resources to a web application providing an outstanding modular platform.

As I’m a big fan of jBoss and of jBoss RESTEasy (JAX-RS implementation) I started looking for a solution to integrate these frameworks and build a web application based on REST services and bundles.

How things get together

First of all, you need to run an OSGi framework inside a web container. Today there are many possibilities however, I choose to use the Eclipse Equinox bridge servlet. You can find more about this implementation here.

Second, you need to convert the jBoss RESTEasy libraries and dependencies into OSGi bundles. You can easy create these bundles by creating Eclipse plugins from existent jar archives (Eclipse: File -> new -> Other, Plug-in form existent JAR archives) and follow the wizard to create a bundle adding the RESTEasy jar files.

Third, we need to use my plugin to start the RESTEasy framework inside the web container as an OSGi service.

And them, you start creating your JAX-RS resources as bundles.

Example

Create a new Eclipse Plug-in project (Tip: In the wizard select an OSGi framework as Standard). You can download an example plugin here.

Sample Activator class

package sample1;

import org.jboss.resteasy.plugins.server.osgi.IResteasyService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

public class Activator implements BundleActivator {

 private ServiceTracker serviceTracker;
 private IResteasyService service;

 public void start(BundleContext context) throws Exception {

    serviceTracker = new ServiceTracker(context, IResteasyService.SERVICE_NAME, null);
    serviceTracker.open();

    service = (IResteasyService) serviceTracker.getService();

    if(service != null) {
       System.out.println("Registering singleton");
       service.addSingletonResource(new SampleResource());
       System.out.println(SampleResource.class.getName() + " registered");
    }    
 }

 public void stop(BundleContext context) throws Exception {

    System.out.println("Removing singleton");
    service.removeSingletonResource(SampleResource.class);

    serviceTracker.close();
    serviceTracker = null;
    System.out.println("Done");
 }
}

Sample JAX-RS Resource class

package sample1;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;

@Path("/sample2")
public class SampleResource2 {

 @GET
 @Path("hello")
 public String sayHello() throws WebApplicationException {
    return "Hello from OSGi";
 }
}

If you start this bundle your will be able to access the url http://localhost:8080/osgi/rest/sample2/hello, and if you stop this bundle the path /sample2/hello will be no longer available for client requests. In this implementation the path /osgi is the application context and /rest is the RESTEasy mapping prefix from the web.xml file.

Download here a complete WAR file to deploy this solution and test yourself the power of RESTEasy and OSGi together.

Download here the experimental code develop to connect the RESTEasy and OSGi.

Advertisements
Categories: Java Tags: , , , , ,