1 23 package com.sun.enterprise.web.stats; 24 25 import java.util.Iterator ; 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 import java.text.MessageFormat ; 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 import com.sun.logging.LogDomains; 32 33 36 public class StatsUtil { 37 38 private static Logger _logger = 39 LogDomains.getLogger(LogDomains.WEB_LOGGER); 40 41 42 52 static Object getStatistic(MBeanServer server, ObjectName on, 53 String attrName) { 54 55 Object resultObj = null; 56 57 try { 58 resultObj = server.getAttribute(on, attrName); 59 } catch (Throwable t) { 60 String msg = _logger.getResourceBundle().getString( 61 "webcontainer.mbeanQueryError"); 62 msg = MessageFormat.format(msg, new Object [] { attrName, on }); 63 _logger.log(Level.WARNING, msg, t); 64 } 65 66 return resultObj; 67 } 68 69 70 84 static int getConstant(MBeanServer server, ObjectName on, 85 String attrName) { 86 87 int result = 0; 88 89 Iterator iter = server.queryNames(on, null).iterator(); 90 if (iter.hasNext()) { 91 Object obj = StatsUtil.getStatistic(server, 92 (ObjectName ) iter.next(), 93 attrName); 94 result = getIntValue(obj); 95 } 96 97 return result; 98 } 99 100 101 112 static int getAggregateStatistic(MBeanServer server, ObjectName on, 113 String attrName) { 114 115 int result = 0; 116 117 Iterator iter = server.queryNames(on, null).iterator(); 118 while (iter.hasNext()) { 119 Object obj = StatsUtil.getStatistic(server, 120 (ObjectName ) iter.next(), 121 attrName); 122 if (obj != null) { 123 result += getIntValue(obj); 124 } 125 } 126 127 return result; 128 } 129 130 131 142 static long getAggregateLongStatistic(MBeanServer server, ObjectName on, 143 String attrName) { 144 145 long result = 0; 146 147 Iterator iter = server.queryNames(on, null).iterator(); 148 while (iter.hasNext()) { 149 Object obj = StatsUtil.getStatistic(server, 150 (ObjectName ) iter.next(), 151 attrName); 152 if (obj != null) { 153 result += getLongValue(obj); 154 } 155 } 156 157 return result; 158 } 159 160 161 172 static int getMaxStatistic(MBeanServer server, ObjectName on, 173 String attrName) { 174 175 int max = 0; 176 177 Iterator iter = server.queryNames(on, null).iterator(); 178 while (iter.hasNext()) { 179 Object obj = StatsUtil.getStatistic(server, 180 (ObjectName ) 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 203 static int getAverageStatistic(MBeanServer server, ObjectName on, 204 String attrName) { 205 206 int total = 0; 207 int num = 0; 208 209 Iterator iter = server.queryNames(on, null).iterator(); 210 while (iter.hasNext()) { 211 Object obj = StatsUtil.getStatistic(server, 212 (ObjectName ) 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 resultObj) { 225 226 int result = 0; 227 228 if (resultObj instanceof Integer ) { 229 Integer countObj = (Integer )resultObj; 230 result = countObj.intValue(); 231 } 232 233 return result; 234 } 235 236 237 static long getLongValue(Object resultObj) { 238 239 long result = 0; 240 241 if (resultObj instanceof Long ) { 242 result = ((Long )resultObj).longValue(); 243 } else if (resultObj instanceof Integer ) { 244 result = ((Integer )resultObj).intValue(); 245 } 246 247 return result; 248 } 249 250 251 262 static long getMaxLongStatistic(MBeanServer server, ObjectName on, 263 String attrName) { 264 265 long max = 0; 266 267 Iterator iter = server.queryNames(on, null).iterator(); 268 while (iter.hasNext()) { 269 Object obj = StatsUtil.getStatistic(server, 270 (ObjectName ) 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 |