22 * CA 95054 USA or visit www.sun.com if you need additional information or |
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
23 * have any questions. |
23 * have any questions. |
24 */ |
24 */ |
25 |
25 |
26 package java.lang.management; |
26 package java.lang.management; |
|
27 import javax.management.DynamicMBean; |
27 import javax.management.MBeanServer; |
28 import javax.management.MBeanServer; |
28 import javax.management.MBeanServerConnection; |
29 import javax.management.MBeanServerConnection; |
|
30 import javax.management.MBeanServerFactory; |
29 import javax.management.MBeanServerPermission; |
31 import javax.management.MBeanServerPermission; |
|
32 import javax.management.NotificationEmitter; |
|
33 import javax.management.ObjectInstance; |
30 import javax.management.ObjectName; |
34 import javax.management.ObjectName; |
|
35 import javax.management.InstanceAlreadyExistsException; |
31 import javax.management.InstanceNotFoundException; |
36 import javax.management.InstanceNotFoundException; |
32 import javax.management.MalformedObjectNameException; |
37 import javax.management.MalformedObjectNameException; |
|
38 import javax.management.MBeanRegistrationException; |
|
39 import javax.management.NotCompliantMBeanException; |
|
40 import javax.management.StandardEmitterMBean; |
|
41 import javax.management.StandardMBean; |
|
42 import java.util.Collections; |
|
43 import java.util.ArrayList; |
33 import java.util.List; |
44 import java.util.List; |
34 import java.security.AccessController; |
45 import java.security.AccessController; |
35 import java.security.Permission; |
46 import java.security.Permission; |
36 import java.security.PrivilegedAction; |
47 import java.security.PrivilegedAction; |
|
48 import java.security.PrivilegedActionException; |
|
49 import java.security.PrivilegedExceptionAction; |
37 import javax.management.JMX; |
50 import javax.management.JMX; |
|
51 import sun.management.ManagementFactoryHelper; |
38 |
52 |
39 /** |
53 /** |
40 * The <tt>ManagementFactory</tt> class is a factory class for getting |
54 * The <tt>ManagementFactory</tt> class is a factory class for getting |
41 * managed beans for the Java platform. |
55 * managed beans for the Java platform. |
42 * This class consists of static methods each of which returns |
56 * This class consists of static methods each of which returns |
47 * <p> |
61 * <p> |
48 * An application can access a platform MXBean in the following ways: |
62 * An application can access a platform MXBean in the following ways: |
49 * <ul> |
63 * <ul> |
50 * <li><i>Direct access to an MXBean interface</i> |
64 * <li><i>Direct access to an MXBean interface</i> |
51 * <ol type="a"> |
65 * <ol type="a"> |
52 * <li>Get the MXBean instance through the static factory method |
66 * <li>Get the MXBean instance through the static factory method, |
|
67 * or the {@link #getPlatformMXBeans(Class)} method |
53 * and access the MXBean locally of the running |
68 * and access the MXBean locally of the running |
54 * virtual machine. |
69 * virtual machine. |
55 * </li> |
70 * </li> |
56 * <li>Construct an MXBean proxy instance that forwards the |
71 * <li>Construct an MXBean proxy instance that forwards the |
57 * method calls to a given {@link MBeanServer MBeanServer} by calling |
72 * method calls to a given {@link MBeanServer MBeanServer} by calling |
58 * {@link #newPlatformMXBeanProxy newPlatfromMXBeanProxy}. |
73 * the {@link #newPlatformMXBeanProxy newPlatformMXBeanProxy} method |
|
74 * or the {@link #getPlatformMXBeans(MBeanServerConnection, Class)} |
|
75 * method. |
59 * A proxy is typically constructed to remotely access |
76 * A proxy is typically constructed to remotely access |
60 * an MXBean of another running virtual machine. |
77 * an MXBean of another running virtual machine. |
61 * </li> |
78 * </li> |
62 * </ol></li> |
79 * </ol></li> |
63 * <li><i>Indirect access to an MXBean interface via MBeanServer</i> |
80 * <li><i>Indirect access to an MXBean interface via MBeanServer</i> |
81 * A platform MXBean is a <i>managed bean</i> that conforms to |
98 * A platform MXBean is a <i>managed bean</i> that conforms to |
82 * the JMX Instrumentation Specification and only uses |
99 * the JMX Instrumentation Specification and only uses |
83 * a set of basic data types described below. |
100 * a set of basic data types described below. |
84 * See <a href="../../../javax/management/MXBean.html#MXBean-spec"> |
101 * See <a href="../../../javax/management/MXBean.html#MXBean-spec"> |
85 * the specification of MXBeans</a> for details. |
102 * the specification of MXBeans</a> for details. |
|
103 * All platform MXBean interfaces extend {@link PlatformManagedObject}s |
|
104 * and new methods may be added in these interfaces |
|
105 * in future Java SE releases. |
|
106 * <p> |
86 * A JMX management application and the platform <tt>MBeanServer</tt> |
107 * A JMX management application and the platform <tt>MBeanServer</tt> |
87 * can interoperate without requiring classes for MXBean specific |
108 * can interoperate without requiring classes for MXBean specific |
88 * data types. |
109 * data types. |
89 * The data types being transmitted between the JMX connector |
110 * The data types being transmitted between the JMX connector |
90 * server and the connector client are |
111 * server and the connector client are |
366 * |
390 * |
367 * @return a {@link ClassLoadingMXBean} object for |
391 * @return a {@link ClassLoadingMXBean} object for |
368 * the Java virtual machine. |
392 * the Java virtual machine. |
369 */ |
393 */ |
370 public static ClassLoadingMXBean getClassLoadingMXBean() { |
394 public static ClassLoadingMXBean getClassLoadingMXBean() { |
371 return sun.management.ManagementFactory.getClassLoadingMXBean(); |
395 return ManagementFactoryHelper.getClassLoadingMXBean(); |
372 } |
396 } |
373 |
397 |
374 /** |
398 /** |
375 * Returns the managed bean for the memory system of |
399 * Returns the managed bean for the memory system of |
376 * the Java virtual machine. |
400 * the Java virtual machine. |
377 * |
401 * |
378 * @return a {@link MemoryMXBean} object for the Java virtual machine. |
402 * @return a {@link MemoryMXBean} object for the Java virtual machine. |
379 */ |
403 */ |
380 public static MemoryMXBean getMemoryMXBean() { |
404 public static MemoryMXBean getMemoryMXBean() { |
381 return sun.management.ManagementFactory.getMemoryMXBean(); |
405 return ManagementFactoryHelper.getMemoryMXBean(); |
382 } |
406 } |
383 |
407 |
384 /** |
408 /** |
385 * Returns the managed bean for the thread system of |
409 * Returns the managed bean for the thread system of |
386 * the Java virtual machine. |
410 * the Java virtual machine. |
387 * |
411 * |
388 * @return a {@link ThreadMXBean} object for the Java virtual machine. |
412 * @return a {@link ThreadMXBean} object for the Java virtual machine. |
389 */ |
413 */ |
390 public static ThreadMXBean getThreadMXBean() { |
414 public static ThreadMXBean getThreadMXBean() { |
391 return sun.management.ManagementFactory.getThreadMXBean(); |
415 return ManagementFactoryHelper.getThreadMXBean(); |
392 } |
416 } |
393 |
417 |
394 /** |
418 /** |
395 * Returns the managed bean for the runtime system of |
419 * Returns the managed bean for the runtime system of |
396 * the Java virtual machine. |
420 * the Java virtual machine. |
397 * |
421 * |
398 * @return a {@link RuntimeMXBean} object for the Java virtual machine. |
422 * @return a {@link RuntimeMXBean} object for the Java virtual machine. |
399 |
423 |
400 */ |
424 */ |
401 public static RuntimeMXBean getRuntimeMXBean() { |
425 public static RuntimeMXBean getRuntimeMXBean() { |
402 return sun.management.ManagementFactory.getRuntimeMXBean(); |
426 return ManagementFactoryHelper.getRuntimeMXBean(); |
403 } |
427 } |
404 |
428 |
405 /** |
429 /** |
406 * Returns the managed bean for the compilation system of |
430 * Returns the managed bean for the compilation system of |
407 * the Java virtual machine. This method returns <tt>null</tt> |
431 * the Java virtual machine. This method returns <tt>null</tt> |
410 * @return a {@link CompilationMXBean} object for the Java virtual |
434 * @return a {@link CompilationMXBean} object for the Java virtual |
411 * machine or <tt>null</tt> if the Java virtual machine has |
435 * machine or <tt>null</tt> if the Java virtual machine has |
412 * no compilation system. |
436 * no compilation system. |
413 */ |
437 */ |
414 public static CompilationMXBean getCompilationMXBean() { |
438 public static CompilationMXBean getCompilationMXBean() { |
415 return sun.management.ManagementFactory.getCompilationMXBean(); |
439 return ManagementFactoryHelper.getCompilationMXBean(); |
416 } |
440 } |
417 |
441 |
418 /** |
442 /** |
419 * Returns the managed bean for the operating system on which |
443 * Returns the managed bean for the operating system on which |
420 * the Java virtual machine is running. |
444 * the Java virtual machine is running. |
421 * |
445 * |
422 * @return an {@link OperatingSystemMXBean} object for |
446 * @return an {@link OperatingSystemMXBean} object for |
423 * the Java virtual machine. |
447 * the Java virtual machine. |
424 */ |
448 */ |
425 public static OperatingSystemMXBean getOperatingSystemMXBean() { |
449 public static OperatingSystemMXBean getOperatingSystemMXBean() { |
426 return sun.management.ManagementFactory.getOperatingSystemMXBean(); |
450 return ManagementFactoryHelper.getOperatingSystemMXBean(); |
427 } |
451 } |
428 |
452 |
429 /** |
453 /** |
430 * Returns a list of {@link MemoryPoolMXBean} objects in the |
454 * Returns a list of {@link MemoryPoolMXBean} objects in the |
431 * Java virtual machine. |
455 * Java virtual machine. |
516 Permission perm = new MBeanServerPermission("createMBeanServer"); |
540 Permission perm = new MBeanServerPermission("createMBeanServer"); |
517 sm.checkPermission(perm); |
541 sm.checkPermission(perm); |
518 } |
542 } |
519 |
543 |
520 if (platformMBeanServer == null) { |
544 if (platformMBeanServer == null) { |
521 platformMBeanServer = |
545 platformMBeanServer = MBeanServerFactory.createMBeanServer(); |
522 sun.management.ManagementFactory.createPlatformMBeanServer(); |
546 for (PlatformComponent pc : PlatformComponent.values()) { |
|
547 List<? extends PlatformManagedObject> list = |
|
548 pc.getMXBeans(pc.getMXBeanInterface()); |
|
549 for (PlatformManagedObject o : list) { |
|
550 // Each PlatformComponent represents one management |
|
551 // interface. Some MXBean may extend another one. |
|
552 // The MXBean instances for one platform component |
|
553 // (returned by pc.getMXBeans()) might be also |
|
554 // the MXBean instances for another platform component. |
|
555 // e.g. com.sun.management.GarbageCollectorMXBean |
|
556 // |
|
557 // So need to check if an MXBean instance is registered |
|
558 // before registering into the platform MBeanServer |
|
559 if (!platformMBeanServer.isRegistered(o.getObjectName())) { |
|
560 addMXBean(platformMBeanServer, o); |
|
561 } |
|
562 } |
|
563 } |
523 } |
564 } |
524 return platformMBeanServer; |
565 return platformMBeanServer; |
525 } |
566 } |
526 |
567 |
527 /** |
568 /** |
655 iae.initCause(e); |
696 iae.initCause(e); |
656 throw iae; |
697 throw iae; |
657 } |
698 } |
658 } |
699 } |
659 |
700 |
|
701 /** |
|
702 * Returns the list of platform MXBeans that implement |
|
703 * the given {@code mxbeanInterface} in the running Java |
|
704 * virtual machine. |
|
705 * The returned list may contain zero, one, or more instances. |
|
706 * The number of instances in the returned list is defined |
|
707 * in the specification of the given management interface. |
|
708 * |
|
709 * @param mxbeanInterface a management interface for a platform |
|
710 * MXBean |
|
711 * |
|
712 * @return the list of platform MXBeans that implements |
|
713 * {@code mxbeanInterface}. |
|
714 * |
|
715 * @throws IllegalArgumentException if {@code mxbeanInterface} |
|
716 * is not a management interface for the platform. |
|
717 * |
|
718 * @since 1.7 |
|
719 */ |
|
720 public static <T extends PlatformManagedObject> List<T> |
|
721 getPlatformMXBeans(Class<T> mxbeanInterface) { |
|
722 String className = mxbeanInterface.getName(); |
|
723 for (PlatformComponent component: PlatformComponent.values()) { |
|
724 // comparing the class name first instead of the Class instance |
|
725 // to avoid causing unnecessary class loading of |
|
726 // the other MXBean interfaces |
|
727 if (className.equals(component.getMXBeanInterfaceName())) { |
|
728 if (component.getMXBeanInterface() == mxbeanInterface) { |
|
729 return component.getMXBeans(mxbeanInterface); |
|
730 } |
|
731 } |
|
732 } |
|
733 throw new IllegalArgumentException(mxbeanInterface.getName() + |
|
734 " is not implemented by any of the platform MXBeans."); |
|
735 } |
|
736 |
|
737 /** |
|
738 * Returns the list of the platform MXBean proxies for |
|
739 * forwarding the method calls of the {@code mxbeanInterface} |
|
740 * through the given {@code MBeanServerConnection}. |
|
741 * The returned list may contain zero, one, or more instances. |
|
742 * The number of instances in the returned list is defined |
|
743 * in the specification of the given management interface. |
|
744 * |
|
745 * @param connection the {@code MBeanServerConnection} to forward to. |
|
746 * @param mxbeanInterface a management interface for a platform |
|
747 * MXBean |
|
748 * |
|
749 * @return the list of platform MXBean proxies for |
|
750 * forwarding the method calls of the {@code mxbeanInterface} |
|
751 * through the given {@code MBeanServerConnection}. |
|
752 * |
|
753 * @throws IllegalArgumentException if {@code mxbeanInterface} |
|
754 * is not a management interface for the platform. |
|
755 * |
|
756 * @throws java.io.IOException if a communication problem |
|
757 * occurred when accessing the {@code MBeanServerConnection}. |
|
758 * |
|
759 * @since 1.7 |
|
760 */ |
|
761 public static <T extends PlatformManagedObject> |
|
762 List<T> getPlatformMXBeans(MBeanServerConnection connection, |
|
763 Class<T> mxbeanInterface) |
|
764 throws java.io.IOException |
|
765 { |
|
766 String className = mxbeanInterface.getName(); |
|
767 for (PlatformComponent component: PlatformComponent.values()) { |
|
768 // comparing the class name first instead of the Class instance |
|
769 // to avoid causing unnecessary class loading of |
|
770 // the other MXBean interfaces |
|
771 if (className.equals(component.getMXBeanInterfaceName())) { |
|
772 if (component.getMXBeanInterface() == mxbeanInterface) { |
|
773 return component.getMXBeans(connection, |
|
774 mxbeanInterface); |
|
775 } |
|
776 } |
|
777 } |
|
778 throw new IllegalArgumentException(mxbeanInterface.getName() + |
|
779 " is not implemented by any of the platform MXBeans."); |
|
780 } |
|
781 |
|
782 /** |
|
783 * Returns a list of {@code Class} objects, subinterface of |
|
784 * {@link PlatformManagedObject}, representing |
|
785 * all management interfaces for |
|
786 * monitoring and managing the Java platform. |
|
787 * |
|
788 * @return a list of {@code Class} objects, subinterface of |
|
789 * {@link PlatformManagedObject} representing |
|
790 * the management interfaces for |
|
791 * monitoring and managing the Java platform. |
|
792 * |
|
793 * @since 1.7 |
|
794 */ |
|
795 public static List<Class<? extends PlatformManagedObject>> getAllPlatformMXBeanInterfaces() { |
|
796 List<Class<? extends PlatformManagedObject>> result = |
|
797 new ArrayList<Class<? extends PlatformManagedObject>>(); |
|
798 for (PlatformComponent component: PlatformComponent.values()) { |
|
799 result.add(component.getMXBeanInterface()); |
|
800 } |
|
801 return result; |
|
802 } |
|
803 |
660 private static final String NOTIF_EMITTER = |
804 private static final String NOTIF_EMITTER = |
661 "javax.management.NotificationEmitter"; |
805 "javax.management.NotificationEmitter"; |
|
806 |
|
807 /** |
|
808 * Registers an MXBean. |
|
809 */ |
|
810 private static void addMXBean(final MBeanServer mbs, final PlatformManagedObject pmo) { |
|
811 // Make DynamicMBean out of MXBean by wrapping it with a StandardMBean |
|
812 final DynamicMBean dmbean; |
|
813 if (pmo instanceof NotificationEmitter) { |
|
814 dmbean = new StandardEmitterMBean(pmo, null, true, (NotificationEmitter) pmo); |
|
815 } else { |
|
816 dmbean = new StandardMBean(pmo, null, true); |
|
817 } |
|
818 |
|
819 try { |
|
820 AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { |
|
821 public Void run() throws InstanceAlreadyExistsException, |
|
822 MBeanRegistrationException, |
|
823 NotCompliantMBeanException { |
|
824 mbs.registerMBean(dmbean, pmo.getObjectName()); |
|
825 return null; |
|
826 } |
|
827 }); |
|
828 } catch (PrivilegedActionException e) { |
|
829 throw new RuntimeException(e.getException()); |
|
830 } |
|
831 } |
|
832 |
662 } |
833 } |