1 23 24 28 29 package com.sun.enterprise.web.monitor.impl; 30 31 import java.io.ObjectInputStream ; 32 import java.util.Hashtable ; 33 import java.util.ArrayList ; 34 import java.util.logging.Level ; 35 import javax.management.ObjectName ; 36 import javax.management.MBeanServerFactory ; 37 import javax.management.MBeanServer ; 38 import com.sun.enterprise.web.monitor.PwcServletStats; 39 40 41 public class PwcServletStatsImpl implements PwcServletStats { 42 43 private transient MBeanServer server; 44 private ObjectName servletObjName; 45 46 47 68 public PwcServletStatsImpl(String domain, 69 String vsId, 70 String contextPath, 71 String servletName, 72 String appName, 73 String serverName) { 74 75 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 77 if(servers != null && !servers.isEmpty()) 78 server = (MBeanServer )servers.get(0); 79 else 80 server = MBeanServerFactory.createMBeanServer(); 81 82 String 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 (objNameStr); 91 } catch (Throwable t) { 92 MonitorUtil.log(Level.SEVERE, 93 "pwc.monitoring.objectNameCreationError", 94 new Object [] { objNameStr }, 95 t); 96 } 97 } 98 99 100 106 public long getMaxTimeMillis() { 107 return getLongValue(queryStatistic(servletObjName, "maxTimeMillis")); 108 } 109 110 111 116 public long getMinTimeMillis() { 117 return getLongValue(queryStatistic(servletObjName, "minTimeMillis")); 118 } 119 120 121 127 public long getProcessingTimeMillis() { 128 return getLongValue(queryStatistic(servletObjName, 129 "processingTimeMillis")); 130 } 131 132 133 138 public int getRequestCount() { 139 return getIntValue(queryStatistic(servletObjName, "requestCount")); 140 } 141 142 143 149 public int getErrorCount() { 150 return getIntValue(queryStatistic(servletObjName, "errorCount")); 151 } 152 153 154 163 private Object queryStatistic(ObjectName on, String attrName) { 164 165 Object resultObj = null; 166 try { 167 resultObj = server.getAttribute(on, attrName); 168 } catch (Throwable t) { 169 MonitorUtil.log(Level.SEVERE, 170 "pwc.monitoring.queryError", 171 new Object [] { attrName, on }, 172 t); 173 } 174 175 return resultObj; 176 } 177 178 179 188 private String createTomcatWebModuleName(String vsId, String contextPath) { 189 190 final String PREFIX = "//"; 191 String 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 resultObj) { 204 205 long result = 0; 206 207 if (resultObj != null) { 208 Long countObj = (Long )resultObj; 209 result = countObj.longValue(); 210 } 211 212 return result; 213 } 214 215 216 private int getIntValue(Object resultObj) { 217 218 int result = 0; 219 220 if (resultObj != null) { 221 Integer countObj = (Integer )resultObj; 222 result = countObj.intValue(); 223 } 224 225 return result; 226 } 227 228 229 234 private void readObject(ObjectInputStream ois) 235 throws java.io.IOException , ClassNotFoundException { 236 ois.defaultReadObject(); 237 238 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 240 if (servers != null && !servers.isEmpty()) { 241 server = (MBeanServer )servers.get(0); 242 } else { 243 server = MBeanServerFactory.createMBeanServer(); 244 } 245 } 246 } 247 | Popular Tags |