1 23 24 package com.sun.ejb.base.stats; 25 26 import java.lang.reflect.Method ; 27 import java.util.HashMap ; 28 29 import com.sun.ejb.spi.stats.EJBMethodStatsManager; 30 31 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistry; 32 import com.sun.enterprise.admin.monitor.registry.MonitoringLevel; 33 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistrationException; 34 35 import java.util.logging.*; 36 import com.sun.enterprise.log.Log; 37 import com.sun.logging.*; 38 39 45 46 public final class EJBMethodStatsManagerImpl 47 implements EJBMethodStatsManager 48 { 49 private static Logger _logger = 50 LogDomains.getLogger(LogDomains.EJB_LOGGER); 51 52 private MonitoringRegistry registry; 53 54 private String appName; 55 private String modName; 56 private String ejbName; 57 58 private Method [] methods; 59 private boolean methodMonitorOn; 60 61 private MethodMonitor[] methodMonitors; 62 private HashMap methodMonitorMap; 63 private Object lock = new Object (); 64 private Object [] logParams = null; 65 66 EJBMethodStatsManagerImpl(MonitoringRegistry registry, 67 String ejbName, String modName, String appName) 68 { 69 this.registry = registry; 70 71 this.ejbName = ejbName; 72 this.modName = modName; 73 this.appName = appName; 74 75 logParams = new Object [] {ejbName, modName, appName}; 76 } 77 78 public final boolean isMethodMonitorOn() { 79 return methodMonitorOn; 80 } 81 82 public final void preInvoke(Method method) { 83 if (methodMonitorOn) { 84 MethodMonitor monitor = null; 85 synchronized (lock) { 86 if (methodMonitorOn) { 87 monitor = (MethodMonitor) methodMonitorMap.get(method); 88 } 89 } 90 if (monitor != null) { 91 monitor.preInvoke(); 92 } 93 } 94 } 95 96 public final void postInvoke(Method method, Throwable th) { 97 if (methodMonitorOn) { 98 MethodMonitor monitor = null; 99 synchronized (lock) { 100 if (methodMonitorOn) { 101 monitor = (MethodMonitor) methodMonitorMap.get(method); 102 } 103 } 104 if (monitor != null) { 105 monitor.postInvoke(th); 106 } 107 } 108 } 109 110 public MethodMonitor[] getMethodMonitors() { 111 return this.methodMonitors; 112 } 113 114 public void undeploy() { 115 synchronized (lock) { 116 methodMonitorOn = false; 117 } 118 deregisterStats(); 119 120 methods = null; 121 methodMonitors = null; 122 methodMonitorMap = null; 123 registry = null; 124 } 125 126 void registerMethods(Method [] methods) { 127 this.methods = methods; 128 } 129 130 void setMethodMonitorOn(boolean monitorOn) { 131 if (methods == null) { 132 _logger.log(Level.WARNING, "base.stats.method.nomethods", logParams); 133 return; 134 } 135 int size = methods.length; 136 if (monitorOn == true) { 137 this.methodMonitors = new MethodMonitor[size]; 138 HashMap map = new HashMap (); 139 for (int i=0; i<size; i++) { 140 methodMonitors[i] = new MethodMonitor(methods[i]); 141 map.put(methods[i], methodMonitors[i]); 142 143 EJBMethodStatsImpl impl = 144 new EJBMethodStatsImpl(methodMonitors[i]); 145 try { 146 if (_logger.isLoggable(Level.FINE)) { 147 _logger.log(Level.FINE, "Registering method: " 148 + methodMonitors[i].getMethodName() 149 + "; for " + appName + "; " + modName 150 + "; " + ejbName); 151 } 152 registry.registerEJBMethodStats(impl, 153 methodMonitors[i].getMethodName(), 154 ejbName, modName, appName, null); 155 if (_logger.isLoggable(Level.FINE)) { 156 _logger.log(Level.FINE, "Registered method: " 157 + methodMonitors[i].getMethodName() 158 + "; for " + appName + "; " + modName 159 + "; " + ejbName); 160 } 161 } catch (MonitoringRegistrationException monRegEx) { 162 Object [] params = new Object [] {ejbName, modName, 163 appName, methodMonitors[i].getMethodName()}; 164 _logger.log(Level.WARNING, 165 "base.stats.method.register.monreg.error", params); 166 _logger.log(Level.FINE, "", monRegEx); 167 } catch (Exception ex) { 168 Object [] params = new Object [] {ejbName, modName, 169 appName, methodMonitors[i].getMethodName()}; 170 _logger.log(Level.WARNING, 171 "base.stats.method.register.error", params); 172 _logger.log(Level.FINE, "", ex); 173 } 174 } 175 this.methodMonitorMap = map; 176 synchronized (lock) { 177 this.methodMonitorOn = true; 178 } 179 } else { 180 synchronized (lock) { 181 this.methodMonitorOn = false; 182 } 183 deregisterStats(); 184 185 this.methodMonitorMap = null; 186 this.methodMonitors = null; 187 188 } 189 } 190 191 void appendStats(StringBuffer sbuf) { 192 if (methodMonitors != null) { 193 int size = methods.length; 194 for (int i=0; i<size; i++) { 195 MethodMonitor monitor = 196 (MethodMonitor) methodMonitors[i]; 197 monitor.appendStats(sbuf); 198 } 199 } 200 } 201 202 private void deregisterStats() { 203 if (methodMonitors == null) { 204 return; 205 } 206 int size = methodMonitors.length; 207 for (int i=0; i<size; i++) { 208 try { 209 registry.unregisterEJBMethodStats( 210 methodMonitors[i].getMethodName(), 211 ejbName, modName, appName); 212 if (_logger.isLoggable(Level.FINE)) { 213 _logger.log(Level.FINE, "Unregistered method: " 214 + methodMonitors[i].getMethodName() 215 + "; for " + appName + "; " + modName 216 + "; " + ejbName); 217 } 218 } catch (MonitoringRegistrationException monRegEx) { 219 Object [] params = new Object [] {ejbName, modName, 220 appName, methodMonitors[i].getMethodName()}; 221 _logger.log(Level.FINE, 222 "base.stats.method.unregister.monreg.error", params); 223 _logger.log(Level.FINE, "", monRegEx); 224 } catch (Exception ex) { 225 Object [] params = new Object [] {ejbName, modName, 226 appName, methodMonitors[i].getMethodName()}; 227 _logger.log(Level.WARNING, 228 "base.stats.method.unregister.error", params); 229 _logger.log(Level.FINE, "", ex); 230 } 231 } 232 233 methodMonitors = null; 234 } 235 236 } 237 | Popular Tags |