My Stuff

2010 Conferences

OSGi DevCon @ JAX London

February 23 - Keynote titled OSGi in the Enterprise: Agility, Modularity, and Architecture’s Paradox

EclipseCon

March 22 - 25 - Tutorial on Modular Architecture

Über Conf

June 14 - 17 - Sessions titled Turtles and Architecture and Patterns of Modular Architecture

Catalyst

July 26 - 30 - Two sessions on rich mobile applications and one on agile development. Half day tutorial on software process improvement.

Tweets @ Twitter

re: #apple event "We sold more iPads than any PC manufacturer sold of their entire PC line." 2012-09-12

re: #Apple Event ""Our notebooks now rank #1 in the US in Market share in the last three months." 2012-09-12

Right on. I just won a Best Buy drawing worth $1000. Either that or I won a shiny new virus by clicking the link. Hmm...what to do. 2012-08-29

The #osgi alliance response (http://t.co/KrN8XNWg) to the @mreinhold #jigsaw announcement (http://t.co/9YvcDdqC). 2012-08-29

Good Q&A with @mreinhold on project #jigsaw. http://t.co/9YvcDdqC. Modularity will change the way we design and run apps! 2012-08-28

LinkedIn Profile

The opinions expressed on this site are my own, and not necessarily those of my employer.

OSGi and Embedded Jetty

Filed Under Architecture & Design, Development, Java, OSGi, Platforms |  

There are two approaches to working with OSGi when developing web applications.

  • Embedding OSGi into the application server
  • Embedding the application server into an OSGi runtime

In this post, I’m going to embed Jetty, the application server, into Felix, the OSGi runtime. As with my previous posts (Simple OSGi Service, OSGi & Modularity, and OSGi & Spring), I’m trying to use the simplest tools for the job to maximize the experience. That means Ant, Felix, Subversion, a simple text editor, and a terminal window. The code can be found in my Google code repository. Look for the project called HelloWorldWebJSP.

As the project name suggests, not only am I going to embed Jetty in Felix, I’m also going to make sure I can serve JSP pages, which requires a bit of extra trickery. If you need additional details, leave a comment or contact me. Let’s go!

Get Felix

I used Felix 1.4.1 for this exercise. I also tested it with 1.4.0, so either of those two versions should be fine. If you don’t have Felix, get it.

Checkout HelloWorldWebJSP

Next thing to do is to checkout the project from the google code repository. If you want to use the scripts included with the project to startup Felix, you should checkout the project in a directory right alongside Felix. To checkout the project, you’ll need subversion. Then, navigate to the directory containing Felix and do the following:

svn checkout
http://kcode.googlecode.com/svn/trunk/osgi/HelloWorldWebJSP

For convenience, I checked in the Felix bundle cache, so you don’t need to install all the necessary bundles. The environment is already configured. However, if you’d like to setup the environment yourself, you should be able to delete the felix-cache directory, and after starting Felix, go through the steps of installing the necessary bundles. I’d wait to do this until after you’ve had a chance to step through the rest of the example. But it’s up to you. If something gets messed up, you can always start over with a clean checkout. The bundles you’ll need to install can be found in the /web/lib directory. They are

jetty-6.1.7.jar
jetty-util-6.1.7.jar
pax-web-service-0.5.1.jar
osgi.cmpn.jar
pax-web-jsp-0.5.1.jar

Start Felix

Next step is to start Felix. Open a terminal window and navigate to the HelloWorldWebJSP directory. You can use the pre-written scripts (assuming your directory structure is correct), or you can invoke it from command-line like the following, replacing the path to felix with the location of felix:

java -Dfelix.config.properties=file:./config.properties
-jar path to felix/felix-1.4.1/bin/felix.jar

Note that you should use the config.properties included with the project, and not the default Felix config.properties. This is because we need to specify an additional package import to make things work (javax.security.cert). Also, if the HelloWorldWebJSP and Felix directories aren’t side-by-side, you’ll have to change the file paths to the Felix bundles in config.properties.

Try it out

At this point, Jetty should be running inside Felix, and you can navigate to the homepage in your browser. To do this, simply point your browser to http://localhost:8380/. Click hello. Click goodbye. To see the JSP, click the link that serves a JSP.

Explore the Configuration

There isn’t a lot of magic here, but it did take some time to find all the bundles necessary to make it happen, especially to get JSP pages working. All in all, there are a total of 10 bundles, and that includes the four Felix bundles, and the bundle we developed as part of the web application (web.jar in ./web/bin directory). To see a list of all the bundles installed, type the following in the terminal window.

ps

Of particular interest, you’ll see two PAX Web bundles. The OPS4J Pax Web - Service (0.5.1) is the bundle that provides Jetty integration (and actually starts Jetty), while OPS4J Pax Web - Jsp Support (0.5.1) provides JSP compilation via Jasper. Note that most of the examples on the PAX Web website use Maven. Since I didn’t want to use Maven, I installed these bundles manually through the Felix console.

Explore the Code

The key element of the web application is the Activator. This registers the servlet, html pages, and JSP page so that Jetty can serve them up. This is where PAX Web makes the job much easier, because it encapsulates integration with Jasper.

Further Experimentation

There are a few interesting things you can do to experiment a bit further.

First, try setting up the environment yourself. Doing this isn’t that difficult since all of the bundles are included with the project when you checked it out. To do this, close down the existing Felix instance by typing the following in the terminal window.

shutdown

Next, delete the felix-cache directory. This gives you a clean Felix instance with none of the bundles installed. Startup Felix and then, one-by-one, install the appropriate bundles (listed above) from within felix using the install command, as follows:

install file:./path to bundle

Note that if HelloWorldWebJSP and Felix aren’t sitting side-by-side in the file system, you’ll need to change config.properties to point to the location of the Felix bundles. They are found in the bundle directory under felix. Also, don’t forget to install web.jar from the bin directory (you may have to run the build script), since that’s the JAR that contains our application classes. Then, start the bundles using the following command at the Felix command line in the terminal, where the bundle id is the id of the bundle obtained using the ps command.

start bundle id

Second, make a change to the JSP, html, or servlet. There is an Ant build script in the web directory that allows you to recompile. Depending on what you changed or added, you may have to modify the build script to make sure any new files are included in the JAR file created. Once compiled, redeploy the web.jar to Felix from within the console. You shouldn’t need to restart Felix when doing this. Just do the following, where bundle id is the id of the Hello World Web bundle (ie. the web application):

stop bundle id
update bundle id
refresh bundle id
start bundle id

Third, try deploying another servlet after registering it in the Activator. Or create and deploy another JSP. Or Add some images and additional resources. You can modify the build script to include them in the web.jar that’s already been deployed to Felix, or you can create your own JAR file and deploy it alongside web.jar. You can deploy these changes without interrupting what’s already running within Felix, providing a great feel for the flexibility of OSGi and the adaptable environments possible. Whatever you do, this base install should give you a foundation for experimenting with web applications using OSGi.

Soon I hope, I’ll be experimenting with the alternative configuration - embedding OSGi into the application server.

Update: While the example above uses Felix, I have also tested this example with Equinox. You can follow the same steps and the result is the same. Except the default port for the Equinox solution is 8080, and if you want that changed, you’ll have to do it yourself. Also, when using Equinox, replace the ps command with ss to see the bundle listing.

Comments

7 Responses to “OSGi and Embedded Jetty”

  1. Alin Dreghiciu on February 16th, 2009 2:58 pm

    Hi,

    Just a small hint.
    If you are using the pax-web-service bundle you do not need the jetty bundles as jetty is embedded in the pax web service bundle. You only need the two bundles from jetty if you want to use a different version of jetty then the one that is embedded in pax web service. And in that case you will have to use the pax-web-bundle instead of pax-web-service, because that one does not embed jetty.

    BTW, I just released Pax Web 0.5.2.

    HTH

  2. kirk on February 16th, 2009 3:14 pm

    Thanx for the tip. Does even more for me than I realized.

  3. Embedding OSGi in Tomcat : Software & Technology @kirkk.com on February 16th, 2009 4:06 pm

    [...] all you’ll need for this example is Tomcat, Ant, and Subversion. If you went through the previous example, you’ve already got Ant and Subversion installed. If you already have Tomcat, you should be [...]

  4. OSGi Post Summary : Software & Technology @kirkk.com on February 19th, 2009 8:41 pm

    [...] OSGi & Embedded Jetty - A simple tutorial showing how Jetty can be embedded in OSGi. The example is fully functional and can be checked out of my Google Code SVN repository. I used Felix for the example, but also tested it under Equinox. [...]

  5. OSGi Discontent - No Migration Path! : Software & Technology @kirkk.com on March 25th, 2009 4:01 pm

    [...] Host OSGi - With this approach, OSGi is the host environment, and the advantages of modularity are brought to the entire application platform. For an example of this approach, see my post where I embed Jetty within Felix. [...]

  6. Supratim on December 21st, 2010 6:07 am

    Hi Kirk,

    I am trying to embed Tomcat in Felix.My IDE is eclipse Europa.I am new to OSGI.Any help will be highly appreciated.

  7. Building modular and extensible java applications with OSGi | Nikos Web Development and Design on December 4th, 2011 12:51 pm

    [...] Spring Dynamic Modules from the JavaWorld Site to get a grasp. Also you can check these posts about setting up OSGi with Jetty and setting up OSGi with Tomcat. The documentation of OSGi with Open-Source OSGi container [...]

Leave a Reply