/*
 * 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 org.icepush.*;
import org.icepush.http.Server;
import org.icepush.servlet.ServletContextConfiguration;
import org.icepush.util.Slot;

import javax.servlet.ServletContext;
import java.util.Timer;
import java.util.logging.Logger;

public class BrowserBoundServlet
        extends org.icepush.servlet.BrowserBoundServlet {
    private static final Logger LOGGER = Logger.getLogger(BrowserBoundServlet.class.getName());
    private static boolean configIsLogged = false;
    private Configuration eeConfiguration;

    public BrowserBoundServlet(
            final PushContext pushContext, final ServletContext context, final PushGroupManager pushGroupManager,
            final Timer monitoringScheduler, final Configuration configuration,
            final boolean terminateBlockingConnectionOnShutdown) {
        super(
                pushContext, context, pushGroupManager, monitoringScheduler, configuration,
                terminateBlockingConnectionOnShutdown);
    }

    protected Server createBlockingConnectionServer() {
        if (eeConfiguration == null) {
            eeConfiguration = new ServletContextConfiguration("com.icesoft.icepush", servletContext);
        }
        // Log config params
        if (!configIsLogged) {
			StringBuilder info = new StringBuilder();
			eeConfiguration.logLong("com.icesoft.icepush.minHeartbeatInterval", eeConfiguration.getAttributeAsLong("minHeartbeatInterval", -1), -1, info);
			eeConfiguration.logLong("com.icesoft.icepush.maxHeartbeatInterval", eeConfiguration.getAttributeAsLong("maxHeartbeatInterval", -1), -1, info);
		 	LOGGER.info("ICEpush EE Configuration: \n" + info);
			configIsLogged = true;
		}

        Slot sequenceNo = new Slot(0L);
        return new ConfigurationServer(
                heartbeatInterval,
                pushContext,
                servletContext,
                configuration,
                new AdjustHeartbeatServer(
                        sequenceNo,
                        heartbeatInterval,
                        eeConfiguration,
                        new SequenceTaggingServer(
                                sequenceNo,
                                new BlockingConnectionServer(
                                        pushGroupManager,
                                        monitoringScheduler,
                                        heartbeatInterval,
                                        terminateBlockingConnectionOnShutdown,
                                        configuration
                                )
                        )
                )
        );
    }
}
