1 7 8 package java.lang.management; 9 10 import javax.management.NotificationEmitter ; 11 import javax.management.MBeanServer ; 12 import javax.management.MBeanServerFactory ; 13 import javax.management.MBeanServerConnection ; 14 import javax.management.MBeanServerPermission ; 15 import javax.management.ObjectName ; 16 import javax.management.InstanceNotFoundException ; 17 import javax.management.MalformedObjectNameException ; 18 import javax.management.openmbean.CompositeData ; 19 import javax.management.openmbean.CompositeType ; 20 import javax.management.openmbean.TabularData ; 21 import java.util.List ; 22 import java.lang.reflect.Proxy ; 23 import java.lang.reflect.InvocationHandler ; 24 import java.security.AccessController ; 25 import java.security.Permission ; 26 import java.security.PrivilegedAction ; 27 import sun.management.PlatformMXBeanInvocationHandler; 28 29 279 public class ManagementFactory { 280 private ManagementFactory() {}; 282 283 287 public final static String CLASS_LOADING_MXBEAN_NAME = 288 "java.lang:type=ClassLoading"; 289 290 294 public final static String COMPILATION_MXBEAN_NAME = 295 "java.lang:type=Compilation"; 296 297 301 public final static String MEMORY_MXBEAN_NAME = 302 "java.lang:type=Memory"; 303 304 308 public final static String OPERATING_SYSTEM_MXBEAN_NAME = 309 "java.lang:type=OperatingSystem"; 310 311 315 public final static String RUNTIME_MXBEAN_NAME = 316 "java.lang:type=Runtime"; 317 318 322 public final static String THREAD_MXBEAN_NAME = 323 "java.lang:type=Threading"; 324 325 332 public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE = 333 "java.lang:type=GarbageCollector"; 334 335 342 public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE= 343 "java.lang:type=MemoryManager"; 344 345 352 public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE= 353 "java.lang:type=MemoryPool"; 354 355 362 public static ClassLoadingMXBean getClassLoadingMXBean() { 363 return sun.management.ManagementFactory.getClassLoadingMXBean(); 364 } 365 366 372 public static MemoryMXBean getMemoryMXBean() { 373 return sun.management.ManagementFactory.getMemoryMXBean(); 374 } 375 376 382 public static ThreadMXBean getThreadMXBean() { 383 return sun.management.ManagementFactory.getThreadMXBean(); 384 } 385 386 393 public static RuntimeMXBean getRuntimeMXBean() { 394 return sun.management.ManagementFactory.getRuntimeMXBean(); 395 } 396 397 406 public static CompilationMXBean getCompilationMXBean() { 407 return sun.management.ManagementFactory.getCompilationMXBean(); 408 } 409 410 417 public static OperatingSystemMXBean getOperatingSystemMXBean() { 418 return sun.management.ManagementFactory.getOperatingSystemMXBean(); 419 } 420 421 430 public static List <MemoryPoolMXBean > getMemoryPoolMXBeans() { 431 return sun.management.ManagementFactory.getMemoryPoolMXBeans(); 432 } 433 434 443 public static List <MemoryManagerMXBean > getMemoryManagerMXBeans() { 444 return sun.management.ManagementFactory.getMemoryManagerMXBeans(); 445 } 446 447 448 459 public static List <GarbageCollectorMXBean > getGarbageCollectorMXBeans() { 460 return sun.management.ManagementFactory.getGarbageCollectorMXBeans(); 461 } 462 463 private static MBeanServer platformMBeanServer; 464 504 public static synchronized MBeanServer getPlatformMBeanServer() { 505 SecurityManager sm = System.getSecurityManager(); 506 if (sm != null) { 507 Permission perm = new MBeanServerPermission ("createMBeanServer"); 508 sm.checkPermission(perm); 509 } 510 511 if (platformMBeanServer == null) { 512 platformMBeanServer = 513 sun.management.ManagementFactory.createPlatformMBeanServer(); 514 } 515 return platformMBeanServer; 516 } 517 518 601 public static <T> T 602 newPlatformMXBeanProxy(MBeanServerConnection connection, 603 String mxbeanName, 604 Class <T> mxbeanInterface) 605 throws java.io.IOException { 606 607 final Class interfaceClass = mxbeanInterface; 608 final ClassLoader loader = (ClassLoader ) 611 AccessController.doPrivileged(new PrivilegedAction () { 612 public Object run() { 613 return interfaceClass.getClassLoader(); 614 } 615 }); 616 if (loader != null) { 617 throw new IllegalArgumentException (mxbeanName + 618 " is not a platform MXBean"); 619 } 620 621 try { 622 final ObjectName objName = new ObjectName (mxbeanName); 623 if (!connection.isInstanceOf(objName, interfaceClass.getName())) { 624 throw new IllegalArgumentException (mxbeanName + 625 " is not an instance of " + interfaceClass); 626 } 627 628 final Class [] interfaces; 629 if (connection.isInstanceOf(objName, 631 NOTIF_EMITTER)) { 632 interfaces = new Class [] { 633 interfaceClass, 634 NotificationEmitter .class 635 }; 636 } else { 637 interfaces = new Class [] {interfaceClass}; 638 } 639 640 InvocationHandler handler = 642 new PlatformMXBeanInvocationHandler(connection, 643 objName, 644 interfaceClass); 645 return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), 646 interfaces, 647 handler); 648 } catch (InstanceNotFoundException e) { 649 final IllegalArgumentException iae = 650 new IllegalArgumentException (mxbeanName + 651 " not found in the connection."); 652 iae.initCause(e); 653 throw iae; 654 } catch (MalformedObjectNameException e) { 655 final IllegalArgumentException iae = 656 new IllegalArgumentException (mxbeanName + 657 " is not a valid ObjectName format."); 658 iae.initCause(e); 659 throw iae; 660 } 661 } 662 663 private static final String NOTIF_EMITTER = 664 "javax.management.NotificationEmitter"; 665 } 666 | Popular Tags |