1 /*
2 * JBoss, the OpenSource WebOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7 package org.jboss.web.tomcat.tc4.statistics;
8
9 import java.io.IOException;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpSession;
12 import javax.servlet.ServletException;
13
14 import org.apache.catalina.valves.ValveBase;
15 import org.apache.catalina.util.LifecycleSupport;
16 import org.apache.catalina;
17 import org.jboss.web.tomcat.tc4.EmbeddedTomcatService;
18 import org.jboss.web.tomcat.statistics.InvocationStatistics;
19
20 /**
21 This valve provides information to the container about active threads and
22 resquest timings.
23
24 @author Thomas Peuss <jboss@peuss.de>
25 @author Scott.Stark@jboss.org
26 @version $Revision: 1.1.1.1 $
27 */
28 public class ContainerStatsValve extends ValveBase implements Lifecycle
29 {
30 /** The info string for this Valve */
31 private static final String info = "ContainerStatsValve/1.0";
32
33 /** Valve-lifecycle helper object */
34 protected LifecycleSupport support = new LifecycleSupport(this);
35
36 /** The web container stats reference */
37 protected InvocationStatistics stats;
38
39 /**
40 Create a new Valve.
41 @param catalina The container associated with the valve.
42 */
43 public ContainerStatsValve(InvocationStatistics stats)
44 {
45 super();
46 this.stats = stats;
47 }
48
49 /**
50 Get information about this Valve.
51 */
52 public String getInfo()
53 {
54 return info;
55 }
56
57 /**
58 Valve-chain handler method.
59 This method gets called when the request goes through the Valve-chain. The
60 activeThreadCount attribute gets incremented/decremented before/after the
61 servlet invocation.
62
63 @param request The request object associated with this request.
64 @param response The response object associated with this request.
65 @param context The context of the Valve-invocation.
66 */
67 public void invoke(Request request, Response response, ValveContext context)
68 throws IOException, ServletException
69 {
70 long start = System.currentTimeMillis();
71
72 // increment active thread count
73 stats.callIn();
74
75 // let the servlet invokation go through
76 context.invokeNext(request, response);
77
78 // decrement active thread count
79 stats.callOut();
80
81 // Update the web context invocation stats
82 long end = System.currentTimeMillis();
83 long elapsed = end - start;
84 String webCtx = request.getContext().getName();
85 stats.updateStats(webCtx, elapsed);
86 }
87
88 // Lifecylce-interface
89 public void addLifecycleListener(LifecycleListener listener)
90 {
91 support.addLifecycleListener(listener);
92 }
93
94 public void removeLifecycleListener(LifecycleListener listener)
95 {
96 support.removeLifecycleListener(listener);
97 }
98
99 public LifecycleListener[] findLifecycleListeners()
100 {
101 return support.findLifecycleListeners();
102 }
103
104 public void start() throws LifecycleException
105 {
106 support.fireLifecycleEvent(START_EVENT, this);
107 }
108
109 public void stop() throws LifecycleException
110 {
111 support.fireLifecycleEvent(STOP_EVENT, this);
112 stats = null;
113 }
114
115 }