/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.icepush;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.icepush.CodeServer;
import org.icepush.http.standard.CacheControlledServer;
import org.icepush.http.standard.CompressingServer;
import org.icepush.servlet.BasicAdaptingServlet;
import org.icepush.servlet.PseudoServlet;
import org.icepush.util.ExtensionRegistry;

public class MainServlet extends org.icepush.servlet.MainServlet {
    private static final Logger LOGGER = Logger.getLogger(MainServlet.class.getName());

    public MainServlet(final ServletContext context) {
        this(context, true);
    }

    public MainServlet(
        final ServletContext servletContext, final boolean terminateBlockingConnectionOnShutdown) {

        this(servletContext, terminateBlockingConnectionOnShutdown, false);
    }

    public MainServlet(
        final ServletContext servletContext, final boolean terminateBlockingConnectionOnShutdown,
        final boolean printProductInfo) {

        this(servletContext, terminateBlockingConnectionOnShutdown, printProductInfo, null);
    }

    public MainServlet(
        final ServletContext servletContext, final boolean terminateBlockingConnectionOnShutdown,
        final boolean printProductInfo, final ScheduledThreadPoolExecutor executor) {

        super(servletContext, terminateBlockingConnectionOnShutdown, printProductInfo, executor);
        LOGGER.info(new ProductInfo().toString());
    }

    public synchronized static MainServlet getInstance(ServletContext servletContext)  {
        MainServlet mainServlet = (MainServlet)servletContext.getAttribute(MainServlet.class.getName());
        if (mainServlet == null)  {
            mainServlet = new MainServlet(servletContext);
            servletContext.setAttribute(MainServlet.class.getName(), mainServlet);
        }
        return mainServlet;
    }

    protected void addDispatches() {
        this.dispatchOn(
            ".*mobi\\.icepush",
            new BasicAdaptingServlet(
                new CacheControlledServer(
                    new CompressingServer(
                        new CodeServer("mobi.js")
                    )
                ),
                configuration
            )
        );
        super.addDispatches();
    }

    protected PseudoServlet createBrowserBoundServlet(final String browserID) {
        return
            new BrowserBoundServlet(
                pushContext, context, pushGroupManager, monitoringScheduler, configuration,
                terminateConnectionOnShutdown
            );
    }

    public static class ExtensionRegistration implements ServletContextListener {
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            ExtensionRegistry.addExtension(
                servletContextEvent.getServletContext(), 10, "org.icepush.MainServlet", MainServlet.class
            );
        }

        public void contextDestroyed(ServletContextEvent servletContextEvent) {
        }
    }
}
