1 7 8 package javax.management; 9 10 import java.security.AccessController ; 12 import java.security.Permission ; 13 import java.security.PrivilegedAction ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 17 import javax.management.loading.ClassLoaderRepository ; 19 20 import com.sun.jmx.defaults.ServiceName; 21 import com.sun.jmx.defaults.JmxProperties; 22 import com.sun.jmx.mbeanserver.GetPropertyAction; 23 import com.sun.jmx.trace.Trace; 24 25 72 public class MBeanServerFactory { 73 74 78 private MBeanServerFactory() { 79 80 } 81 82 87 private static MBeanServerBuilder builder = null; 88 89 101 107 118 123 139 public static void releaseMBeanServer(MBeanServer mbeanServer) { 140 checkPermission("releaseMBeanServer"); 141 142 removeMBeanServer(mbeanServer); 143 } 144 145 179 public static MBeanServer createMBeanServer() { 180 return createMBeanServer(null); 181 } 182 183 216 public static MBeanServer createMBeanServer(String domain) { 217 checkPermission("createMBeanServer"); 218 219 final MBeanServer mBeanServer = newMBeanServer(domain); 220 addMBeanServer(mBeanServer); 221 return mBeanServer; 222 } 223 224 260 public static MBeanServer newMBeanServer() { 261 return newMBeanServer(null); 262 } 263 264 299 public static MBeanServer newMBeanServer(String domain) { 300 checkPermission("newMBeanServer"); 301 302 final MBeanServerBuilder mbsBuilder = getNewMBeanServerBuilder(); 305 307 synchronized(mbsBuilder) { 308 final MBeanServerDelegate delegate = 309 mbsBuilder.newMBeanServerDelegate(); 310 if (delegate == null) { 311 final String msg = 312 "MBeanServerBuilder.newMBeanServerDelegate() " + 313 "returned null"; 314 throw new JMRuntimeException (msg); 315 } 316 final MBeanServer mbeanServer = 317 mbsBuilder.newMBeanServer(domain,null,delegate); 318 if (mbeanServer == null) { 319 final String msg = 320 "MBeanServerBuilder.newMBeanServer() returned null"; 321 throw new JMRuntimeException (msg); 322 } 323 return mbeanServer; 324 } 325 } 326 327 346 public synchronized static ArrayList findMBeanServer(String agentId) { 347 checkPermission("findMBeanServer"); 348 349 if (agentId == null) 350 return (ArrayList ) mBeanServerList.clone(); 351 352 ArrayList result = new ArrayList (); 353 for (Iterator i = mBeanServerList.iterator(); i.hasNext(); ) { 354 MBeanServer mbs = (MBeanServer ) i.next(); 355 String name = mBeanServerName(mbs); 356 if (agentId.equals(name)) 357 result.add(mbs); 358 } 359 return result; 360 } 361 362 379 public static ClassLoaderRepository getClassLoaderRepository( 380 MBeanServer server) { 381 return server.getClassLoaderRepository(); 382 } 383 384 private static final ObjectName delegateName; 385 static { 386 ObjectName name; 387 try { 388 name = new ObjectName (ServiceName.DELEGATE); 389 } catch (JMException e) { 390 392 name = null; 393 trace("<clinit>", 394 "internal error creating delegate ObjectName: " + e); 395 } 396 delegateName = name; 397 } 398 399 private static String mBeanServerName(MBeanServer mbs) { 400 try { 401 return (String ) mbs.getAttribute(delegateName, "MBeanServerId"); 402 } catch (JMException e) { 403 return null; 404 } 405 } 406 407 private static void checkPermission(String action) 408 throws SecurityException { 409 SecurityManager sm = System.getSecurityManager(); 410 if (sm != null) { 411 Permission perm = new MBeanServerPermission (action); 412 sm.checkPermission(perm); 413 } 414 } 415 416 private static synchronized void addMBeanServer(MBeanServer mbs) { 417 mBeanServerList.add(mbs); 418 } 419 420 private static synchronized void removeMBeanServer(MBeanServer mbs) { 421 boolean removed = mBeanServerList.remove(mbs); 422 if (!removed) { 423 trace("removeMBeanServer", "MBeanServer was not in list!"); 424 throw new IllegalArgumentException ("MBeanServer was not in list!"); 425 } 426 } 427 428 private static final ArrayList mBeanServerList = new ArrayList (); 429 430 434 private static Class loadBuilderClass(String builderClassName) 435 throws ClassNotFoundException { 436 final ClassLoader loader = 437 Thread.currentThread().getContextClassLoader(); 438 439 if (loader != null) { 440 return loader.loadClass(builderClassName); 442 } 443 444 return Class.forName(builderClassName); 446 } 447 448 454 private static MBeanServerBuilder newBuilder(Class builderClass) { 455 try { 456 final Object builder = builderClass.newInstance(); 457 return (MBeanServerBuilder )builder; 458 } catch (RuntimeException x) { 459 throw x; 460 } catch (Exception x) { 461 final String msg = 462 "Failed to instantiate a MBeanServerBuilder from " + 463 builderClass + ": " + x; 464 throw new JMRuntimeException (msg, x); 465 } 466 } 467 468 472 private static synchronized void checkMBeanServerBuilder() { 473 try { 474 PrivilegedAction act = 475 new GetPropertyAction(JmxProperties.JMX_INITIAL_BUILDER); 476 String builderClassName = (String ) 477 AccessController.doPrivileged(act); 478 479 try { 480 final Class newBuilderClass; 481 if (builderClassName == null || builderClassName.length() == 0) 482 newBuilderClass = MBeanServerBuilder .class; 483 else 484 newBuilderClass = loadBuilderClass(builderClassName); 485 486 if (builder != null) { 488 final Class builderClass = builder.getClass(); 489 if (newBuilderClass == builderClass) 490 return; } 492 493 builder = newBuilder(newBuilderClass); 495 } catch (ClassNotFoundException x) { 496 final String msg = 497 "Failed to load MBeanServerBuilder class " + 498 builderClassName + ": " + x; 499 throw new JMRuntimeException (msg, x); 500 } 501 } catch (RuntimeException x) { 502 debug("checkMBeanServerBuilder", 503 "Failed to instantiate MBeanServerBuilder: " + x + 504 "\n\t\tCheck the value of the " + 505 JmxProperties.JMX_INITIAL_BUILDER + " property." ); 506 throw x; 507 } 508 } 509 510 529 private static synchronized MBeanServerBuilder getNewMBeanServerBuilder() { 530 checkMBeanServerBuilder(); 531 return builder; 532 } 533 534 535 private static void trace(String method, String message) { 536 if (Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER)) { 537 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, 538 MBeanServerFactory .class.getName(), method, message); 539 } 540 } 541 542 private static void debug(String method, String message) { 543 if (Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER)) { 544 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, 545 MBeanServerFactory .class.getName(), method, message); 546 } 547 } 548 549 private static void error(String method, String message) { 550 if (Trace.isSelected(Trace.LEVEL_ERROR, Trace.INFO_MBEANSERVER)) { 551 Trace.send(Trace.LEVEL_ERROR, Trace.INFO_MBEANSERVER, 552 MBeanServerFactory .class.getName(), method, message); 553 } 554 } 555 } 556 | Popular Tags |