Unless I am missing something obvious, using the Equinox HttpService requires one to:
-
a) get the HttpService from the OSGi service registry
-
b) register the HttpServlet on the HttpService via a call to registerServlet(..)
This can be done however much more elegantly and - way more importantly - in a dynamic aware fashion meaning, that the HttpServlet that is registered survives life-cycle changes of the HttpService.
E.g. consider the case where one registers the HttpServlet with registerServlet(..) and the HttpService is redeployed afterwards. Unless the HttpService maintains its internal state (which is rather unlikely if e.g. Tomcat gets replaced with Jetty), the HttpServlet won't be available anymore and consumer code will have to re-register the HttpServlet again (by listening to life-cycle changes of the HttpService). If, on the other hand the HttpServlet is simply registered with the OSGi service registry as a javax.servlet.Servlet μService (SR never goes away), the HttpService itself (or an extender as a matter of fact) can lookup all services of the javax.servlet.Servlet type whenever its need to (read when the HttpService transitions through life-cycle changes).
To leverage this on Equinox, just follow the steps below:
-
Download recent versions of both bundles 'HTTP Service API' and 'HTTP Service Whiteboard' to e.g. Eclipse's dropins/ folder
- It is version 2.2.2 as of writing
-
Add also bundle org.eclipse.equinox.http.jetty and its dependencies to the launch configuration
- Alternatively use Felix's incarnation of Jetty called 'HTTP Service Jetty'
-
package org.kuppe.exmpl; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ChatServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setStatus(HttpServletResponse.SC_OK); } }
-
<?xml version="1.0" encoding="UTF-8"?> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.kuppe.exmpl.chat.web"> <implementation/> <service> <provide interface="javax.servlet.Servlet"/> </service> <property name="alias" type="String" value="/chat"/> </scr:component>
-
Set 'Bundle-ActivationPolicy: lazy' and list the component xml as a Service-Component for the DS extender in the MANIFEST.MF
- Do not forget to explicitly add DS org.eclipse.equinox.ds to your launch until capabilites are available
-
Optionally add -Dorg.eclipse.equinox.http.jetty.http.port=8181 to change the Jetty port to 8181
Also read the generic Apache Felix Http whiteboard instructions!