KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > counter > CounterInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.varia.counter;
23
24 import java.util.Map JavaDoc;
25
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import org.jboss.invocation.Invocation;
29 import org.jboss.ejb.Container;
30 import org.jboss.ejb.plugins.*;
31
32
33 import org.jboss.logging.Logger;
34 import org.jboss.varia.counter.CounterService;
35
36 /**
37  * Interceptor that uses the CounterService MBean to record the length of time
38  * spent in 'lower' interceptors (below it in the stack).
39  *
40  * <p><b>How to use:</b></p>
41  * <p>First, the CounterService MBean must be installed in JBoss.
42  * See counter-service.xml for details/examples.
43  *
44  * <p>Next, you need to configure this interceptor into the interceptor stacks
45  * of any beans you wish to monitor. This can be done either globally for a
46  * container-config in standardjboss.xml, or on a per-bean basis in a jar's
47  * jboss.jcml. Just insert the following at the top of the &lt;container-interceptors&gt;
48  * section. If you're overriding this for a bean in jboss.xml, you'll need to
49  * override the entire container-interceptors section.</p>
50  *
51  * <code>
52  * &lt;interceptor&gt;org.jboss.varia.counter.CounterInterceptor&lt;/interceptor&gt;
53  * </code>
54  *
55  * <p>This can go anywhere in the container-interceptors section, but either
56  * the top or the bottom will probably be best for gathering application
57  * statistics.
58  *
59  * @author <a HREF="mailto:danch@nvisia.com">Dan Christopherson</href>
60  * @version $Revision: 37459 $
61  */

62 public class CounterInterceptor
63    extends AbstractInterceptor
64 {
65    Container container = null;
66    CounterService counter = null;
67    boolean loggedNoCounter = false;
68    StringBuffer JavaDoc baseCounterName = null;
69    int baseNameLength = 0;
70
71    public CounterInterceptor() {
72    }
73    
74    public void setContainer(Container container) {
75       baseCounterName = new StringBuffer JavaDoc(container.getBeanClass().getName());
76       baseNameLength = baseCounterName.length();
77       this.container = container;
78    }
79    public Container getContainer() {
80       return container;
81    }
82
83    public Object JavaDoc invokeHome(Invocation mi) throws Exception JavaDoc {
84       long startTime=System.currentTimeMillis();
85       try {
86          return super.invokeHome(mi);
87       } finally {
88          if (getCounter() != null) {
89             long endTime=System.currentTimeMillis();
90             baseCounterName.append("Home.");
91             baseCounterName.append(mi.getMethod().getName());
92             counter.accumulate(baseCounterName.toString(), endTime-startTime);
93             baseCounterName.setLength(baseNameLength);
94          }
95       }
96    }
97
98    public Object JavaDoc invoke(Invocation mi) throws Exception JavaDoc {
99       long startTime=System.currentTimeMillis();
100       try {
101          return super.invoke(mi);
102       } finally {
103          if (getCounter() != null) {
104             long endTime=System.currentTimeMillis();
105             baseCounterName.append('.');
106             baseCounterName.append(mi.getMethod().getName());
107             counter.accumulate(baseCounterName.toString(), endTime-startTime);
108             baseCounterName.setLength(baseNameLength);
109          }
110       }
111    }
112
113    public void create() throws java.lang.Exception JavaDoc {
114       //get a reference to the CounterService from JNDI
115
log.debug("CounterInterceptor initializing");
116    }
117
118    private CounterService getCounter() {
119       if (counter == null) {
120          try {
121             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
122             counter = (CounterService)ctx.lookup(CounterService.JNDI_NAME);
123          } catch (NamingException JavaDoc ne) {
124             if (!loggedNoCounter) {
125                log.warn("CounterInterceptor can't get counter service ", ne);
126                loggedNoCounter = true;
127             }
128          }
129       }
130       return counter;
131    }
132
133    // Monitorable implementation ------------------------------------
134

135    public void sample(Object JavaDoc s)
136    {
137       // Just here to because Monitorable request it but will be removed soon
138
}
139    
140    public Map JavaDoc retrieveStatistic()
141    {
142       return null;
143    }
144    
145    public void resetStatistic()
146    {
147    }
148 }
149
Popular Tags