KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > stats > StatsUtil


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 package com.sun.enterprise.web.stats;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import com.sun.logging.LogDomains;
32
33 /**
34  * Utility class for retrieving and manipulating stats.
35  */

36 public class StatsUtil {
37
38     private static Logger JavaDoc _logger =
39         LogDomains.getLogger(LogDomains.WEB_LOGGER);
40
41
42     /**
43      * Queries the MBean with the given object name for the value of the
44      * attribute with the given name.
45      *
46      * @param server MBean server
47      * @param on MBean object name
48      * @param attrName Attribute name
49      *
50      * @return Attribute value
51      */

52     static Object JavaDoc getStatistic(MBeanServer JavaDoc server, ObjectName JavaDoc on,
53                                  String JavaDoc attrName) {
54
55         Object JavaDoc resultObj = null;
56
57         try {
58             resultObj = server.getAttribute(on, attrName);
59         } catch (Throwable JavaDoc t) {
60             String JavaDoc msg = _logger.getResourceBundle().getString(
61                                             "webcontainer.mbeanQueryError");
62             msg = MessageFormat.format(msg, new Object JavaDoc[] { attrName, on });
63             _logger.log(Level.WARNING, msg, t);
64         }
65
66         return resultObj;
67     }
68
69   
70     /**
71      * Queries the first MBeans corresponding to the given (wildcard)
72      * object name for the value of the attribute with the given name, and
73      * returns it.
74      *
75      * This method assumes that the given attribute name has the same value
76      * for all MBeans corresponding to the given wildcard object name.
77      *
78      * @param server MBean server
79      * @param on MBean object name
80      * @param attrName Attribute name
81      *
82      * @return Attribute values
83      */

84     static int getConstant(MBeanServer JavaDoc server, ObjectName JavaDoc on,
85                            String JavaDoc attrName) {
86
87     int result = 0;
88
89         Iterator JavaDoc iter = server.queryNames(on, null).iterator();
90         if (iter.hasNext()) {
91             Object JavaDoc obj = StatsUtil.getStatistic(server,
92                                                 (ObjectName JavaDoc) iter.next(),
93                                                 attrName);
94             result = getIntValue(obj);
95         }
96
97         return result;
98     }
99
100
101     /**
102      * Queries the MBeans corresponding to the given (wildcard) object name
103      * for the value of the attribute with the given name, and returns the
104      * aggregated attribute values.
105      *
106      * @param server MBean server
107      * @param on MBean object name
108      * @param attrName Attribute name
109      *
110      * @return Aggregated attribute values
111      */

112     static int getAggregateStatistic(MBeanServer JavaDoc server, ObjectName JavaDoc on,
113                                      String JavaDoc attrName) {
114
115     int result = 0;
116
117         Iterator JavaDoc iter = server.queryNames(on, null).iterator();
118         while (iter.hasNext()) {
119             Object JavaDoc obj = StatsUtil.getStatistic(server,
120                                                 (ObjectName JavaDoc) iter.next(),
121                                                 attrName);
122             if (obj != null) {
123                 result += getIntValue(obj);
124             }
125         }
126
127         return result;
128     }
129     
130     
131     /**
132      * Queries the MBeans corresponding to the given (wildcard) object name
133      * for the value of the attribute with the given name, and returns the
134      * aggregated attribute values.
135      *
136      * @param server MBean server
137      * @param on MBean object name
138      * @param attrName Attribute name
139      *
140      * @return Aggregated attribute values
141      */

142     static long getAggregateLongStatistic(MBeanServer JavaDoc server, ObjectName JavaDoc on,
143                                           String JavaDoc attrName) {
144
145     long result = 0;
146
147         Iterator JavaDoc iter = server.queryNames(on, null).iterator();
148         while (iter.hasNext()) {
149             Object JavaDoc obj = StatsUtil.getStatistic(server,
150                                                 (ObjectName JavaDoc) iter.next(),
151                                                 attrName);
152             if (obj != null) {
153                 result += getLongValue(obj);
154             }
155         }
156
157         return result;
158     }
159
160
161     /**
162      * Queries the MBeans corresponding to the given (wildcard) object name
163      * for the value of the attribute with the given name, and returns the
164      * largest attribute value.
165      *
166      * @param server MBean server
167      * @param on MBean object name
168      * @param attrName Attribute name
169      *
170      * @return Largest attribute value
171      */

172     static int getMaxStatistic(MBeanServer JavaDoc server, ObjectName JavaDoc on,
173                                String JavaDoc attrName) {
174
175     int max = 0;
176
177         Iterator JavaDoc iter = server.queryNames(on, null).iterator();
178         while (iter.hasNext()) {
179             Object JavaDoc obj = StatsUtil.getStatistic(server,
180                                                 (ObjectName JavaDoc) iter.next(),
181                                                 attrName);
182             int result = getIntValue(obj);
183             if (result > max) {
184                 max = result;
185             }
186         }
187
188         return max;
189     }
190
191
192     /**
193      * Queries the MBeans corresponding to the given (wildcard) object name
194      * for the value of the attribute with the given name, and returns the
195      * average attribute value.
196      *
197      * @param server MBean server
198      * @param on MBean object name
199      * @param attrName Attribute name
200      *
201      * @return Average attribute value
202      */

203     static int getAverageStatistic(MBeanServer JavaDoc server, ObjectName JavaDoc on,
204                                    String JavaDoc attrName) {
205
206         int total = 0;
207         int num = 0;
208
209         Iterator JavaDoc iter = server.queryNames(on, null).iterator();
210         while (iter.hasNext()) {
211             Object JavaDoc obj = StatsUtil.getStatistic(server,
212                                                 (ObjectName JavaDoc) iter.next(),
213                                                 attrName);
214             if (obj != null) {
215                 total += getIntValue(obj);
216                 num++;
217             }
218         }
219
220         return total/num;
221     }
222
223
224     static int getIntValue(Object JavaDoc resultObj) {
225
226         int result = 0;
227
228         if (resultObj instanceof Integer JavaDoc) {
229             Integer JavaDoc countObj = (Integer JavaDoc)resultObj;
230             result = countObj.intValue();
231         }
232
233         return result;
234     }
235
236
237     static long getLongValue(Object JavaDoc resultObj) {
238
239         long result = 0;
240
241         if (resultObj instanceof Long JavaDoc) {
242             result = ((Long JavaDoc)resultObj).longValue();
243         } else if (resultObj instanceof Integer JavaDoc) {
244             result = ((Integer JavaDoc)resultObj).intValue();
245         }
246
247         return result;
248     }
249     
250     
251     /**
252      * Queries the MBeans corresponding to the given (wildcard) object name
253      * for the value of the attribute with the given name, and returns the
254      * largest attribute value.
255      *
256      * @param server MBean server
257      * @param on MBean object name
258      * @param attrName Attribute name
259      *
260      * @return Largest attribute value
261      */

262     static long getMaxLongStatistic(MBeanServer JavaDoc server, ObjectName JavaDoc on,
263                                     String JavaDoc attrName) {
264
265         long max = 0;
266
267         Iterator JavaDoc iter = server.queryNames(on, null).iterator();
268         while (iter.hasNext()) {
269             Object JavaDoc obj = StatsUtil.getStatistic(server,
270                                                 (ObjectName JavaDoc) iter.next(),
271                                                 attrName);
272             long result = getLongValue(obj);
273             if (result > max) {
274                 max = result;
275             }
276         }
277
278         return max;
279     }
280 }
281
Popular Tags