1 23 24 28 29 package com.sun.enterprise.web.monitor.impl; 30 31 import java.io.ObjectInputStream ; 32 import java.util.ArrayList ; 33 import java.util.Set ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.logging.Level ; 37 import javax.management.ObjectName ; 38 import javax.management.MBeanServerFactory ; 39 import javax.management.MBeanServer ; 40 import javax.management.Attribute ; 41 import com.sun.enterprise.web.monitor.PwcWebModuleStats; 42 43 46 public class PwcWebModuleStatsImpl implements PwcWebModuleStats { 47 48 private static final String WEB_MODULE_PREFIX = "//"; 49 private static final String LIST_SESSION_IDS = "listSessionIds"; 50 private static final String GET_SESSION = "getSession"; 51 private static final String [] STRING_PARAM = new String [] { 52 "java.lang.String" }; 53 private static final Integer ZERO_INTEGER = new Integer (0); 54 55 private ObjectName jspMonitorObjName; 56 private ObjectName sessionManagerObjName; 57 private ObjectName ctxObjName; 58 private transient MBeanServer server; 59 60 61 73 public PwcWebModuleStatsImpl(String ctxObjNameStr, 74 String ctxPath, 75 String domain, 76 String vsId, 77 String appName, 78 String serverName) { 79 80 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 82 if(servers != null && !servers.isEmpty()) { 83 server = (MBeanServer )servers.get(0); 84 } else { 85 server = MBeanServerFactory.createMBeanServer(); 86 } 87 88 if ("".equals(ctxPath)) { 89 ctxPath = "/"; 90 } 91 92 95 try { 96 ctxObjName = new ObjectName (ctxObjNameStr); 97 } catch (Throwable t) { 98 MonitorUtil.log(Level.SEVERE, 99 "pwc.monitoring.objectNameCreationError", 100 new Object [] { ctxObjNameStr }, 101 t); 102 } 103 104 107 String 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 (objNameStr); 115 } catch (Throwable t) { 116 MonitorUtil.log(Level.SEVERE, 117 "pwc.monitoring.objectNameCreationError", 118 new Object [] { objNameStr }, 119 t); 120 } 121 122 125 objNameStr = domain + ":type=Manager,path=" + ctxPath 126 + ",host=" + vsId; 127 try { 128 sessionManagerObjName = new ObjectName (objNameStr); 129 } catch (Throwable t) { 130 MonitorUtil.log(Level.SEVERE, 131 "pwc.monitoring.objectNameCreationError", 132 new Object [] { objNameStr }, 133 t); 134 } 135 136 } 137 138 139 144 public int getJspCount() { 145 return queryJspStatistic(jspMonitorObjName, "jspCount"); 146 } 147 148 149 154 public int getJspReloadCount() { 155 return queryJspStatistic(jspMonitorObjName, "jspReloadCount"); 156 } 157 158 159 164 public int getJspErrorCount() { 165 return queryJspStatistic(jspMonitorObjName, "jspErrorCount"); 166 } 167 168 169 175 public int getSessionsTotal() { 176 return getIntValue(queryStatistic(sessionManagerObjName, 177 "sessionCount")); 178 } 179 180 181 186 public int getActiveSessionsCurrent() { 187 return getIntValue(queryStatistic(sessionManagerObjName, 188 "activeSessions")); 189 } 190 191 192 198 public int getActiveSessionsHigh() { 199 return getIntValue(queryStatistic(sessionManagerObjName, 200 "maxActive")); 201 } 202 203 204 212 public int getRejectedSessionsTotal() { 213 return getIntValue(queryStatistic(sessionManagerObjName, 214 "rejectedSessions")); 215 } 216 217 218 223 public int getExpiredSessionsTotal() { 224 return getIntValue(queryStatistic(sessionManagerObjName, 225 "expiredSessions")); 226 } 227 228 229 236 public int getSessionMaxAliveTimeSeconds() { 237 return getIntValue(queryStatistic(sessionManagerObjName, 238 "sessionMaxAliveTimeSeconds")); 239 } 240 241 242 249 public int getSessionAverageAliveTimeSeconds() { 250 return getIntValue(queryStatistic(sessionManagerObjName, 251 "sessionAverageAliveTimeSeconds")); 252 } 253 254 255 261 public long getStartTimeMillis() { 262 return getLongValue(queryStatistic(ctxObjName, "startTimeMillis")); 263 } 264 265 266 273 public long getServletProcessingTimesMillis() { 274 return getLongValue(queryStatistic(ctxObjName, "processingTimeMillis")); 275 } 276 277 278 283 public String getSessionIds() { 284 285 Object resultObj = null; 286 287 try { 288 resultObj = server.invoke(sessionManagerObjName, 289 LIST_SESSION_IDS, 290 null, 291 null); 292 } catch (Throwable t) { 293 MonitorUtil.log(Level.SEVERE, 294 "pwc.monitoring.actionInvocationError", 295 new Object [] { LIST_SESSION_IDS, 296 sessionManagerObjName }, 297 t); 298 } 299 300 return (String ) resultObj; 301 } 302 303 304 316 public HashMap getSession(String id) { 317 318 Object resultObj = null; 319 320 try { 321 resultObj = server.invoke(sessionManagerObjName, 322 GET_SESSION, 323 new Object [] { id }, 324 STRING_PARAM); 325 } catch (Throwable t) { 326 if (!(t instanceof IllegalStateException )) { 328 MonitorUtil.log(Level.SEVERE, 329 "pwc.monitoring.actionInvocationError", 330 new Object [] { GET_SESSION, 331 sessionManagerObjName }, 332 t); 333 } 334 } 335 336 return (HashMap ) resultObj; 337 } 338 339 340 343 public void reset() { 344 345 resetStatistic(jspMonitorObjName, "jspReloadCount"); 347 } 348 349 350 359 private int queryJspStatistic(ObjectName on, String attrName) { 360 361 int result = 0; 362 363 370 Set jspMonitorONs = server.queryNames(on, null); 371 Iterator iter = jspMonitorONs.iterator(); 372 373 while (iter.hasNext()) { 374 375 ObjectName jspMonitorON = (ObjectName ) iter.next(); 376 Object obj = queryStatistic(jspMonitorON, attrName); 377 if (obj != null) { 378 result += getIntValue(obj); 379 } 380 } 381 382 return result; 383 } 384 385 386 395 private Object queryStatistic(ObjectName on, String attrName) { 396 397 Object resultObj = null; 398 try { 399 resultObj = server.getAttribute(on, attrName); 400 } catch (Throwable t) { 401 MonitorUtil.log(Level.SEVERE, 402 "pwc.monitoring.queryError", 403 new Object [] { attrName, on }, 404 t); 405 } 406 407 return resultObj; 408 } 409 410 411 418 private void resetStatistic(ObjectName on, String attrName) { 419 420 Attribute attr = new Attribute (attrName, ZERO_INTEGER); 421 try { 422 server.setAttribute(on, attr); 423 } catch (Throwable t) { 424 MonitorUtil.log(Level.SEVERE, 425 "pwc.monitoring.resetError", 426 new Object [] { attrName, on }, 427 t); 428 } 429 } 430 431 432 437 private void readObject(ObjectInputStream ois) 438 throws java.io.IOException , ClassNotFoundException { 439 440 ois.defaultReadObject(); 441 442 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 444 if (servers != null && !servers.isEmpty()) { 445 server = (MBeanServer )servers.get(0); 446 } else { 447 server = MBeanServerFactory.createMBeanServer(); 448 } 449 } 450 451 452 private long getLongValue(Object resultObj) { 453 454 long result = 0; 455 456 if (resultObj != null) { 457 Long countObj = (Long )resultObj; 458 result = countObj.longValue(); 459 } 460 461 return result; 462 } 463 464 465 private int getIntValue(Object resultObj) { 466 467 int result = 0; 468 469 if (resultObj != null) { 470 Integer countObj = (Integer )resultObj; 471 result = countObj.intValue(); 472 } 473 474 return result; 475 } 476 477 } 478 | Popular Tags |