• Share this article:

Writing a bundle-based server application

Thursday, February 28, 2008 - 12:40 by Wayne Beaton

Building server-based applications with Equinox is getting easier. The Equinox project’s server team has created a short tutorial that describes how to get a servlet up and running using a handful of bundles on the Equinox framework. The best part is that everything you need to run the tutorial is already included in the Ganymede M5 release of Eclipse for RCP/Plug-in Developers (previously, you had to load a bunch of prerequisite bundles). The tutorial is a little short on details, so I thought I’d take a stab at expanding on them a bit.

The first step (after downloading and installing Eclipse for RCP/Plug-in Developers) is to create a new Plug-in Project (File > New > Project > Plug-in Project). Next, navigate to the “Dependencies” page in the new plug-in’s Manifest Editor and add the org.eclipse.equinox.http.registry and javax.servlet bundles as “Required Plug-ins”. Next, navigate to the “Extensions” page and add an extension to org.eclipse.equinox.http.registry.servlets. Be sure to pick a suitable class name and alias (the alias is what you’ll use to access the servlet; e.g. for http://localhost/hello, the alias is “/hello”).

manifest.png

Clicking on the “class*:” label will open the “New Class” wizard:

Here, change the superclass to javax.servlet.http.HttpServlet, remove the suggested interface (javax.servlet.Servlet is implemented by javax.servlet.http.HttpServlet anyway), and click “Finish”. In the body of the resulting class, use code-completion (CTRL+1) to override the doGet method.

servlet-code.png

All that’s left is to run it. Right click on the bundle in the Package Explorer, and select “Run As > OSGi Framework”. Use a browser to navigate to your servlet; the URL will look something like “http://localhost/alias” (replace “/alias” with the alias you used when you created the extension).

The first time I ran the results of the tutorial, the server wouldn’t start. Diagnosing the problem can be a challenge if you don’t know where the log is. When you run an Equinox application, the logs end up in *.log files in the [workspace]/.metadata/.plugins/org.eclipse.pde.core/OSGi Framework directory. You can also add the -consoleLog switch in the “Program Arguments” of your launch configuration which will cause logging information to be dumped onto the OSGi console.

It turns out that the port that the HTTP Server wanted to use, 80, was already in use. I opted to use a different port. There are several VM arguments that you can use to tune the settings of the HTTP Server, in particular
-Dorg.eclipse.equinox.http.jetty.http.port=8080
specifies the port on which to run. Adding this entry to my launch configuration got everything up and running on port 8080, requiring me to change the URL in my browser to “http://localhost:8080/alias”.

launch.png

I’m planning to record these steps a short demo that I’ll post on Eclipse Live. I’m also planning to write this up a little better as an Eclipse Corner Article. In the meantime, comments, criticisms, and concerns are invited.