Starting and configuring a Jetty 7 instance in Equinox
Starting an embedded Jetty 7 server in Equinox is easy, but getting the right configuration in the right place can be slightly harder.
I am currently using the following Jetty bundles:
org.eclipse.jetty.client
org.eclipse.jetty.continuation
org.eclipse.jetty.deploy
org.eclipse.jetty.http
org.eclipse.jetty.io
org.eclipse.jetty.jmx
org.eclipse.jetty.osgi.boot
org.eclipse.jetty.security
org.eclipse.jetty.server
org.eclipse.jetty.servlet
org.eclipse.jetty.servlets
org.eclipse.jetty.util
org.eclipse.jetty.webapp
org.eclipse.jetty.xml
Some bundles are required only because I want to support gzip loading, jmx introspection and authentication. What bundles can be removed is left as an exercise to the reader.
Jetty is started by the org.eclipse.jetty.osgi.boot
bundle, so I make sure this is auto-started. However, in order for Jetty to become useful I also need to reconfigure it.
When the Jetty boot bundle is activated, it runs the DefaultJettyAtJettyHomeHelper#startJettyAtJettyHome(org.osgi.framework.BundleContext) method and that in turn looks at a few properties to determine where to read the configuration from. We elected to use the jetty.home.bundle
property.
Armed with the information gleaned from those docs, and the instructions on how to inject new server-wide features into Jetty-OSGi on the eclipse wiki, I created a fragment bundle that is loaded and activated with org.eclipse.jetty.osgi.boot
and modifies the jetty.home.bundle
property so the configuration is read from the fragment bundle instead of the one included in the Jetty boot bundle.
To set the property I created a fragment activator (all package names have been changed to protect the innocent):
package jettyconfig;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class FragmentActivator implements BundleActivator {
@Override
public void start(BundleContext context) throws Exception {
// Set jetty.home.bundle to the name of our bundle
System.setProperty("jetty.home.bundle", "jettyconfig");
}
@Override
public void stop(BundleContext context) throws Exception {
}
}
The fragment activator will be executed just before the Jetty boot bundle is activated.
It would also be possible to set this property from the command-line (as is probably more proper), but after some discussion we preferred to do it like this until something better comes along.
The MANIFEST.MF looks like this:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Jetty Configuration
Bundle-SymbolicName: jettyconfig
Bundle-Version: 1.0.0.qualifier
Fragment-Host: org.eclipse.jetty.osgi.boot;bundle-version="7.2.2"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Vendor: Henrik Gustafsson
Finally, I could put my config in /etc/jetty.xml in the fragment bundle, and Jetty is started with this configuration. As a starting point for my jetty.xml I used the jetty-osgi-default.xml file and the notes on the jetty.xml wiki page. In particular I needed a ContextHandlerCollection
(or perhaps a OSGiAppProvider) configured for Jetty to pick up the services properly.
Reader Comments (3)
Hi ,
I have some open questions for which i have dropped a mail to jetty-users@eclipse.org . Could you please help me to solve the issue ?
Best Regards,
Mitul.
Hi All,
I am using fragment bundle approach to start the jetty on given port say 50091 .Jetty is embedded in equinox container . The approach is mentioned in
http://blog.fnord.se/ STARTING AND CONFIGURING A JETTY 7 INSTANCE IN EQUINOX
Is is possible to start jetty on 2nd port say 50092 using the same approach . I tried creating the 2nd bundle say test and configure the jetty.xml their. When i started by application it first calls the FragmentActivator class in jettyConfig bundle and then it calls the FragmentActivator class from test bundle . Here jetty.xml from test bundle is final outcome i.e jetty is running on 50092 port and not on 50091 .
Can you help me on this ?
Best Regards,
Mitul
Hi
I have a JettyBundle that starts a Server which works fine
Server server = new Server(SERVER_PORT);
server.start();
I have another GWT bundle that contains Servlets. The servlet is deployed fine but I can't get hold of an OSGi Context
in the Servlet init method
config.getServletContext().getAttribute("osgi-bundlecontext")
How do you get a reference?
public void init(ServletConfig config)
FrameworkUtil.getBundle(WebClientService.class).getBundleContext();