Posts Tagged ‘Java’

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.


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);;

    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");

    serviceTracker = null;

Sample JAX-RS Resource class

package sample1;


public class SampleResource2 {

 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.

Categories: Java Tags: , , , , ,

JavaScript Dependency Injection

February 15, 2010 Leave a comment

Some time ago I had this idea: – How to create a JavaScript dependency injection container in Java? The beginning of this idea can be read here.

This idea is redundant if you think that now a days we have lots of JavaScript frameworks or even GWT which helps us to write JavaScript applications in Java. However, this is not I want. I want something complete different, something that help me to have the best of two worlds: Java and JavaScript.

How it works is pretty simple: Inject resources (variables, code, objects etc) into the JavaScript code before sending to the client (browser in this case). The draft version of this application allow us to put i18n in action into JavaScript using Apache Velocity as engine.

You can check the development at Google’s code website here.

I’ll update this blog with examples as soon as the development is ready for real things.


Categories: Java, JavaScript Tags: , , , ,