Apache Felix HTTP whiteboard pattern on Equinox

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:

  1. Download recent versions of both bundles 'HTTP Service API' and 'HTTP Service Whiteboard' to e.g. Eclipse's dropins/ folder

    1. It is version 2.2.2 as of writing
  2. Add also bundle org.eclipse.equinox.http.jetty and its dependencies to the launch configuration

    1. Alternatively use Felix's incarnation of Jetty called 'HTTP Service Jetty'
  3. 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);
        }
    }
    
  4. <?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>
    
  5. Set 'Bundle-ActivationPolicy: lazy' and list the component xml as a Service-Component for the DS extender in the MANIFEST.MF

    1. Do not forget to explicitly add DS org.eclipse.equinox.ds to your launch until capabilites are available
  6. 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!


Want to comment? Send me an email to blog-comments-2016 at lemmster d.t de and I'll paste it here (I won't publish your address). Why don't you use an external comment service like disqus, you ask? Well, I like to keep this site under my control, comments included. You can use markdown to format your comment.