1 29 30 package com.caucho.management.j2ee; 31 32 import com.caucho.jmx.IntrospectionMBean; 33 import com.caucho.jmx.Jmx; 34 import com.caucho.server.host.Host; 35 import com.caucho.server.webapp.WebApp; 36 import com.caucho.util.Alarm; 37 38 import javax.management.MalformedObjectNameException ; 39 import javax.management.ObjectName ; 40 import java.util.Collection ; 41 import java.util.Hashtable ; 42 import java.util.Map ; 43 import java.util.Set ; 44 import java.util.TreeSet ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 51 abstract public class J2EEManagedObject { 52 private static final Logger log 53 = Logger.getLogger(J2EEManagedObject.class.getName()); 54 55 private static final String [] CONTEXT_KEYS = { 56 "J2EEServer", 57 "Host", 58 "J2EEApplication", 59 "WebModule" 60 }; 61 62 private final long _startTime; 63 64 protected ObjectName _objectName; 65 66 public J2EEManagedObject() 67 { 68 _startTime = Alarm.getCurrentTime(); 69 } 70 71 public String getObjectName() 72 { 73 return createObjectName().getCanonicalName(); 74 } 75 76 ObjectName createObjectName() 77 { 78 if (_objectName == null) { 79 Hashtable <String ,String > properties = new Hashtable <String , String >(); 80 81 try { 82 _objectName = createObjectName(properties); 83 } 84 catch (MalformedObjectNameException ex) { 85 if (log.isLoggable(Level.FINE)) { 86 StringBuilder builder = new StringBuilder (); 87 88 builder.append('\''); 89 for (Map.Entry <String ,String > entry : properties.entrySet()) { 90 if (builder.length() > 0) 91 builder.append(','); 92 93 builder.append(entry.getKey()); 94 builder.append('='); 95 builder.append(entry.getValue()); 96 } 97 98 builder.append("' "); 99 builder.append(ex.toString()); 100 101 log.log(Level.FINE, builder.toString(), ex); 102 } 103 } 104 } 105 106 return _objectName; 107 } 108 109 114 abstract protected String getName(); 115 116 122 protected boolean isJ2EEServer() 123 { 124 return true; 125 } 126 127 133 protected boolean isJ2EEApplication() 134 { 135 return true; 136 } 137 138 protected String quote(String value) 139 { 140 if (value == null) 141 return "null"; 142 else if (value.length() == 0) 143 return "default"; 144 else { 145 for (int i = 0; i < value.length(); i++) { 146 char ch = value.charAt(i); 147 148 switch (ch) { 149 case ',': 150 case '=': 151 case '?': 152 case '"': 153 case ':': 154 return ObjectName.quote(value); 155 } 156 } 157 158 return value; 159 } 160 } 161 162 165 protected ObjectName createObjectName(Hashtable <String ,String > properties) 166 throws MalformedObjectNameException 167 { 168 WebApp webApp = WebApp.getLocal(); 169 170 if (webApp != null) { 171 String contextPath = webApp.getContextPath(); 172 173 if (contextPath == null || contextPath.length() == 0) 174 contextPath = "/"; 175 176 properties.put("WebModule", quote(contextPath)); 177 } 178 179 Host host = Host.getLocal(); 180 181 if (isJ2EEApplication()) { 182 J2EEApplication j2eeApplication = J2EEApplication.getLocal(); 183 184 if (j2eeApplication == null) 185 properties.put("J2EEApplication", quote("null")); 186 else 187 properties.put("J2EEApplication", quote(j2eeApplication.getName())); 188 } 189 190 if (host != null) 191 properties.put("Host", quote(host.getName())); 192 193 if (isJ2EEServer()) { 194 J2EEServer j2eeServer = J2EEServer.getLocal(); 195 196 if (j2eeServer != null) 197 properties.put("J2EEServer", quote(j2eeServer.getName())); 198 } 199 200 String j2eeType; 201 202 String className = getClass().getName(); 203 204 int lastDot = className.lastIndexOf('.'); 205 206 j2eeType = className.substring(lastDot + 1); 207 208 properties.put("j2eeType", quote(j2eeType)); 209 210 String name = getName(); 211 212 if (name == null) 213 name = "null"; 214 215 properties.put("name", quote(name)); 216 217 return new ObjectName ("j2ee", properties); 218 } 219 220 226 protected String [] queryObjectNames(String ... pattern) 227 { 228 TreeSet <String > objectNames = new TreeSet <String >(); 229 230 queryObjectNames(objectNames, pattern); 231 232 return objectNames.toArray(new String [objectNames.size()]); 233 } 234 235 240 protected String [] queryObjectNamesSet(String [][] patterns) 241 { 242 TreeSet <String > objectNames = new TreeSet <String >(); 243 244 for (String [] pattern : patterns) { 245 queryObjectNames(objectNames, pattern); 246 } 247 248 return objectNames.toArray(new String [objectNames.size()]); 249 } 250 251 private void queryObjectNames(Collection <String > objectNames, String [] pattern) 252 { 253 try { 254 StringBuilder patternBuilder = new StringBuilder (); 255 256 patternBuilder.append("j2ee:"); 257 258 int length = pattern.length; 259 260 for (int i = 0; i < length; i++) { 261 if (i != 0) 262 patternBuilder.append(','); 263 264 String key = pattern[i]; 265 String value = pattern[++i]; 266 267 patternBuilder.append(key); 268 patternBuilder.append('='); 269 patternBuilder.append(quote(value)); 270 } 271 272 for (String contextKey : CONTEXT_KEYS) { 273 if (patternBuilder.indexOf(contextKey) >= 0) 274 continue; 275 276 String value = _objectName.getKeyProperty(contextKey); 277 278 if (value != null) { 279 patternBuilder.append(','); 280 patternBuilder.append(contextKey); 281 patternBuilder.append('='); 282 patternBuilder.append(quote(value)); 283 } 284 } 285 286 patternBuilder.append(",*"); 287 288 ObjectName queryObjectName = new ObjectName (patternBuilder.toString()); 289 290 Set <ObjectName > matchingObjectNames 291 = Jmx.getGlobalMBeanServer().queryNames(queryObjectName, null); 292 293 for (ObjectName matchingObjectName : matchingObjectNames) 294 objectNames.add(matchingObjectName.getCanonicalName()); 295 } 296 catch (Exception ex) { 297 if (log.isLoggable(Level.FINE)) 298 log.log(Level.FINE, ex.toString(), ex); 299 } 300 } 301 302 305 public boolean isStateManageable() 306 { 307 return (this instanceof StateManageable); 308 } 309 310 313 public boolean isStatisticsProvider() 314 { 315 return (this instanceof StatisticsProvider); 316 } 317 318 321 public boolean isEventProvider() 322 { 323 return (this instanceof EventProvider); 324 } 325 326 long getStartTime() 327 { 328 return _startTime; 329 } 330 331 338 public static <T extends J2EEManagedObject> T register(T managedObject) 339 { 340 341 if (managedObject == null) 342 return null; 343 344 ObjectName objectName = null; 345 346 try { 347 objectName = managedObject.createObjectName(); 348 349 Object mbean = new IntrospectionMBean(managedObject, managedObject.getClass(), true); 350 351 if (false) { 353 if (objectName != null) 354 Jmx.register(mbean, objectName); 355 } 356 357 return managedObject; 358 } 359 catch (Exception ex) { 360 if (log.isLoggable(Level.FINE)) 361 log.log(Level.FINE, managedObject.getClass() + " " + objectName + " " + ex.toString(), ex); 362 363 return null; 364 } 365 } 366 367 375 public static void unregister(J2EEManagedObject managedObject) 376 { 377 if (managedObject == null) 378 return; 379 380 ObjectName objectName = null; 381 382 try { 383 objectName = managedObject.createObjectName(); 384 385 if (false) { 387 Jmx.unregister(objectName); 388 } 389 } 390 catch (Throwable ex) { 391 if (log.isLoggable(Level.FINEST)) 392 log.log(Level.FINEST, managedObject.getClass() + " " + objectName + " " + ex.toString(), ex); 393 } 394 } 395 } 396 | Popular Tags |