/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas.web.jetty50;

import org.mortbay.jetty.servlet.Dispatcher;
import org.mortbay.jetty.servlet.FilterHolder;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.servlet.WebApplicationContext;
import org.mortbay.jetty.servlet.WebApplicationHandler;
import org.mortbay.jetty.servlet.WebApplicationContext.Configuration;
import org.mortbay.jetty.servlet.jsr77.Jsr77Filter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Replace the JSR77 Configuration implementation from Jetty 5.0
 * This class add Filters to the Servlets in order to provide some statistics.
 *
 * @author Guillaume Sauthier
 */
public class JSR77Configuration implements Configuration {

    /**
     * Filter name prefix
     */
    public static final String FILTER_PREFIX = "JSR77Filter-";

    /**
     * logger
     */
    private static Log log = LogFactory.getLog(JSR77Configuration.class);

    /**
     * The monitored WebApplication
     */
    private WebApplicationContext webAppContext = null;

    /**
     * Defualt Constructor
     */
    public JSR77Configuration() {
        super();
    }

    /**
     * @param wac the monitored WebApplication
     */
    public void setWebApplicationContext(WebApplicationContext wac) {
        this.webAppContext = wac;
    }

    /**
     * @return the monitored WebApplication
     */
    public WebApplicationContext getWebApplicationContext() {
        return this.webAppContext;
    }

    /**
     * was not implemented in Jetty's Configuration
     * @throws Exception not documented !
     */
    public void configureClassPath() throws Exception {
        // TODO Auto-generated method stub

    }

    /**
     * was not implemented in Jetty's Configuration
     * @throws Exception not documented !
     */
    public void configureDefaults() throws Exception {
        // TODO Auto-generated method stub

    }

    /**
     * Add the Statistic Filters for JSR77
     * @throws Exception not documented !
     */
    public void configureWebApp() throws Exception {
        if (this.webAppContext.isStarted()) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot configure webapp after it is started");
            }
            return;
        }

        // get the context, ask it for all of its servlets, configure a
        // Jsr77Filter for
        // each of them as the LAST filter
        if (this.webAppContext != null) {
            ServletHolder[] servlets = this.webAppContext.getServletHandler().getServlets();
            WebApplicationHandler waHandler = this.webAppContext.getWebApplicationHandler();

            for (int i = 0; servlets != null && i < servlets.length; i++) {
                String filterName = FILTER_PREFIX + servlets[i].getName();
                FilterHolder holder = waHandler.defineFilter(filterName, Jsr77Filter.class.getName());
                waHandler.addFilterServletMapping(servlets[i].getName(), filterName, Dispatcher.__ALL);
                holder.put("servlet-name", servlets[i].getName());
                if (log.isDebugEnabled()) {
                    log.debug("Configured JSR77 filter " + filterName);
                }
            }
        }

    }

}
