OSGi Tips

As of release 6.1.5, Jetty ships with jar file manifests that include appropriate OSGi bundle information. This means that you can import the jetty jars into an OSGi framework and use them, for example, to build a HTTP service.

The jetty jars whose manifests include OSGi bundle information are:

servlet-api-2.5.jar

jetty-util.jar

jetty.jar

jetty-management.jar

jetty-naming.jar

jetty-plus.jar

jetty-annotations.jar

jsp-api-2.1.jar

jsp-2.1.jar

Using jetty jars in Equinox

The jetty bundles mostly have dependencies amongst themselves (loosely in the order listed above), although the jetty-naming.jar and the the jsp jars do have external dependencies.

If you are using Equinox, it is quite easy to resolve these dependencies. Here's a pre-baked Equinox config.ini file with all the external dependencies already setup up so that they will be loaded at start up time. Assuming you downloaded the config.ini file to /tmp, here's a quick way to start up Equinox 3.3 with this config.ini file:

You can now go ahead and import the jetty jars. This is a matter of using the install command to load each of them. Assuming I have jetty 6.1.5 installed in /home/janb/src/jetty-6.1.5, I install the servlet-api-2.5 jar and check on it's status by doing:

The final step is to start that jar so that it is available to the OSGi environment:

osgi> start 21

Rinse and repeat for all the jars we listed at the top of this article and you will have successfully imported and made available the jetty jars in OSGi!

Note: if you get unresolved constraints on bundles that appear to be provided by the bundle itself, you can safely ignore the error on the 'diag' step. This is the case for servlet-api.jar and jetty.jar.

OSGi dependency resolution, and a closer look at the Equinox config.ini file

As we said before, jetty is mostly self-contained. There are a few jars (aka bundles in OSGi-speak) which do, however, have external dependencies and these must be resolved before you will be able to fully use jetty. If you are using the supplied config.ini file, then this is already solved for you. However, if you are using a different OSGi framework, or you'd like to understand a bit more about dependency resolution, then read on.

The jars and their external package dependencies are:

jetty-naming.jar

javax.mail

jsp-api-2.1.jar

com.sun.org.apache.xalan.internal.res,

com.sun.org.apache.xml.internal.utils,

com.sun.org.apache.xpath.internal,

com.sun.org.apache.xpath.internal.jaxp,

com.sun.org.apache.xpath.internal.objects,

javax.naming,

javax.sql,

javax.xml.namespace,

javax.xml.parsers,

javax.xml.transform,

javax.xml.transform.dom,

javax.xml.transform.sax,

javax.xml.transform.stream,

javax.xml.xpath,

org.w3c.dom,

org.w3c.dom.traversal,

org.xml.sax,

org.xml.sax.helpers

jsp-2.1.jar

javax.el,

javax.xml.parsers,

javax.xml.transform,

javax.xml.transform.dom,

javax.xml.transform.stream,

javax.xml.validation,

org.apache.tools.ant,

org.apache.tools.ant.taskdefs,

org.apache.tools.ant.types,

org.eclipse.jdt.core.compiler,

org.eclipse.jdt.internal.compiler,

org.eclipse.jdt.internal.compiler.classfmt,

org.eclipse.jdt.internal.compiler.env,

org.eclipse.jdt.internal.compiler.problem,

org.w3c.dom,

org.w3c.dom.ls,

org.xml.sax,

org.xml.sax.ext,

org.xml.sax.helpers,

sun.misc

All of these, except for javax.mail, org.apache.tools.ant.andorg.eclipse.jdt. can all be found in the jvm's rt.jar. In Equinox, by default, these system packages from the system classloader are not exposed inside the OSGi framework. So, we need to expose them by using this setup in the config.ini:

To resolve the javax.mail, org.apache.tools.ant.andorg.eclipse.jdt. packages - which equate to the mail.jar, ant.jar and the core.jar from the jetty distribution - we need to use these Eclipse plugins (note, this page was created based on Eclipse 3.3, so the plugin versions may change depending on your Eclipse version):

These plugins have their own dependencies which need to be resolved, and so we end up with this list of bundle dependencies, which we instruct Eclipse to pre-load for us, rather than manually installing and resolving each one: