package homega.http.cometd;

import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.felix.ipojo.Nullable;
import org.mortbay.cometd.continuation.ContinuationCometdServlet;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;

public class JettyCometdServer {
    private HttpService m_httpservice;
    private Servlet m_cometd;

    /**
     * The validate call-back. Simply register the CometdServlet thanks to the
     * HttpService.
     */
    @SuppressWarnings("unused")
    private void start() {
        try {
            m_cometd = new ContinuationCometdServlet();

            m_httpservice.registerServlet("/cometd", m_cometd, null, null);

            // In order to test, i put a the Dojo cometd client in a webdemo
            // folder in the bundle resources.
            // m_httpservice.registerResources("/examples", "/webdemo", null);

        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NamespaceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * The invalidate call-back. Unregister the previously registered Servlet
     * and resources.
     */
    @SuppressWarnings("unused")
    private void stop() {
        if (m_httpservice != null && !(m_httpservice instanceof Nullable)) {
            // m_httpservice.unregister("/examples");
            m_httpservice.unregister("/cometd");
        }
    }
}
