src/share/classes/java/lang/management/ManagementFactory.java

Print this page




  23  * questions.
  24  */
  25 
  26 package java.lang.management;
  27 import javax.management.DynamicMBean;
  28 import javax.management.MBeanServer;
  29 import javax.management.MBeanServerConnection;
  30 import javax.management.MBeanServerFactory;
  31 import javax.management.MBeanServerPermission;
  32 import javax.management.NotificationEmitter;
  33 import javax.management.ObjectInstance;
  34 import javax.management.ObjectName;
  35 import javax.management.InstanceAlreadyExistsException;
  36 import javax.management.InstanceNotFoundException;
  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;
  44 import java.util.List;


  45 import java.security.AccessController;
  46 import java.security.Permission;
  47 import java.security.PrivilegedAction;
  48 import java.security.PrivilegedActionException;
  49 import java.security.PrivilegedExceptionAction;
  50 import javax.management.JMX;
  51 import sun.management.ManagementFactoryHelper;
  52 
  53 /**
  54  * The <tt>ManagementFactory</tt> class is a factory class for getting
  55  * managed beans for the Java platform.
  56  * This class consists of static methods each of which returns
  57  * one or more <a href="#MXBean">platform MXBean(s)</a> representing
  58  * the management interface of a component of the Java virtual
  59  * machine.
















  60  *








  61  * <p>
  62  * An application can access a platform MXBean in the following ways:


  63  * <ul>
  64  * <li><i>Direct access to an MXBean interface</i>
  65  *     <ol type="a">
  66  *     <li>Get the MXBean instance through the static factory method,
  67  *         or the {@link #getPlatformMXBeans(Class)} method
  68  *         and access the MXBean locally of the running
  69  *         virtual machine.
  70  *         </li>
  71  *     <li>Construct an MXBean proxy instance that forwards the
  72  *         method calls to a given {@link MBeanServer MBeanServer} by calling
  73  *         the {@link #newPlatformMXBeanProxy newPlatformMXBeanProxy} method
  74  *         or the {@link #getPlatformMXBeans(MBeanServerConnection, Class)}
  75  *         method.


  76  *         A proxy is typically constructed to remotely access
  77  *         an MXBean of another running virtual machine.
  78  *         </li>
  79  *     </ol></li>
  80  * <li><i>Indirect access to an MXBean interface via MBeanServer</i>
  81  *     <ol type="a">
  82  *     <li>Go through the {@link #getPlatformMBeanServer
  83  *         platform MBeanServer} to access MXBeans locally or
  84  *         a specific <tt>MBeanServerConnection</tt> to access
  85  *         MXBeans remotely.
  86  *         The attributes and operations of an MXBean use only
  87  *         <em>JMX open types</em> which include basic data types,
  88  *         {@link javax.management.openmbean.CompositeData CompositeData},
  89  *         and {@link javax.management.openmbean.TabularData TabularData}
  90  *         defined in
  91  *         {@link javax.management.openmbean.OpenType OpenType}.
  92  *         The mapping is specified below.


  93  *        </li>
  94  *     </ol></li>
  95  * </ul>

  96  *
  97  * <h4><a name="MXBean">Platform MXBeans</a></h4>
  98  * A platform MXBean is a <i>managed bean</i> that conforms to
  99  * the JMX Instrumentation Specification and only uses
 100  * a set of basic data types described below.
 101  * See <a href="../../../javax/management/MXBean.html#MXBean-spec">
 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>
 107  * A JMX management application and the platform <tt>MBeanServer</tt>
 108  * can interoperate without requiring classes for MXBean specific
 109  * data types.
 110  * The data types being transmitted between the JMX connector
 111  * server and the connector client are
 112  * {@linkplain javax.management.openmbean.OpenType open types}
 113  * and this allows interoperation across versions.
 114  * <p>
 115  * The platform MXBean interfaces use only the following data types:
 116  * <ul>
 117  *   <li>Primitive types such as <tt>int</tt>, <tt>long</tt>,
 118  *       <tt>boolean</tt>, etc</li>
 119  *   <li>Wrapper classes for primitive types such as
 120  *       {@link java.lang.Integer Integer}, {@link java.lang.Long Long},
 121  *       {@link java.lang.Boolean Boolean}, etc and
 122  *       {@link java.lang.String String}</li>
 123  *   <li>{@link java.lang.Enum Enum} classes</li>
 124  *   <li>Classes that define only getter methods and define a static
 125  *       <tt>from</tt> method with a
 126  *       {@link javax.management.openmbean.CompositeData CompositeData}
 127  *       argument to convert from an input <tt>CompositeData</tt> to
 128  *       an instance of that class
 129  *       </li>
 130  *   <li>{@link java.util.List List&lt;E&gt;}
 131  *       where <tt>E</tt> is a primitive type, a wrapper class,
 132  *       an enum class, or a class supporting conversion from a
 133  *       <tt>CompositeData</tt> to its class
 134  *       </li>
 135  *   <li>{@link java.util.Map Map&lt;K,V&gt;}
 136  *       where <tt>K</tt> and <tt>V</tt> are
 137  *       a primitive type, a wrapper class,
 138  *       an enum class, or a class supporting conversion from a
 139  *       <tt>CompositeData</tt> to its class
 140  *       </li>
 141  * </ul>
 142  *
 143  * <p>
 144  * When an attribute or operation of a platform MXBean
 145  * is accessed via an <tt>MBeanServer</tt>, the data types are mapped
 146  * as follows:
 147  * <ul>
 148  *   <li>A primitive type or a wrapper class is mapped
 149  *       to the same type.
 150  *       </li>
 151  *   <li>An {@link Enum} is mapped to
 152  *       <tt>String</tt> whose value is the name of the enum constant.
 153  *   <li>A class that defines only getter methods and a static
 154  *       <tt>from</tt> method with a
 155  *       {@link javax.management.openmbean.CompositeData CompositeData}
 156  *       argument is mapped to
 157  *       {@link javax.management.openmbean.CompositeData CompositeData}.
 158  *       </li>
 159  *   <li><tt>Map&lt;K,V&gt;</tt> is mapped to
 160  *       {@link javax.management.openmbean.TabularData TabularData}
 161  *       whose row type is a
 162  *       {@link javax.management.openmbean.CompositeType CompositeType} with
 163  *       two items whose names are <i>"key"</i> and <i>"value"</i>
 164  *       and the item types are
 165  *       the corresponding mapped type of <tt>K</tt> and <tt>V</tt>
 166  *       respectively and the <i>"key"</i> is the index.
 167  *       </li>
 168  *   <li><tt>List&lt;E&gt;</tt> is mapped to an array with the mapped
 169  *       type of <tt>E</tt> as the element type.
 170  *       </li>
 171  *   <li>An array of element type <tt>E</tt> is mapped to
 172  *       an array of the same dimenions with the mapped type of <tt>E</tt>
 173  *       as the element type.</li>
 174  * </ul>
 175  *
 176  * The {@link javax.management.MBeanInfo MBeanInfo}
 177  * for a platform MXBean
 178  * describes the data types of the attributes and operations
 179  * as primitive or open types mapped as specified above.
 180  *
 181  * <p>
 182  * For example, the {@link MemoryMXBean}
 183  * interface has the following <i>getter</i> and <i>setter</i> methods:
 184  *
 185  * <blockquote><pre>
 186  * public MemoryUsage getHeapMemoryUsage();
 187  * public boolean isVerbose();
 188  * public void setVerbose(boolean value);
 189  * </pre></blockquote>
 190  *
 191  * These attributes in the <tt>MBeanInfo</tt>
 192  * of the <tt>MemoryMXBean</tt> have the following names and types:
 193  *
 194  * <blockquote>
 195  * <table border>
 196  * <tr>
 197  *   <th>Attribute Name</th>
 198  *   <th>Type</th>
 199  *   </tr>
 200  * <tr>
 201  *   <td><tt>HeapMemoryUsage</tt></td>
 202  *   <td>{@link MemoryUsage#from
 203  *              CompositeData representing MemoryUsage}</td>
 204  * </tr>
 205  * <tr>
 206  *   <td><tt>Verbose</tt></td>
 207  *   <td><tt>boolean</tt></td>
 208  * </tr>
 209  * </table>
 210  * </blockquote>
 211  *
 212  * <h4><a name="MXBeanNames">MXBean Names</a></h4>
 213  * Each platform MXBean for a Java virtual machine has a unique
 214  * {@link javax.management.ObjectName ObjectName} for
 215  * registration in the platform <tt>MBeanServer</tt> that can
 216  * be obtained by calling the {@link PlatformManagedObject#getObjectName}
 217  * method.
 218  *
 219  * A Java virtual machine has a single instance of the following management
 220  * interfaces:
 221  *
 222  * <blockquote>
 223  * <table border>
 224  * <tr>
 225  * <th>Management Interface</th>
 226  * <th>ObjectName</th>
 227  * </tr>
 228  * <tr>
 229  * <td> {@link ClassLoadingMXBean} </td>
 230  * <td> {@link #CLASS_LOADING_MXBEAN_NAME
 231  *             <tt>java.lang:type=ClassLoading</tt>}</td>
 232  * </tr>
 233  * <tr>
 234  * <td> {@link MemoryMXBean} </td>
 235  * <td> {@link #MEMORY_MXBEAN_NAME
 236  *             <tt>java.lang:type=Memory</tt>}</td>
 237  * </tr>
 238  * <tr>
 239  * <td> {@link ThreadMXBean} </td>
 240  * <td> {@link #THREAD_MXBEAN_NAME
 241  *             <tt>java.lang:type=Threading</tt>}</td>
 242  * </tr>
 243  * <tr>
 244  * <td> {@link RuntimeMXBean} </td>
 245  * <td> {@link #RUNTIME_MXBEAN_NAME
 246  *             <tt>java.lang:type=Runtime</tt>}</td>
 247  * </tr>
 248  * <tr>
 249  * <td> {@link OperatingSystemMXBean} </td>
 250  * <td> {@link #OPERATING_SYSTEM_MXBEAN_NAME
 251  *             <tt>java.lang:type=OperatingSystem</tt>}</td>
 252  * </tr>





 253  * </table>
 254  * </blockquote>
 255  *
 256  * <p>
 257  * A Java virtual machine has zero or a single instance of
 258  * the following management interfaces.
 259  *
 260  * <blockquote>
 261  * <table border>
 262  * <tr>
 263  * <th>Management Interface</th>
 264  * <th>ObjectName</th>
 265  * </tr>
 266  * <tr>
 267  * <td> {@link CompilationMXBean} </td>
 268  * <td> {@link #COMPILATION_MXBEAN_NAME
 269  *             <tt>java.lang:type=Compilation</tt>}</td>
 270  * </tr>
 271  * </table>
 272  * </blockquote>
 273  *
 274  * <p>
 275  * A Java virtual machine may have one or more instances of the following
 276  * management interfaces.
 277  * <blockquote>
 278  * <table border>
 279  * <tr>
 280  * <th>Management Interface</th>
 281  * <th>ObjectName</th>
 282  * </tr>
 283  * <tr>
 284  * <td> {@link GarbageCollectorMXBean} </td>
 285  * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
 286  *    <tt>java.lang:type=GarbageCollector</tt>}<tt>,name=</tt><i>collector's name</i></td>
 287  * </tr>
 288  * <tr>
 289  * <td> {@link MemoryManagerMXBean} </td>
 290  * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
 291  *    <tt>java.lang:type=MemoryManager</tt>}<tt>,name=</tt><i>manager's name</i></td>
 292  * </tr>
 293  * <tr>
 294  * <td> {@link MemoryPoolMXBean} </td>
 295  * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
 296  *    <tt>java.lang:type=MemoryPool</tt>}<tt>,name=</tt><i>pool's name</i></td>
 297  * </tr>




 298  * </table>
 299  * </blockquote>
 300  *
 301  * @see <a href="../../../javax/management/package-summary.html">
 302  *      JMX Specification</a>
 303  * @see <a href="package-summary.html#examples">
 304  *      Ways to Access Management Metrics</a>
 305  * @see java.util.logging.LoggingMXBean
 306  * @see javax.management.MXBean
 307  *
 308  * @author  Mandy Chung
 309  * @since   1.5
 310  */
 311 public class ManagementFactory {
 312     // A class with only static fields and methods.
 313     private ManagementFactory() {};
 314 
 315     /**
 316      * String representation of the
 317      * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
 318      */
 319     public final static String CLASS_LOADING_MXBEAN_NAME =
 320         "java.lang:type=ClassLoading";
 321 
 322     /**
 323      * String representation of the
 324      * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
 325      */


 479 
 480     /**
 481      * Returns a list of {@link GarbageCollectorMXBean} objects
 482      * in the Java virtual machine.
 483      * The Java virtual machine may have one or more
 484      * <tt>GarbageCollectorMXBean</tt> objects.
 485      * It may add or remove <tt>GarbageCollectorMXBean</tt>
 486      * during execution.
 487      *
 488      * @return a list of <tt>GarbageCollectorMXBean</tt> objects.
 489      *
 490      */
 491     public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
 492         return ManagementFactoryHelper.getGarbageCollectorMXBeans();
 493     }
 494 
 495     private static MBeanServer platformMBeanServer;
 496     /**
 497      * Returns the platform {@link javax.management.MBeanServer MBeanServer}.
 498      * On the first call to this method, it first creates the platform
 499      * <tt>MBeanServer</tt> by calling the
 500      * {@link javax.management.MBeanServerFactory#createMBeanServer
 501      * MBeanServerFactory.createMBeanServer}
 502      * method and registers the platform MXBeans in this platform
 503      * <tt>MBeanServer</tt> using the <a href="#MXBeanNames">MXBean names</a>
 504      * defined in the class description.
 505      * This method, in subsequent calls, will simply return the
 506      * initially created platform <tt>MBeanServer</tt>.
 507      * <p>
 508      * MXBeans that get created and destroyed dynamically, for example,
 509      * memory {@link MemoryPoolMXBean pools} and
 510      * {@link MemoryManagerMXBean managers},
 511      * will automatically be registered and deregistered into the platform
 512      * <tt>MBeanServer</tt>.
 513      * <p>
 514      * If the system property <tt>javax.management.builder.initial</tt>
 515      * is set, the platform <tt>MBeanServer</tt> creation will be done
 516      * by the specified {@link javax.management.MBeanServerBuilder}.
 517      * <p>
 518      * It is recommended that this platform MBeanServer also be used
 519      * to register other application managed beans
 520      * besides the platform MXBeans.
 521      * This will allow all MBeans to be published through the same
 522      * <tt>MBeanServer</tt> and hence allow for easier network publishing
 523      * and discovery.
 524      * Name conflicts with the platform MXBeans should be avoided.
 525      *
 526      * @return the platform <tt>MBeanServer</tt>; the platform
 527      *         MXBeans are registered into the platform <tt>MBeanServer</tt>
 528      *         at the first time this method is called.
 529      *
 530      * @exception SecurityException if there is a security manager
 531      * and the caller does not have the permission required by
 532      * {@link javax.management.MBeanServerFactory#createMBeanServer}.
 533      *
 534      * @see javax.management.MBeanServerFactory
 535      * @see javax.management.MBeanServerFactory#createMBeanServer
 536      */
 537     public static synchronized MBeanServer getPlatformMBeanServer() {
 538         SecurityManager sm = System.getSecurityManager();
 539         if (sm != null) {
 540             Permission perm = new MBeanServerPermission("createMBeanServer");
 541             sm.checkPermission(perm);
 542         }
 543 
 544         if (platformMBeanServer == null) {
 545             platformMBeanServer = MBeanServerFactory.createMBeanServer();
 546             for (PlatformComponent pc : PlatformComponent.values()) {
 547                 List<? extends PlatformManagedObject> list =


 654                                String mxbeanName,
 655                                Class<T> mxbeanInterface)
 656             throws java.io.IOException {
 657 
 658         final Class interfaceClass = mxbeanInterface;
 659         // Only allow MXBean interfaces from rt.jar loaded by the
 660         // bootstrap class loader
 661         final ClassLoader loader =
 662             AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
 663                 public ClassLoader run() {
 664                     return interfaceClass.getClassLoader();
 665                 }
 666             });
 667         if (loader != null) {
 668             throw new IllegalArgumentException(mxbeanName +
 669                 " is not a platform MXBean");
 670         }
 671 
 672         try {
 673             final ObjectName objName = new ObjectName(mxbeanName);
 674             if (!connection.isInstanceOf(objName, interfaceClass.getName())) {


 675                 throw new IllegalArgumentException(mxbeanName +
 676                     " is not an instance of " + interfaceClass);
 677             }
 678 
 679             final Class[] interfaces;
 680             // check if the registered MBean is a notification emitter
 681             boolean emitter = connection.isInstanceOf(objName, NOTIF_EMITTER);
 682 
 683             // create an MXBean proxy
 684             return JMX.newMXBeanProxy(connection, objName, mxbeanInterface,
 685                                       emitter);
 686         } catch (InstanceNotFoundException e) {
 687             final IllegalArgumentException iae =
 688                 new IllegalArgumentException(mxbeanName +
 689                     " not found in the connection.");
 690             iae.initCause(e);
 691             throw iae;
 692         } catch (MalformedObjectNameException e) {
 693             final IllegalArgumentException iae =
 694                 new IllegalArgumentException(mxbeanName +
 695                     " is not a valid ObjectName format.");
 696             iae.initCause(e);
 697             throw iae;
 698         }
 699     }
 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<>();


 798         for (PlatformComponent component: PlatformComponent.values()) {
 799             result.add(component.getMXBeanInterface());
 800         }
 801         return result;
 802     }
 803 
 804     private static final String NOTIF_EMITTER =
 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 
 833 }


  23  * questions.
  24  */
  25 
  26 package java.lang.management;
  27 import javax.management.DynamicMBean;
  28 import javax.management.MBeanServer;
  29 import javax.management.MBeanServerConnection;
  30 import javax.management.MBeanServerFactory;
  31 import javax.management.MBeanServerPermission;
  32 import javax.management.NotificationEmitter;
  33 import javax.management.ObjectInstance;
  34 import javax.management.ObjectName;
  35 import javax.management.InstanceAlreadyExistsException;
  36 import javax.management.InstanceNotFoundException;
  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.List;
  44 import java.util.Set;
  45 import java.util.TreeSet;
  46 import java.security.AccessController;
  47 import java.security.Permission;
  48 import java.security.PrivilegedAction;
  49 import java.security.PrivilegedActionException;
  50 import java.security.PrivilegedExceptionAction;
  51 import javax.management.JMX;
  52 import sun.management.ManagementFactoryHelper;
  53 
  54 /**
  55  * The {@code ManagementFactory} class is a factory class for getting
  56  * managed beans for the Java platform.
  57  * This class consists of static methods each of which returns
  58  * one or more <i>platform MXBeans</i> representing
  59  * the management interface of a component of the Java virtual
  60  * machine.
  61  * <p>
  62  * <h4><a name="MXBean">Platform MXBeans</a></h4>
  63  * <p>
  64  * A platform MXBean is a <i>managed bean</i> that
  65  * conforms to the <a href="../../../javax/management/package-summary.html">JMX</a>
  66  * Instrumentation Specification and only uses a set of basic data types.
  67  * A JMX management application and the {@linkplain
  68  * #getPlatformMBeanServer platform MBeanServer}
  69  * can interoperate without requiring classes for MXBean specific
  70  * data types.
  71  * The data types being transmitted between the JMX connector
  72  * server and the connector client are
  73  * {@linkplain javax.management.openmbean.OpenType open types}
  74  * and this allows interoperation across versions.
  75  * See <a href="../../../javax/management/MXBean.html#MXBean-spec">
  76  * the specification of MXBeans</a> for details.
  77  *
  78  * <a name="MXBeanNames"></a>
  79  * <p>Each platform MXBean is a {@link PlatformManagedObject}
  80  * and it has a unique
  81  * {@link javax.management.ObjectName ObjectName} for
  82  * registration in the platform {@code MBeanServer} as returned by
  83  * by the {@link PlatformManagedObject#getObjectName getObjectName}
  84  * method.
  85  *
  86  * <p>
  87  * An application can access a platform MXBean in the following ways:
  88  * <h5>1. Direct access to an MXBean interface</h5>
  89  * <blockquote>
  90  * <ul>
  91  *     <li>Get an MXBean instance by calling the 
  92  *         {@link #getPlatformMXBean(Class) getPlatformMXBean} or
  93  *         {@link #getPlatformMXBeans(Class) getPlatformMXBeans} method
  94  *         and access the MXBean locally in the running

  95  *         virtual machine.
  96  *         </li>
  97  *     <li>Construct an MXBean proxy instance that forwards the
  98  *         method calls to a given {@link MBeanServer MBeanServer} by calling
  99  *         the {@link #getPlatformMXBean(MBeanServerConnection, Class)} or
 100  *         {@link #getPlatformMXBeans(MBeanServerConnection, Class)} method.
 101  *         The {@link #newPlatformMXBeanProxy newPlatformMXBeanProxy} method
 102  *         can also be used to construct an MXBean proxy instance of 
 103  *         a given {@code ObjectName}.
 104  *         A proxy is typically constructed to remotely access
 105  *         an MXBean of another running virtual machine.
 106  *         </li>
 107  * </ul>
 108  * <h5>2. Indirect access to an MXBean interface via MBeanServer</h5>
 109  * <ul>
 110  *     <li>Go through the platform {@code MBeanServer} to access MXBeans
 111  *         locally or a specific <tt>MBeanServerConnection</tt> to access

 112  *         MXBeans remotely.
 113  *         The attributes and operations of an MXBean use only
 114  *         <em>JMX open types</em> which include basic data types,
 115  *         {@link javax.management.openmbean.CompositeData CompositeData},
 116  *         and {@link javax.management.openmbean.TabularData TabularData}
 117  *         defined in
 118  *         {@link javax.management.openmbean.OpenType OpenType}.
 119  *         The mapping is specified in 
 120  *         the {@linkplain javax.management.MXBean MXBean} specification
 121  *         for details.
 122  *        </li>

 123  * </ul>
 124  * </blockquote>
 125  *









 126  * <p>
 127  * The {@link #getPlatformManagementInterfaces getPlatformManagementInterfaces}
 128  * method returns all management interfaces supported in the Java virtual machine
 129  * including the standard management interfaces listed in the tables
 130  * below as well as the management interfaces extended by the JDK implementation.



 131  * <p>








































































































 132  * A Java virtual machine has a single instance of the following management
 133  * interfaces:
 134  *
 135  * <blockquote>
 136  * <table border>
 137  * <tr>
 138  * <th>Management Interface</th>
 139  * <th>ObjectName</th>
 140  * </tr>
 141  * <tr>
 142  * <td> {@link ClassLoadingMXBean} </td>
 143  * <td> {@link #CLASS_LOADING_MXBEAN_NAME
 144  *             java.lang:type=ClassLoading}</td>
 145  * </tr>
 146  * <tr>
 147  * <td> {@link MemoryMXBean} </td>
 148  * <td> {@link #MEMORY_MXBEAN_NAME
 149  *             java.lang:type=Memory}</td>
 150  * </tr>
 151  * <tr>
 152  * <td> {@link ThreadMXBean} </td>
 153  * <td> {@link #THREAD_MXBEAN_NAME
 154  *             java.lang:type=Threading}</td>
 155  * </tr>
 156  * <tr>
 157  * <td> {@link RuntimeMXBean} </td>
 158  * <td> {@link #RUNTIME_MXBEAN_NAME
 159  *             java.lang:type=Runtime}</td>
 160  * </tr>
 161  * <tr>
 162  * <td> {@link OperatingSystemMXBean} </td>
 163  * <td> {@link #OPERATING_SYSTEM_MXBEAN_NAME
 164  *             java.lang:type=OperatingSystem}</td>
 165  * </tr>
 166  * <tr>
 167  * <td> {@link PlatformLoggingMXBean} </td>
 168  * <td> {@link java.util.logging.LogManager#LOGGING_MXBEAN_NAME
 169  *             java.util.logging:type=Logging}</td>
 170  * </tr>
 171  * </table>
 172  * </blockquote>
 173  *
 174  * <p>
 175  * A Java virtual machine has zero or a single instance of
 176  * the following management interfaces.
 177  *
 178  * <blockquote>
 179  * <table border>
 180  * <tr>
 181  * <th>Management Interface</th>
 182  * <th>ObjectName</th>
 183  * </tr>
 184  * <tr>
 185  * <td> {@link CompilationMXBean} </td>
 186  * <td> {@link #COMPILATION_MXBEAN_NAME
 187  *             java.lang:type=Compilation}</td>
 188  * </tr>
 189  * </table>
 190  * </blockquote>
 191  *
 192  * <p>
 193  * A Java virtual machine may have one or more instances of the following
 194  * management interfaces.
 195  * <blockquote>
 196  * <table border>
 197  * <tr>
 198  * <th>Management Interface</th>
 199  * <th>ObjectName</th>
 200  * </tr>
 201  * <tr>
 202  * <td> {@link GarbageCollectorMXBean} </td>
 203  * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
 204  *             java.lang:type=GarbageCollector}<tt>,name=</tt><i>collector's name</i></td>
 205  * </tr>
 206  * <tr>
 207  * <td> {@link MemoryManagerMXBean} </td>
 208  * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
 209  *             java.lang:type=MemoryManager}<tt>,name=</tt><i>manager's name</i></td>
 210  * </tr>
 211  * <tr>
 212  * <td> {@link MemoryPoolMXBean} </td>
 213  * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
 214  *             java.lang:type=MemoryPool}<tt>,name=</tt><i>pool's name</i></td>
 215  * </tr>
 216  * <tr>
 217  * <td> {@link BufferPoolMXBean} </td>
 218  * <td> {@code java.nio:type=BufferPool,name=}<i>pool name</i></td>
 219  * </tr>
 220  * </table>
 221  * </blockquote>
 222  *
 223  * @see <a href="../../../javax/management/package-summary.html">
 224  *      JMX Specification</a>
 225  * @see <a href="package-summary.html#examples">
 226  *      Ways to Access Management Metrics</a>

 227  * @see javax.management.MXBean
 228  *
 229  * @author  Mandy Chung
 230  * @since   1.5
 231  */
 232 public class ManagementFactory {
 233     // A class with only static fields and methods.
 234     private ManagementFactory() {};
 235 
 236     /**
 237      * String representation of the
 238      * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
 239      */
 240     public final static String CLASS_LOADING_MXBEAN_NAME =
 241         "java.lang:type=ClassLoading";
 242 
 243     /**
 244      * String representation of the
 245      * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
 246      */


 400 
 401     /**
 402      * Returns a list of {@link GarbageCollectorMXBean} objects
 403      * in the Java virtual machine.
 404      * The Java virtual machine may have one or more
 405      * <tt>GarbageCollectorMXBean</tt> objects.
 406      * It may add or remove <tt>GarbageCollectorMXBean</tt>
 407      * during execution.
 408      *
 409      * @return a list of <tt>GarbageCollectorMXBean</tt> objects.
 410      *
 411      */
 412     public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
 413         return ManagementFactoryHelper.getGarbageCollectorMXBeans();
 414     }
 415 
 416     private static MBeanServer platformMBeanServer;
 417     /**
 418      * Returns the platform {@link javax.management.MBeanServer MBeanServer}.
 419      * On the first call to this method, it first creates the platform
 420      * {@code MBeanServer} by calling the
 421      * {@link javax.management.MBeanServerFactory#createMBeanServer
 422      * MBeanServerFactory.createMBeanServer}
 423      * method and registers each platform MXBean in this platform
 424      * {@code MBeanServer} with its 
 425      * {@link PlatformManagedObject#getObjectName ObjectName}.
 426      * This method, in subsequent calls, will simply return the
 427      * initially created platform {@code MBeanServer}.
 428      * <p>
 429      * MXBeans that get created and destroyed dynamically, for example,
 430      * memory {@link MemoryPoolMXBean pools} and
 431      * {@link MemoryManagerMXBean managers},
 432      * will automatically be registered and deregistered into the platform
 433      * {@code MBeanServer}.
 434      * <p>
 435      * If the system property {@code javax.management.builder.initial}
 436      * is set, the platform {@code MBeanServer} creation will be done
 437      * by the specified {@link javax.management.MBeanServerBuilder}.
 438      * <p>
 439      * It is recommended that this platform MBeanServer also be used
 440      * to register other application managed beans
 441      * besides the platform MXBeans.
 442      * This will allow all MBeans to be published through the same
 443      * {@code MBeanServer} and hence allow for easier network publishing
 444      * and discovery.
 445      * Name conflicts with the platform MXBeans should be avoided.
 446      *
 447      * @return the platform {@code MBeanServer}; the platform
 448      *         MXBeans are registered into the platform {@code MBeanServer}
 449      *         at the first time this method is called.
 450      *
 451      * @exception SecurityException if there is a security manager
 452      * and the caller does not have the permission required by
 453      * {@link javax.management.MBeanServerFactory#createMBeanServer}.
 454      *
 455      * @see javax.management.MBeanServerFactory
 456      * @see javax.management.MBeanServerFactory#createMBeanServer
 457      */
 458     public static synchronized MBeanServer getPlatformMBeanServer() {
 459         SecurityManager sm = System.getSecurityManager();
 460         if (sm != null) {
 461             Permission perm = new MBeanServerPermission("createMBeanServer");
 462             sm.checkPermission(perm);
 463         }
 464 
 465         if (platformMBeanServer == null) {
 466             platformMBeanServer = MBeanServerFactory.createMBeanServer();
 467             for (PlatformComponent pc : PlatformComponent.values()) {
 468                 List<? extends PlatformManagedObject> list =


 575                                String mxbeanName,
 576                                Class<T> mxbeanInterface)
 577             throws java.io.IOException {
 578 
 579         final Class interfaceClass = mxbeanInterface;
 580         // Only allow MXBean interfaces from rt.jar loaded by the
 581         // bootstrap class loader
 582         final ClassLoader loader =
 583             AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
 584                 public ClassLoader run() {
 585                     return interfaceClass.getClassLoader();
 586                 }
 587             });
 588         if (loader != null) {
 589             throw new IllegalArgumentException(mxbeanName +
 590                 " is not a platform MXBean");
 591         }
 592 
 593         try {
 594             final ObjectName objName = new ObjectName(mxbeanName);
 595             // skip the isInstanceOf check for LoggingMXBean
 596             String intfName = interfaceClass.getName();
 597             if (!connection.isInstanceOf(objName, intfName)) {
 598                 throw new IllegalArgumentException(mxbeanName +
 599                     " is not an instance of " + interfaceClass);
 600             }
 601 
 602             final Class[] interfaces;
 603             // check if the registered MBean is a notification emitter
 604             boolean emitter = connection.isInstanceOf(objName, NOTIF_EMITTER);
 605 
 606             // create an MXBean proxy
 607             return JMX.newMXBeanProxy(connection, objName, mxbeanInterface,
 608                                       emitter);
 609         } catch (InstanceNotFoundException|MalformedObjectNameException e) {
 610             throw new IllegalArgumentException(e);










 611         }
 612     }
 613 
 614     /**
 615      * Returns the platform MXBean implementing
 616      * the given {@code mxbeanInterface} which is specified 
 617      * to have one single instance in the Java virtual machine.
 618      * This method may return {@code null} if the management interface
 619      * is not implemented in the Java virtual machine (for example,
 620      * a Java virtual machine with no compilation system does not
 621      * implement {@link CompilationMXBean});
 622      * otherwise, this method is equivalent to calling:
 623      * <pre>
 624      *    {@link #getPlatformMXBeans(Class)
 625      *      getPlatformMXBeans(mxbeanInterface)}.get(0);
 626      * </pre>
 627      *
 628      * @param mxbeanInterface a management interface for a platform
 629      *     MXBean with one single instance in the Java virtual machine
 630      *     if implemented.
 631      *
 632      * @return the platform MXBean that implements
 633      * {@code mxbeanInterface}, or {@code null} if not exist.
 634      *
 635      * @throws IllegalArgumentException if {@code mxbeanInterface}
 636      * is not a platform management interface or
 637      * not a singleton platform MXBean.
 638      *
 639      * @since 1.7
 640      */
 641     public static <T extends PlatformManagedObject>
 642             T getPlatformMXBean(Class<T> mxbeanInterface) {
 643         PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
 644         if (pc == null)
 645             throw new IllegalArgumentException(mxbeanInterface.getName() +
 646                 " is not a platform management interface");
 647         if (!pc.isSingleton())
 648             throw new IllegalArgumentException(mxbeanInterface.getName() +
 649                 " can have zero or more than one instances");
 650 
 651         return pc.getSingletonMXBean(mxbeanInterface);
 652     }
 653 
 654     /**
 655      * Returns the list of platform MXBeans implementing
 656      * the given {@code mxbeanInterface} in the Java
 657      * virtual machine.
 658      * The returned list may contain zero, one, or more instances.
 659      * The number of instances in the returned list is defined
 660      * in the specification of the given management interface.
 661      * The order is undefined and there is no guarantee that
 662      * the list returned is in the same order as previous invocations.
 663      *
 664      * @param mxbeanInterface a management interface for a platform
 665      *                        MXBean
 666      *
 667      * @return the list of platform MXBeans that implement
 668      * {@code mxbeanInterface}.
 669      *
 670      * @throws IllegalArgumentException if {@code mxbeanInterface}
 671      * is not a platform management interface.
 672      *
 673      * @since 1.7
 674      */
 675     public static <T extends PlatformManagedObject> List<T>
 676             getPlatformMXBeans(Class<T> mxbeanInterface) {
 677         PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
 678         if (pc == null)
 679             throw new IllegalArgumentException(mxbeanInterface.getName() +
 680                 " is not a platform management interface");
 681         return Collections.unmodifiableList(pc.getMXBeans(mxbeanInterface));



 682     }
 683 
 684     /**
 685      * Returns the platform MXBean proxy for 
 686      * {@code mxbeanInterface} which is specified to have one single
 687      * instance in a Java virtual machine and the proxy will 
 688      * forward the method calls through the given {@code MBeanServerConnection}.
 689      * This method may return {@code null} if the management interface
 690      * is not implemented in the Java virtual machine being monitored
 691      * (for example, a Java virtual machine with no compilation system
 692      * does not implement {@link CompilationMXBean});
 693      * otherwise, this method is equivalent to calling:
 694      * <pre>
 695      *     {@link #getPlatformMXBeans(MBeanServerConnection, Class)
 696      *        getPlatformMXBeans(connection, mxbeanInterface)}.get(0);
 697      * </pre>
 698      * 
 699      * @param connection the {@code MBeanServerConnection} to forward to.
 700      * @param mxbeanInterface a management interface for a platform
 701      *     MXBean with one single instance in the Java virtual machine
 702      *     being monitored, if implemented.
 703      *
 704      * @return the platform MXBean proxy for
 705      * forwarding the method calls of the {@code mxbeanInterface}
 706      * through the given {@code MBeanServerConnection},
 707      * or {@code null} if not exist.
 708      *
 709      * @throws IllegalArgumentException if {@code mxbeanInterface}
 710      * is not a platform management interface or
 711      * not a singleton platform MXBean.
 712      * @throws java.io.IOException if a communication problem
 713      * occurred when accessing the {@code MBeanServerConnection}.
 714      *
 715      * @see #newPlatformMXBeanProxy
 716      * @since 1.7
 717      */
 718     public static <T extends PlatformManagedObject>
 719             T getPlatformMXBean(MBeanServerConnection connection,
 720                                 Class<T> mxbeanInterface)
 721         throws java.io.IOException
 722     {
 723         PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
 724         if (pc == null)
 725             throw new IllegalArgumentException(mxbeanInterface.getName() +
 726                 " is not a platform management interface");
 727         if (!pc.isSingleton())
 728             throw new IllegalArgumentException(mxbeanInterface.getName() +
 729                 " can have zero or more than one instances");
 730         return pc.getSingletonMXBean(connection, mxbeanInterface);
 731     }
 732 
 733     /**
 734      * Returns the list of the platform MXBean proxies for
 735      * forwarding the method calls of the {@code mxbeanInterface}
 736      * through the given {@code MBeanServerConnection}.
 737      * The returned list may contain zero, one, or more instances.
 738      * The number of instances in the returned list is defined
 739      * in the specification of the given management interface.
 740      * The order is undefined and there is no guarantee that
 741      * the list returned is in the same order as previous invocations.
 742      *
 743      * @param connection the {@code MBeanServerConnection} to forward to.
 744      * @param mxbeanInterface a management interface for a platform
 745      *                        MXBean
 746      *
 747      * @return the list of platform MXBean proxies for
 748      * forwarding the method calls of the {@code mxbeanInterface}
 749      * through the given {@code MBeanServerConnection}.
 750      *
 751      * @throws IllegalArgumentException if {@code mxbeanInterface}
 752      * is not a platform management interface.
 753      *
 754      * @throws java.io.IOException if a communication problem
 755      * occurred when accessing the {@code MBeanServerConnection}.
 756      *
 757      * @see #newPlatformMXBeanProxy
 758      * @since 1.7
 759      */
 760     public static <T extends PlatformManagedObject>
 761             List<T> getPlatformMXBeans(MBeanServerConnection connection,
 762                                        Class<T> mxbeanInterface)
 763         throws java.io.IOException
 764     {
 765         PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
 766         if (pc == null) {










 767             throw new IllegalArgumentException(mxbeanInterface.getName() +
 768                 " is not a platform management interface");
 769         }
 770         return Collections.unmodifiableList(pc.getMXBeans(connection, mxbeanInterface));
 771     }
 772 
 773     /**
 774      * Returns the set of {@code Class} objects, subinterface of
 775      * {@link PlatformManagedObject}, representing
 776      * all management interfaces for
 777      * monitoring and managing the Java platform.
 778      *
 779      * @return the set of {@code Class} objects, subinterface of
 780      * {@link PlatformManagedObject} representing
 781      * the management interfaces for
 782      * monitoring and managing the Java platform.
 783      *
 784      * @since 1.7
 785      */
 786     public static Set<Class<? extends PlatformManagedObject>>
 787            getPlatformManagementInterfaces()
 788     {
 789         Set<Class<? extends PlatformManagedObject>> result =
 790             new TreeSet<>();
 791         for (PlatformComponent component: PlatformComponent.values()) {
 792             result.add(component.getMXBeanInterface());
 793         }
 794         return Collections.unmodifiableSet(result);
 795     }
 796 
 797     private static final String NOTIF_EMITTER =
 798         "javax.management.NotificationEmitter";
 799 
 800     /**
 801      * Registers an MXBean.
 802      */
 803     private static void addMXBean(final MBeanServer mbs, final PlatformManagedObject pmo) {
 804         // Make DynamicMBean out of MXBean by wrapping it with a StandardMBean
 805         final DynamicMBean dmbean;
 806         if (pmo instanceof DynamicMBean) {
 807             dmbean = DynamicMBean.class.cast(pmo);
 808         } else if (pmo instanceof NotificationEmitter) {
 809             dmbean = new StandardEmitterMBean(pmo, null, true, (NotificationEmitter) pmo);
 810         } else {
 811             dmbean = new StandardMBean(pmo, null, true);
 812         }
 813 
 814         try {
 815             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
 816                 public Void run() throws InstanceAlreadyExistsException,
 817                                          MBeanRegistrationException,
 818                                          NotCompliantMBeanException {
 819                     mbs.registerMBean(dmbean, pmo.getObjectName());
 820                     return null;
 821                 }
 822             });
 823         } catch (PrivilegedActionException e) {
 824             throw new RuntimeException(e.getException());
 825         }
 826     }
 827 
 828 }