KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashMap JavaDoc;
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 /**
40  * A class that manages all the monitored EJB methods
41  *
42  * @author Mahesh Kannan
43  *
44  */

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 JavaDoc appName;
55     private String JavaDoc modName;
56     private String JavaDoc ejbName;
57
58     private Method JavaDoc[] methods;
59     private boolean methodMonitorOn;
60
61     private MethodMonitor[] methodMonitors;
62     private HashMap JavaDoc methodMonitorMap;
63     private Object JavaDoc lock = new Object JavaDoc();
64     private Object JavaDoc[] logParams = null;
65
66     EJBMethodStatsManagerImpl(MonitoringRegistry registry,
67         String JavaDoc ejbName, String JavaDoc modName, String JavaDoc appName)
68     {
69     this.registry = registry;
70
71     this.ejbName = ejbName;
72     this.modName = modName;
73     this.appName = appName;
74
75     logParams = new Object JavaDoc[] {ejbName, modName, appName};
76     }
77
78     public final boolean isMethodMonitorOn() {
79     return methodMonitorOn;
80     }
81
82     public final void preInvoke(Method JavaDoc 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 JavaDoc method, Throwable JavaDoc 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 JavaDoc[] 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 JavaDoc map = new HashMap JavaDoc();
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 JavaDoc[] params = new Object JavaDoc[] {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 JavaDoc ex) {
168             Object JavaDoc[] params = new Object JavaDoc[] {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 JavaDoc 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 JavaDoc[] params = new Object JavaDoc[] {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 JavaDoc ex) {
225         Object JavaDoc[] params = new Object JavaDoc[] {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