/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This 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 (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.web.tomcat.tc4.statistics;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;

import org.apache.catalina.valves.ValveBase;
import org.apache.catalina.util.LifecycleSupport;
import org.apache.catalina.*;
import org.jboss.web.tomcat.tc4.EmbeddedTomcatService;
import org.jboss.web.tomcat.statistics.InvocationStatistics;

/**
 This valve provides information to the container about active threads and
 resquest timings.

 @author Thomas Peuss <jboss@peuss.de>
 @author Scott.Stark@jboss.org
 @version $Revision: 1.1.1.1.2.1 $
 */
public class ContainerStatsValve extends ValveBase implements Lifecycle
{
   /** The info string for this Valve */
   private static final String info = "ContainerStatsValve/1.0";

   /** Valve-lifecycle helper object */
   protected LifecycleSupport support = new LifecycleSupport(this);

   /** The web container stats reference */
   protected InvocationStatistics stats;

   /**
    Create a new Valve.
    @param catalina The container associated with the valve. 
    */
   public ContainerStatsValve(InvocationStatistics stats)
   {
      super();
      this.stats = stats;
   }

   /**
    Get information about this Valve.
    */
   public String getInfo()
   {
      return info;
   }

   /**
    Valve-chain handler method.
    This method gets called when the request goes through the Valve-chain. The
    activeThreadCount attribute gets incremented/decremented before/after the
    servlet invocation.

    @param request The request object associated with this request.
    @param response The response object associated with this request.
    @param context The context of the Valve-invocation.
    */
   public void invoke(Request request, Response response, ValveContext context)
      throws IOException, ServletException
   {
      long start = System.currentTimeMillis();

      // increment active thread count
      stats.callIn();

      // let the servlet invokation go through
      context.invokeNext(request, response);
     
      // decrement active thread count
      stats.callOut();

      // Update the web context invocation stats
      long end = System.currentTimeMillis();
      long elapsed = end - start;
      String webCtx = request.getContext().getName();
      stats.updateStats(webCtx, elapsed);
   }

   // Lifecylce-interface
   public void addLifecycleListener(LifecycleListener listener)
   {
      support.addLifecycleListener(listener);
   }

   public void removeLifecycleListener(LifecycleListener listener)
   {
      support.removeLifecycleListener(listener);
   }

   public LifecycleListener[] findLifecycleListeners()
   {
      return support.findLifecycleListeners();
   }

   public void start() throws LifecycleException
   {
      support.fireLifecycleEvent(START_EVENT, this);
   }

   public void stop() throws LifecycleException
   {
      support.fireLifecycleEvent(STOP_EVENT, this);
      stats = null;
   }

}
