1 23 24 package com.sun.ejb.base.stats; 25 26 import java.lang.reflect.Method ; 27 28 import java.util.ArrayList ; 29 30 import com.sun.enterprise.admin.monitor.stats.TimeStatisticImpl; 31 import com.sun.enterprise.admin.monitor.stats.MutableTimeStatisticImpl; 32 33 40 41 public class MethodMonitor { 42 43 private String methodName; 44 private boolean monitorOn = true; 45 46 private static ThreadLocal execThreadLocal = new ThreadLocal (); 47 private Object lock = new Object (); 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 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 list = (ArrayList ) execThreadLocal.get(); 67 if (list == null) { 68 list = new ArrayList (5); 69 execThreadLocal.set(list); 70 } 71 list.add(new Long (System.currentTimeMillis())); 72 synchronized (lock) { 73 invocationCount++; 74 } 75 } 76 } 77 78 public void postInvoke(Throwable th) { 79 if (monitorOn) { 80 ArrayList list = (ArrayList ) execThreadLocal.get(); 81 if ( (list != null) && (list.size() > 0) ) { 82 int index = list.size(); 83 Long startTime = (Long ) 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 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 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 constructMethodName(Method method) { 141 StringBuffer sbuf = new StringBuffer (); 142 sbuf.append(method.getName()); 143 Class [] paramTypes = method.getParameterTypes(); 144 int sz = paramTypes.length; 145 if (sz > 0) { 146 String 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 |