Hello, OSGi, Part 3: Take it to the server side

OSGi Web application development with Server-Side Equinox

The first two articles in the Hello, OSGi series have laid the groundwork for understanding how the OSGi service platform works. In this final installment, you'll learn how OSGi's pluggable, component-based development model applies to Web applications. You'll build a bundles-based OSGi application that can be used to serve static resources, servlets, or JSPs. You'll also gain hands-on experience using the Equinox framework to easily leverage OSGi's modularity, versioning, and dynamic services in your Web applications.

The first article is this series, "Hello, OSGi, Part 1: Bundles for beginners," introduced OSGi development concepts and showed you how to build a simple Hello World application using Equinox, Eclipse's OSGi container implementation. "Hello, OSGi, Part 2: Spring Dynamic Modules" introduced Spring DM, which makes it very easy to build Spring applications that can be deployed in an OSGi container. This final article in the series tackles the question being asked by many developers right now: I like the concept of OSGi, but how do I use it to build Web applications?

As you'll learn in this article, OSGi's development model greatly simplifies the process of writing truly pluggable, modular, and componentized server-side applications. Development exercises will be based (as they have been throughout this series) on the OSGi container reference implementation, Equinox. This time, however, we'll be using Equinox on the server side. We'll begin by discussing the options for deploying your OSGi Web application (in this case Jetty or Tomcat), followed by a short tutorial in setting up your Web application development environment. You will then develop a simple Hello World OSGi Web app and experiment with the different ways to deploy it.

Application deployment using Server-Side Equinox

Equinox rising
Although the Server-Side Equinox project is in the early stages of development, it is quickly growing. There is a move to add support for popular MVC frameworks such as Apache Struts and the Spring Web framework, along with popular object-relational mapping tools such as Hibernate. The Rich Ajax Platform is another Eclipse project that allows you to use Ajax applications with server-side Equinox.

There are two ways to deploy OSGi Web applications using Server-Side Equinox: You can embed a lightweight servlet container such as Jetty inside your OSGi container, or you can embed your OSGi container inside a Web application, and then deploy that application in a servlet container such as Apache Tomcat.

Now, here's the good news: the process of developing an OSGi Web application is the same no matter which approach you choose. Thus, it's best to embed the servlet container inside your OSGi container during the development process; once your application is ready, you can re-package and deploy it if you wish. You'll learn how to embed a servlet container inside an OSGi container in just a moment. First we need to set up your development environment.

Setting up the development environment

For this article's application development exercise you will wrap a lightweight servlet container inside an OSGi bundle (or plug-in, in the Eclipse context). Once this plug-in is installed in your OSGi container, it will listen for HTTP requests. Upon receiving a request it will check to see if any of the installed plug-ins can handle it and, if so, will then forward control to the appropriate plug-in to generate a response. Once the response has been generated, the OSGi container will forward it to the client.

Server-Side Equinox simplifies OSGi-based Web application development by providing a couple of readymade plug-ins that wrap a servlet container inside an OSGi plug-in. Each of the following plug-ins has an embedded servlet container:

  • org.eclipse.equinox.http: A plug-in with a very small footprint, suitable for resource-constrained environments.
  • org.eclipse.equinox.http.jetty: Uses Jetty as the underlying engine for providing Servlet API 2.4 support.

In this section, you will set up your OSGi Web application development environment by embedding the Jetty plug-in inside your Eclipse IDE. (You should start up your IDE now if you haven't already.)

  1. Download jettyhttp-anon.psf onto your desktop.
  2. Right-click Package Explorer and choose Import > Team > Team Project Set. Then click Next.
  3. On the Import a Team Project Set dialog, click Browse and select the jettyhttp-anon.psf file that you downloaded to your desktop, as shown in Figure 1.

    Importing the plug-in.
    Figure 1. Importing the plug-in (click to enlarge)
    Click Finish to start the import process. This step may take a few minutes, depending on your Internet connection, because Eclipse will try to download the necessary project files from the dev.eclipse.org CVS repository.
  4. Once the import is complete, you should see that the following bundles have been imported into your workspace:
    • javax.servlet v2_4
    • org.apache.commons.logging v1_0_4
    • org.eclipse.equinox.http.jetty
    • org.eclipse.equinox.http.registry
    • org.eclipse.equinox.http.servlet
    • org.mortbay.jetty v5_1_11

Your development environment is ready to go. The next step is to execute your bundles in the Equinox OSGi Framework. Revisit the first part of this series if you don't remember how to do that.

1 2 3 4 5 Page 1
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.