/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sample.scatterplot.websockets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import com.sun.grizzly.websockets.WebSocketEngine;
import javax.inject.Inject;
import javax.servlet.ServletConfig;
import sample.scatterplot.ejb.PlotQueryEjb;

/**
 *
 * The Servlet that registers the PlotApplication into the module.
 * The Servlet gets the EJB via CDI and gives it to the Application,
 * besides the servlet passes all the mappings as argument to the
 * application to know when the request is for the application.
 *
 * @author ricky
 */
@WebServlet(name="WebSocketsServlet", urlPatterns={"/plot"}, loadOnStartup=1)
public class WebSocketsServlet extends HttpServlet {

    /**
     * PlotApplication to register
     */
    private PlotApplication app = null;

    /**
     * The EJB to use
     */
    @Inject
    private PlotQueryEjb plotQueryEjb;

    /**
     * Init registers the WebSocket application
     * @param config ServletConfig
     * @throws ServletException Some error
     */
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.err.println("init");
        System.err.println("plotQueryEjb: " + plotQueryEjb);
        app = new PlotApplication(plotQueryEjb,
                config.getServletContext().getServletRegistration(config.getServletName()).getMappings());
        WebSocketEngine.getEngine().register(app);
    }

    /**
     * Unregister WebSockets application
     */
    @Override
    public void destroy() {
        System.err.println("destroy");
        WebSocketEngine.getEngine().unregister(app);
    }
}

