KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > monitor > impl > PwcWebModuleStatsImpl


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 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.enterprise.web.monitor.impl;
30
31 import java.io.ObjectInputStream JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.MBeanServerFactory JavaDoc;
39 import javax.management.MBeanServer JavaDoc;
40 import javax.management.Attribute JavaDoc;
41 import com.sun.enterprise.web.monitor.PwcWebModuleStats;
42
43 /**
44  * Class responsible for gathering web module statistics.
45  */

46 public class PwcWebModuleStatsImpl implements PwcWebModuleStats {
47     
48     private static final String JavaDoc WEB_MODULE_PREFIX = "//";
49     private static final String JavaDoc LIST_SESSION_IDS = "listSessionIds";
50     private static final String JavaDoc GET_SESSION = "getSession";
51     private static final String JavaDoc[] STRING_PARAM = new String JavaDoc[] {
52                                                         "java.lang.String" };
53     private static final Integer JavaDoc ZERO_INTEGER = new Integer JavaDoc(0);
54
55     private ObjectName JavaDoc jspMonitorObjName;
56     private ObjectName JavaDoc sessionManagerObjName;
57     private ObjectName JavaDoc ctxObjName;
58     private transient MBeanServer JavaDoc server;
59
60
61     /**
62      * Constructor.
63      *
64      * @param ctxObjNameStr Context object name
65      * @param ctxPath Context path
66      * @param domain Domain in which the Servlet MBean is registered
67      * @param vsId The id of the virtual-server on which the web module has
68      * been deployed
69      * @param appName Name of the J2EE application to which the web module
70      * belongs, or null if the web module is standalone
71      * @param serverName The server instance name
72      */

73     public PwcWebModuleStatsImpl(String JavaDoc ctxObjNameStr,
74                                  String JavaDoc ctxPath,
75                                  String JavaDoc domain,
76                                  String JavaDoc vsId,
77                                  String JavaDoc appName,
78                                  String JavaDoc serverName) {
79
80         // Get an instance of the MBeanServer
81
ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
82         if(servers != null && !servers.isEmpty()) {
83             server = (MBeanServer JavaDoc)servers.get(0);
84         } else {
85             server = MBeanServerFactory.createMBeanServer();
86         }
87
88         if ("".equals(ctxPath)) {
89             ctxPath = "/";
90         }
91
92         /*
93          * j2eeType=WebModule
94          */

95         try {
96             ctxObjName = new ObjectName JavaDoc(ctxObjNameStr);
97         } catch (Throwable JavaDoc t) {
98             MonitorUtil.log(Level.SEVERE,
99                             "pwc.monitoring.objectNameCreationError",
100                             new Object JavaDoc[] { ctxObjNameStr },
101                             t);
102         }
103
104         /*
105          * type=JspMonitor
106          */

107         String JavaDoc objNameStr = domain
108                 + ":type=JspMonitor"
109                 + ",WebModule=" + WEB_MODULE_PREFIX + vsId + ctxPath
110                 + ",J2EEApplication=" + appName
111                 + ",J2EEServer=" + serverName
112                 + ",*";
113         try {
114             jspMonitorObjName = new ObjectName JavaDoc(objNameStr);
115         } catch (Throwable JavaDoc t) {
116             MonitorUtil.log(Level.SEVERE,
117                             "pwc.monitoring.objectNameCreationError",
118                             new Object JavaDoc[] { objNameStr },
119                             t);
120         }
121
122         /*
123          * type=Manager
124          */

125         objNameStr = domain + ":type=Manager,path=" + ctxPath
126                 + ",host=" + vsId;
127         try {
128             sessionManagerObjName = new ObjectName JavaDoc(objNameStr);
129         } catch (Throwable JavaDoc t) {
130             MonitorUtil.log(Level.SEVERE,
131                             "pwc.monitoring.objectNameCreationError",
132                             new Object JavaDoc[] { objNameStr },
133                             t);
134         }
135
136     }
137     
138     
139     /**
140      * Gets the number of JSPs that have been loaded in the web module.
141      *.
142      * @return Number of JSPs that have been loaded
143      */

144     public int getJspCount() {
145         return queryJspStatistic(jspMonitorObjName, "jspCount");
146     }
147     
148     
149     /**
150      * Gets the number of JSPs that have been reloaded in the web module.
151      *.
152      * @return Number of JSPs that have been reloaded
153      */

154     public int getJspReloadCount() {
155         return queryJspStatistic(jspMonitorObjName, "jspReloadCount");
156     }
157
158
159     /**
160      * Gets the number of errors that were triggered by JSP invocations.
161      *.
162      * @return Number of errors triggered by JSP invocations
163      */

164     public int getJspErrorCount() {
165         return queryJspStatistic(jspMonitorObjName, "jspErrorCount");
166     }
167
168
169     /**
170      * Gets the total number of sessions that have been created for the web
171      * module.
172      *.
173      * @return Total number of sessions created
174      */

175     public int getSessionsTotal() {
176         return getIntValue(queryStatistic(sessionManagerObjName,
177                                           "sessionCount"));
178     }
179
180
181     /**
182      * Gets the number of currently active sessions for the web module.
183      *.
184      * @return Number of currently active sessions
185      */

186     public int getActiveSessionsCurrent() {
187         return getIntValue(queryStatistic(sessionManagerObjName,
188                                           "activeSessions"));
189     }
190
191
192     /**
193      * Gets the maximum number of concurrently active sessions for the web
194      * module.
195      *
196      * @return Maximum number of concurrently active sessions
197      */

198     public int getActiveSessionsHigh() {
199         return getIntValue(queryStatistic(sessionManagerObjName,
200                                           "maxActive"));
201     }
202
203
204     /**
205      * Gets the total number of rejected sessions for the web module.
206      *
207      * <p>This is the number of sessions that were not created because the
208      * maximum allowed number of sessions were active.
209      *
210      * @return Total number of rejected sessions
211      */

212     public int getRejectedSessionsTotal() {
213         return getIntValue(queryStatistic(sessionManagerObjName,
214                                           "rejectedSessions"));
215     }
216
217
218     /**
219      * Gets the total number of expired sessions for the web module.
220      *.
221      * @return Total number of expired sessions
222      */

223     public int getExpiredSessionsTotal() {
224         return getIntValue(queryStatistic(sessionManagerObjName,
225                                           "expiredSessions"));
226     }
227
228
229     /**
230      * Gets the longest time (in seconds) that an expired session had been
231      * alive.
232      *
233      * @return Longest time (in seconds) that an expired session had been
234      * alive.
235      */

236     public int getSessionMaxAliveTimeSeconds() {
237         return getIntValue(queryStatistic(sessionManagerObjName,
238                                           "sessionMaxAliveTimeSeconds"));
239     }
240
241
242     /**
243      * Gets the average time (in seconds) that expired sessions had been
244      * alive.
245      *
246      * @return Average time (in seconds) that expired sessions had been
247      * alive.
248      */

249     public int getSessionAverageAliveTimeSeconds() {
250         return getIntValue(queryStatistic(sessionManagerObjName,
251                                           "sessionAverageAliveTimeSeconds"));
252     }
253
254
255     /**
256      * Gets the time when the web module was started.
257      *
258      * @return Time (in milliseconds since January 1, 1970, 00:00:00) when the
259      * web module was started
260      */

261     public long getStartTimeMillis() {
262         return getLongValue(queryStatistic(ctxObjName, "startTimeMillis"));
263     }
264
265
266     /**
267      * Gets the cumulative processing times of all servlets in the web module
268      * associated with this PwcWebModuleStatsImpl.
269      *
270      * @return Cumulative processing times of all servlets in the web module
271      * associated with this PwcWebModuleStatsImpl
272      */

273     public long getServletProcessingTimesMillis() {
274         return getLongValue(queryStatistic(ctxObjName, "processingTimeMillis"));
275     }
276
277
278     /**
279      * Returns the session ids of all currently active sessions.
280      *
281      * @return Session ids of all currently active sessions
282      */

283     public String JavaDoc getSessionIds() {
284
285         Object JavaDoc resultObj = null;
286
287         try {
288             resultObj = server.invoke(sessionManagerObjName,
289                                       LIST_SESSION_IDS,
290                                       null,
291                                       null);
292         } catch (Throwable JavaDoc t) {
293             MonitorUtil.log(Level.SEVERE,
294                             "pwc.monitoring.actionInvocationError",
295                             new Object JavaDoc[] { LIST_SESSION_IDS,
296                                            sessionManagerObjName },
297                             t);
298         }
299
300         return (String JavaDoc) resultObj;
301     }
302
303
304     /**
305      * Returns information about the session with the given id.
306      *
307      * <p>The session information is organized as a HashMap, mapping
308      * session attribute names to the String representation of their values.
309      *
310      * @param id Session id
311      *
312      * @return HashMap mapping session attribute names to the String
313      * representation of their values, or null if no session with the
314      * specified id exists, or if the session does not have any attributes
315      */

316     public HashMap JavaDoc getSession(String JavaDoc id) {
317
318         Object JavaDoc resultObj = null;
319
320         try {
321             resultObj = server.invoke(sessionManagerObjName,
322                                       GET_SESSION,
323                                       new Object JavaDoc[] { id },
324                                       STRING_PARAM);
325         } catch (Throwable JavaDoc t) {
326             // Ignore if session has been invalidated
327
if (!(t instanceof IllegalStateException JavaDoc)) {
328                 MonitorUtil.log(Level.SEVERE,
329                                 "pwc.monitoring.actionInvocationError",
330                                 new Object JavaDoc[] { GET_SESSION,
331                                                sessionManagerObjName },
332                                 t);
333             }
334         }
335
336         return (HashMap JavaDoc) resultObj;
337     }
338
339
340     /**
341      * Resets this PwcWebModuleStats.
342      */

343     public void reset() {
344
345         // Reset JSP stats
346
resetStatistic(jspMonitorObjName, "jspReloadCount");
347     }
348
349
350     /**
351      * Queries the MBeanServer for the given JSP related monitoring attribute.
352      *
353      * @param on The ObjectName of the MBean(s) being queried
354      * @param attrName The name of the attribute whose value is to be
355      * returned
356      *
357      * @return The value corresponding to the given attribute name
358      */

359     private int queryJspStatistic(ObjectName JavaDoc on, String JavaDoc attrName) {
360
361     int result = 0;
362
363         /*
364          * Web module may have more than one JspServlet and associated
365          * JspMonitoring MBean: there is one JspServlet declared in and
366          * inherited from default-web.xml, plus one JspServlet for each
367          * servlet mapped to a jsp-file in web.xml.
368          * Enumerate all of them and get their stats for the given attribute.
369          */

370         Set JavaDoc jspMonitorONs = server.queryNames(on, null);
371         Iterator JavaDoc iter = jspMonitorONs.iterator();
372
373         while (iter.hasNext()) {
374
375             ObjectName JavaDoc jspMonitorON = (ObjectName JavaDoc) iter.next();
376             Object JavaDoc obj = queryStatistic(jspMonitorON, attrName);
377             if (obj != null) {
378                 result += getIntValue(obj);
379             }
380         }
381
382         return result;
383     }
384
385     
386     /*
387      * Queries the MBeanServer for an attribute.
388      *
389      * @param on The ObjectName of the MBean being queried
390      * @param attrName The name of the attribute whose value is to be
391      * returned
392      *
393      * @return The value corresponding to the given attribute name
394      */

395     private Object JavaDoc queryStatistic(ObjectName JavaDoc on, String JavaDoc attrName) {
396
397         Object JavaDoc resultObj = null;
398         try {
399             resultObj = server.getAttribute(on, attrName);
400         } catch (Throwable JavaDoc t) {
401             MonitorUtil.log(Level.SEVERE,
402                             "pwc.monitoring.queryError",
403                             new Object JavaDoc[] { attrName, on },
404                             t);
405         }
406
407         return resultObj;
408     }
409
410
411     /**
412      * Resets the value of the given attribute on the MBean with the given
413      * ObjectName.
414      *
415      * @param on MBean's ObjectName
416      * @param attrName Name of the attribute to be reset
417      */

418     private void resetStatistic(ObjectName JavaDoc on, String JavaDoc attrName) {
419
420         Attribute JavaDoc attr = new Attribute JavaDoc(attrName, ZERO_INTEGER);
421         try {
422             server.setAttribute(on, attr);
423         } catch (Throwable JavaDoc t) {
424             MonitorUtil.log(Level.SEVERE,
425                             "pwc.monitoring.resetError",
426                             new Object JavaDoc[] { attrName, on },
427                             t);
428         }
429     }
430
431
432     /*
433      * Restores this object's state from a stream.
434      *
435      * @param ois Stream from which to restore this object's state
436      */

437     private void readObject(ObjectInputStream JavaDoc ois)
438             throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
439
440         ois.defaultReadObject();
441
442         // Get an instance of the MBeanServer
443
ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
444         if (servers != null && !servers.isEmpty()) {
445             server = (MBeanServer JavaDoc)servers.get(0);
446         } else {
447             server = MBeanServerFactory.createMBeanServer();
448         }
449     }
450
451
452     private long getLongValue(Object JavaDoc resultObj) {
453
454         long result = 0;
455
456         if (resultObj != null) {
457             Long JavaDoc countObj = (Long JavaDoc)resultObj;
458             result = countObj.longValue();
459         }
460
461         return result;
462     }
463
464
465     private int getIntValue(Object JavaDoc resultObj) {
466
467         int result = 0;
468
469         if (resultObj != null) {
470             Integer JavaDoc countObj = (Integer JavaDoc)resultObj;
471             result = countObj.intValue();
472         }
473
474         return result;
475     }
476
477 }
478
Popular Tags