/*
 * 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.faces.demo.poll;

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

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class DebugListener
implements
    HttpSessionActivationListener, HttpSessionAttributeListener, HttpSessionBindingListener, HttpSessionListener,
    ServletContextListener {

    private static final Logger LOGGER = Logger.getLogger(DebugListener.class.getName());

    private static final String DEBUG_ENABLED_NAME = "com.icesoft.faces.demo.poll.debugEnabled";

    // HttpSessionActivationListener -----------------------------------------------------------------------------------

    public void sessionDidActivate(final HttpSessionEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(Level.INFO, "Session did activate :: Session ID: '" + event.getSession().getId() + "'");
            }
        }
    }

    public void sessionWillPassivate(final HttpSessionEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(Level.INFO, "Session did passivate :: Session ID: '" + event.getSession().getId() + "'");
            }
        }
    }

    // HttpSessionAttributeListener ------------------------------------------------------------------------------------

    public void attributeAdded(final HttpSessionBindingEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Attribute added :: " +
                        "Session ID: '" + event.getSession().getId() + "', " +
                        "Attribute Name: '" + event.getName() + "', " +
                        "Attribute Class: '" + event.getValue().getClass().getName() + "', " +
                        "Attribute Value: '" + event.getValue() + "'");
            }
        }
    }

    public void attributeRemoved(final HttpSessionBindingEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Attribute removed :: " +
                        "Session ID: '" + event.getSession().getId() + "', " +
                        "Attribute Name: '" + event.getName() + "', " +
                        "Attribute Class: '" + event.getValue().getClass().getName() + "', " +
                        "Attribute Value: '" + event.getValue() + "'");
            }
        }
    }

    public void attributeReplaced(final HttpSessionBindingEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Attribute replaced :: " +
                        "Session ID: '" + event.getSession().getId() + "', " +
                        "Attribute Name: '" + event.getName() + "', " +
                        "Attribute Class: '" + event.getValue().getClass().getName() + "', " +
                        "Attribute Value: '" + event.getValue() + "'");
            }
        }
    }

    // HttpSessionBindingListener --------------------------------------------------------------------------------------

    public void valueBound(final HttpSessionBindingEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Value bound :: " +
                        "Session ID: '" + event.getSession().getId() + "', " +
                        "Name: '" + event.getName() + "', " +
                        "Value: '" + event.getValue() + "'");
            }
        }
    }

    public void valueUnbound(final HttpSessionBindingEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Value unbound :: " +
                        "Session ID: '" + event.getSession().getId() + "', " +
                        "Name: '" + event.getName() + "', " +
                        "Value: '" + event.getValue() + "'");
            }
        }
    }

    // HttpSessionListener ---------------------------------------------------------------------------------------------

    public void sessionCreated(final HttpSessionEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Session created :: " +
                        "Session ID: '" + event.getSession().getId() + "'");
            }
        }
    }

    public void sessionDestroyed(final HttpSessionEvent event) {
        if (Boolean.valueOf(event.getSession().getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Session destroyed :: " +
                        "Session ID: '" + event.getSession().getId() + "'");
            }
        }
    }

    // ServletContextListener ------------------------------------------------------------------------------------------

    public void contextDestroyed(final ServletContextEvent event) {
        if (Boolean.valueOf(event.getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Context destroyed :: " +
                        "Servlet Context Name: '" + event.getServletContext().getServletContextName() + "'");
            }
        }
    }

    public void contextInitialized(final ServletContextEvent event) {
        if (Boolean.valueOf(event.getServletContext().getInitParameter(DEBUG_ENABLED_NAME))) {
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.log(
                    Level.INFO,
                    "Context initialized :: " +
                        "Servlet Context Name: '" + event.getServletContext().getServletContextName() + "'");
            }
        }
    }
}
