Friday, January 28, 2011

Configure embedded Jetty in OSGi enviroment

As mentioned earlier, recently was working on using embedded Jetty server in an OSGi environment. Though the information is available on web, but quite scattered. Especially how to configure or debug jetty. So, below is the collated information:
Once you have the up and running servlet as per the steps at:
http://bryanhunt.wordpress.com/2010/05/14/osgi-as-a-web-application-server/
You can do away with auto start of bundles in the launch configuration, use the following code in the activator of your bundle
Bundle bundle = Platform.getBundle("org.eclipse.equinox.http.registry");
if (bundle.getState() == Bundle.RESOLVED) {
 bundle.start(Bundle.START_TRANSIENT);
} 

To make sure your bundle auto starts, implement the startup extension. In your plugin.xml put this:  

    <extension point="org.eclipse.ui.startup">  </extension>

Configure Jetty:  
Now, many times you will not have the option to change the config.ini to provide the port, or you want to configure the Jetty from within your application, there you will need to use the JettyConfigurator class. Use the following code in the activator of your bundle
Dictionary settings = new Hashtable();
settings.put("http.enabled", Boolean.TRUE);
settings.put("http.port", 8080);
settings.put("http.host", "0.0.0.0");
settings.put("https.enabled", Boolean.FALSE);
try {
     JettyConfigurator.startServer("my.jetty", settings);
} catch (Exception e) {
   e.printStackTrace();
} 
You can configure many other settings using the JettyConfigurator as mentioned above. 

No comments:

Post a Comment