KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.management.MBeanServerFactory JavaDoc;
37 import javax.management.MBeanServer JavaDoc;
38 import com.sun.enterprise.web.monitor.PwcServletStats;
39
40
41 public class PwcServletStatsImpl implements PwcServletStats {
42     
43     private transient MBeanServer JavaDoc server;
44     private ObjectName JavaDoc servletObjName;
45
46     
47     /**
48      * Constructor.
49      *
50      * The ObjectName of the Servlet MBean follows this pattern:
51      *
52      * <domain>:j2eeType=Servlet,name=<servlet/jsp name>,
53      * WebModule=<webmodule name>,J2EEApplication=<application name>,
54      * J2EEServer=<server name>
55      *
56      * Example: com.sun.appserv:j2eeType=Servlet,name=default,
57      * WebModule=//server/,J2EEApplication=null,J2EEServer=server
58      *
59      * @param domain Domain in which the Servlet MBean is registered
60      * @param vsId Id of the virtual-server for which servlet monitoring
61      * is being enabled
62      * @param contextPath Context path of the webmodule
63      * @param servletName Name of the Servlet/JSP
64      * @param appName Name of the J2EE App to which the web module belongs, or
65      * null if web module is standalone
66      * @param serverName Name of the server instance
67      */

68     public PwcServletStatsImpl(String JavaDoc domain,
69                                String JavaDoc vsId,
70                                String JavaDoc contextPath,
71                                String JavaDoc servletName,
72                                String JavaDoc appName,
73                                String JavaDoc serverName) {
74         
75         // Get an instance of the MBeanServer
76
ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
77         if(servers != null && !servers.isEmpty())
78             server = (MBeanServer JavaDoc)servers.get(0);
79         else
80             server = MBeanServerFactory.createMBeanServer();
81
82         // Construct the ObjectName of the Servlet MBean
83
String JavaDoc objNameStr = domain
84                 + ":j2eeType=Servlet"
85                 + ",name=" + servletName
86                 + ",WebModule=" + createTomcatWebModuleName(vsId, contextPath)
87                 + ",J2EEApplication=" + appName
88                 + ",J2EEServer=" + serverName;
89         try {
90             servletObjName = new ObjectName JavaDoc(objNameStr);
91         } catch (Throwable JavaDoc t) {
92             MonitorUtil.log(Level.SEVERE,
93                             "pwc.monitoring.objectNameCreationError",
94                             new Object JavaDoc[] { objNameStr },
95                             t);
96         }
97     }
98     
99     
100     /**
101      * Gets the maximum request processing time of the servlet being
102      * monitored.
103      *
104      * @return Maximum request processing time
105      */

106     public long getMaxTimeMillis() {
107         return getLongValue(queryStatistic(servletObjName, "maxTimeMillis"));
108     }
109
110     
111     /**
112      * Gets the minimum request processing time of the servlet being monitored.
113      *
114      * @return Minimum request processing time
115      */

116     public long getMinTimeMillis() {
117         return getLongValue(queryStatistic(servletObjName, "minTimeMillis"));
118     }
119
120
121     /**
122      * Gets the total execution time of the service method of the servlet being
123      * monitored.
124      *
125      * @return Execution time of the servlet's service method
126      */

127     public long getProcessingTimeMillis() {
128         return getLongValue(queryStatistic(servletObjName,
129                                            "processingTimeMillis"));
130     }
131
132     
133     /**
134      * Gets the number of requests processed by the servlet being monitored.
135      *
136      * @return Number of processed requests
137      */

138     public int getRequestCount() {
139         return getIntValue(queryStatistic(servletObjName, "requestCount"));
140     }
141
142     
143     /**
144      * Gets the number of requests processed by the servlet being monitored
145      * that resulted in errors.
146      *
147      * @return Error count
148      */

149     public int getErrorCount() {
150         return getIntValue(queryStatistic(servletObjName, "errorCount"));
151     }
152
153
154     /*
155      * Queries the MBeanServer for an attribute.
156      *
157      * @param on The ObjectName of the MBean being queried
158      * @param attrName The name of the attribute whose value is to be
159      * returned
160      *
161      * @return The value corresponding to the given attribute name
162      */

163     private Object JavaDoc queryStatistic(ObjectName JavaDoc on, String JavaDoc attrName) {
164
165         Object JavaDoc resultObj = null;
166         try {
167             resultObj = server.getAttribute(on, attrName);
168         } catch (Throwable JavaDoc t) {
169             MonitorUtil.log(Level.SEVERE,
170                             "pwc.monitoring.queryError",
171                             new Object JavaDoc[] { attrName, on },
172                             t);
173         }
174
175         return resultObj;
176     }
177
178     
179     /*
180      * Constructs the WebModule component of the Servlet MBean ObjectName.
181      *
182      * @param vsId The id of the virtual server on which the webmodule has been
183      * deployed
184      * @param contextPath The contextPath of the webmodule
185      *
186      * @return WebModule component of the Servlet MBean ObjectName
187      */

188     private String JavaDoc createTomcatWebModuleName(String JavaDoc vsId, String JavaDoc contextPath) {
189
190         final String JavaDoc PREFIX = "//";
191         String JavaDoc tcWebModuleName;
192
193         if ((contextPath != null) && (!contextPath.equals(""))) {
194             tcWebModuleName = PREFIX + vsId + contextPath;
195         } else {
196             tcWebModuleName = PREFIX + vsId + "/";
197         }
198
199         return tcWebModuleName;
200     }
201
202     
203     private long getLongValue(Object JavaDoc resultObj) {
204
205         long result = 0;
206
207         if (resultObj != null) {
208             Long JavaDoc countObj = (Long JavaDoc)resultObj;
209             result = countObj.longValue();
210         }
211
212         return result;
213     }
214     
215
216     private int getIntValue(Object JavaDoc resultObj) {
217
218         int result = 0;
219
220         if (resultObj != null) {
221             Integer JavaDoc countObj = (Integer JavaDoc)resultObj;
222             result = countObj.intValue();
223         }
224
225         return result;
226     }
227
228
229     /*
230      * Restores this object's state from a stream.
231      *
232      * @param ois Stream from which to restore this object's state
233      */

234     private void readObject(ObjectInputStream JavaDoc ois)
235             throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
236         ois.defaultReadObject();
237
238         // Get an instance of the MBeanServer
239
ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
240         if (servers != null && !servers.isEmpty()) {
241             server = (MBeanServer JavaDoc)servers.get(0);
242         } else {
243             server = MBeanServerFactory.createMBeanServer();
244         }
245     }
246 }
247
Popular Tags