KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > base > stats > MethodMonitor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.base.stats;
25
26 import java.lang.reflect.Method JavaDoc;
27
28 import java.util.ArrayList JavaDoc;
29
30 import com.sun.enterprise.admin.monitor.stats.TimeStatisticImpl;
31 import com.sun.enterprise.admin.monitor.stats.MutableTimeStatisticImpl;
32
33 /**
34  * A Class for providing stats for an EJB method
35  * All concrete S1AS containers instantiate one instance of
36  * this class for each EJB method.
37  *
38  * @author Mahesh Kannan
39  */

40
41 public class MethodMonitor {
42
43     private String JavaDoc methodName;
44     private boolean monitorOn = true;
45
46     private static ThreadLocal JavaDoc execThreadLocal = new ThreadLocal JavaDoc();
47     private Object JavaDoc lock = new Object JavaDoc();
48     private int successCount = 0;
49     private int errorCount = 0;
50     private int invocationCount = 0;
51     private long totalExecutionTime = 0;
52
53     private MutableTimeStatisticImpl methodStat;
54
55     public MethodMonitor(Method JavaDoc method) {
56     this.methodName = constructMethodName(method);
57     this.monitorOn = true;
58     }
59
60     void setMutableTimeStatisticImpl(MutableTimeStatisticImpl methodStat) {
61     this.methodStat = methodStat;
62     }
63
64     public void preInvoke() {
65     if (monitorOn) {
66         ArrayList JavaDoc list = (ArrayList JavaDoc) execThreadLocal.get();
67         if (list == null) {
68         list = new ArrayList JavaDoc(5);
69         execThreadLocal.set(list);
70         }
71         list.add(new Long JavaDoc(System.currentTimeMillis()));
72         synchronized (lock) {
73         invocationCount++;
74         }
75     }
76     }
77
78     public void postInvoke(Throwable JavaDoc th) {
79     if (monitorOn) {
80         ArrayList JavaDoc list = (ArrayList JavaDoc) execThreadLocal.get();
81         if ( (list != null) && (list.size() > 0) ) {
82         int index = list.size();
83         Long JavaDoc startTime = (Long JavaDoc) list.remove(index-1);
84         synchronized(lock) {
85             if (th == null) {
86             successCount++;
87             } else {
88             errorCount++;
89             }
90             if (startTime != null) {
91             long diff = System.currentTimeMillis()
92                 - startTime.longValue();
93             totalExecutionTime = diff;
94
95             methodStat.incrementCount(diff);
96             }
97         }
98         }
99     }
100     }
101
102     public void resetAllStats(boolean monitorOn) {
103     successCount = 0;
104     errorCount = 0;
105     invocationCount = 0;
106     totalExecutionTime = 0;
107     this.monitorOn = monitorOn;
108     }
109
110     public String JavaDoc getMethodName() {
111     return this.methodName;
112     }
113
114     public int getTotalInvocations() {
115     return invocationCount;
116     }
117
118     public long getExecutionTime() {
119     return totalExecutionTime;
120     }
121
122     public int getTotalNumErrors() {
123     return errorCount;
124     }
125
126     public int getTotalNumSuccess() {
127     return successCount;
128     }
129
130     public void appendStats(StringBuffer JavaDoc sbuf) {
131     sbuf.append("\n\t[Method ")
132         .append("name=").append(methodName).append("; ")
133         .append("invCount=").append(invocationCount).append("; ")
134         .append("success=").append(successCount).append("; ")
135         .append("errors=").append(errorCount).append("; ")
136         .append("totalTime=").append(totalExecutionTime).append("]");
137     }
138
139
140     private String JavaDoc constructMethodName(Method JavaDoc method) {
141     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
142     sbuf.append(method.getName());
143     Class JavaDoc[] paramTypes = method.getParameterTypes();
144     int sz = paramTypes.length;
145     if (sz > 0) {
146         String JavaDoc dash = "-";
147         for (int i=0; i<sz; i++) {
148         sbuf.append(dash)
149             .append(paramTypes[i].getName());
150         }
151     }
152     return sbuf.toString();
153     }
154
155 }
156
Popular Tags