jdk/src/java.management/share/classes/java/lang/management/ManagementFactory.java
changeset 32034 05676cfd40b5
parent 30355 e37c7eba132f
child 34882 ce2a8ec851c1
equal deleted inserted replaced
32033:bf24e33c7919 32034:05676cfd40b5
   114  *         </li>
   114  *         </li>
   115  * </ul>
   115  * </ul>
   116  * <h4>2. Indirect access to an MXBean interface via MBeanServer</h4>
   116  * <h4>2. Indirect access to an MXBean interface via MBeanServer</h4>
   117  * <ul>
   117  * <ul>
   118  *     <li>Go through the platform {@code MBeanServer} to access MXBeans
   118  *     <li>Go through the platform {@code MBeanServer} to access MXBeans
   119  *         locally or a specific <tt>MBeanServerConnection</tt> to access
   119  *         locally or a specific {@code MBeanServerConnection} to access
   120  *         MXBeans remotely.
   120  *         MXBeans remotely.
   121  *         The attributes and operations of an MXBean use only
   121  *         The attributes and operations of an MXBean use only
   122  *         <em>JMX open types</em> which include basic data types,
   122  *         <em>JMX open types</em> which include basic data types,
   123  *         {@link javax.management.openmbean.CompositeData CompositeData},
   123  *         {@link javax.management.openmbean.CompositeData CompositeData},
   124  *         and {@link javax.management.openmbean.TabularData TabularData}
   124  *         and {@link javax.management.openmbean.TabularData TabularData}
   207  * <th>ObjectName</th>
   207  * <th>ObjectName</th>
   208  * </tr>
   208  * </tr>
   209  * <tr>
   209  * <tr>
   210  * <td> {@link GarbageCollectorMXBean} </td>
   210  * <td> {@link GarbageCollectorMXBean} </td>
   211  * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
   211  * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
   212  *             java.lang:type=GarbageCollector}<tt>,name=</tt><i>collector's name</i></td>
   212  *             java.lang:type=GarbageCollector}{@code ,name=}<i>collector's name</i></td>
   213  * </tr>
   213  * </tr>
   214  * <tr>
   214  * <tr>
   215  * <td> {@link MemoryManagerMXBean} </td>
   215  * <td> {@link MemoryManagerMXBean} </td>
   216  * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
   216  * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
   217  *             java.lang:type=MemoryManager}<tt>,name=</tt><i>manager's name</i></td>
   217  *             java.lang:type=MemoryManager}{@code ,name=}<i>manager's name</i></td>
   218  * </tr>
   218  * </tr>
   219  * <tr>
   219  * <tr>
   220  * <td> {@link MemoryPoolMXBean} </td>
   220  * <td> {@link MemoryPoolMXBean} </td>
   221  * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
   221  * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
   222  *             java.lang:type=MemoryPool}<tt>,name=</tt><i>pool's name</i></td>
   222  *             java.lang:type=MemoryPool}{@code ,name=}<i>pool's name</i></td>
   223  * </tr>
   223  * </tr>
   224  * <tr>
   224  * <tr>
   225  * <td> {@link BufferPoolMXBean} </td>
   225  * <td> {@link BufferPoolMXBean} </td>
   226  * <td> {@code java.nio:type=BufferPool,name=}<i>pool name</i></td>
   226  * <td> {@code java.nio:type=BufferPool,name=}<i>pool name</i></td>
   227  * </tr>
   227  * </tr>
   241     // A class with only static fields and methods.
   241     // A class with only static fields and methods.
   242     private ManagementFactory() {};
   242     private ManagementFactory() {};
   243 
   243 
   244     /**
   244     /**
   245      * String representation of the
   245      * String representation of the
   246      * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
   246      * {@code ObjectName} for the {@link ClassLoadingMXBean}.
   247      */
   247      */
   248     public final static String CLASS_LOADING_MXBEAN_NAME =
   248     public final static String CLASS_LOADING_MXBEAN_NAME =
   249         "java.lang:type=ClassLoading";
   249         "java.lang:type=ClassLoading";
   250 
   250 
   251     /**
   251     /**
   252      * String representation of the
   252      * String representation of the
   253      * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
   253      * {@code ObjectName} for the {@link CompilationMXBean}.
   254      */
   254      */
   255     public final static String COMPILATION_MXBEAN_NAME =
   255     public final static String COMPILATION_MXBEAN_NAME =
   256         "java.lang:type=Compilation";
   256         "java.lang:type=Compilation";
   257 
   257 
   258     /**
   258     /**
   259      * String representation of the
   259      * String representation of the
   260      * <tt>ObjectName</tt> for the {@link MemoryMXBean}.
   260      * {@code ObjectName} for the {@link MemoryMXBean}.
   261      */
   261      */
   262     public final static String MEMORY_MXBEAN_NAME =
   262     public final static String MEMORY_MXBEAN_NAME =
   263         "java.lang:type=Memory";
   263         "java.lang:type=Memory";
   264 
   264 
   265     /**
   265     /**
   266      * String representation of the
   266      * String representation of the
   267      * <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.
   267      * {@code ObjectName} for the {@link OperatingSystemMXBean}.
   268      */
   268      */
   269     public final static String OPERATING_SYSTEM_MXBEAN_NAME =
   269     public final static String OPERATING_SYSTEM_MXBEAN_NAME =
   270         "java.lang:type=OperatingSystem";
   270         "java.lang:type=OperatingSystem";
   271 
   271 
   272     /**
   272     /**
   273      * String representation of the
   273      * String representation of the
   274      * <tt>ObjectName</tt> for the {@link RuntimeMXBean}.
   274      * {@code ObjectName} for the {@link RuntimeMXBean}.
   275      */
   275      */
   276     public final static String RUNTIME_MXBEAN_NAME =
   276     public final static String RUNTIME_MXBEAN_NAME =
   277         "java.lang:type=Runtime";
   277         "java.lang:type=Runtime";
   278 
   278 
   279     /**
   279     /**
   280      * String representation of the
   280      * String representation of the
   281      * <tt>ObjectName</tt> for the {@link ThreadMXBean}.
   281      * {@code ObjectName} for the {@link ThreadMXBean}.
   282      */
   282      */
   283     public final static String THREAD_MXBEAN_NAME =
   283     public final static String THREAD_MXBEAN_NAME =
   284         "java.lang:type=Threading";
   284         "java.lang:type=Threading";
   285 
   285 
   286     /**
   286     /**
   287      * The domain name and the type key property in
   287      * The domain name and the type key property in
   288      * the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.
   288      * the {@code ObjectName} for a {@link GarbageCollectorMXBean}.
   289      * The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>
   289      * The unique {@code ObjectName} for a {@code GarbageCollectorMXBean}
   290      * can be formed by appending this string with
   290      * can be formed by appending this string with
   291      * "<tt>,name=</tt><i>collector's name</i>".
   291      * "{@code ,name=}<i>collector's name</i>".
   292      */
   292      */
   293     public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
   293     public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
   294         "java.lang:type=GarbageCollector";
   294         "java.lang:type=GarbageCollector";
   295 
   295 
   296     /**
   296     /**
   297      * The domain name and the type key property in
   297      * The domain name and the type key property in
   298      * the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.
   298      * the {@code ObjectName} for a {@link MemoryManagerMXBean}.
   299      * The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>
   299      * The unique {@code ObjectName} for a {@code MemoryManagerMXBean}
   300      * can be formed by appending this string with
   300      * can be formed by appending this string with
   301      * "<tt>,name=</tt><i>manager's name</i>".
   301      * "{@code ,name=}<i>manager's name</i>".
   302      */
   302      */
   303     public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=
   303     public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=
   304         "java.lang:type=MemoryManager";
   304         "java.lang:type=MemoryManager";
   305 
   305 
   306     /**
   306     /**
   307      * The domain name and the type key property in
   307      * The domain name and the type key property in
   308      * the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.
   308      * the {@code ObjectName} for a {@link MemoryPoolMXBean}.
   309      * The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>
   309      * The unique {@code ObjectName} for a {@code MemoryPoolMXBean}
   310      * can be formed by appending this string with
   310      * can be formed by appending this string with
   311      * <tt>,name=</tt><i>pool's name</i>.
   311      * {@code ,name=}<i>pool's name</i>.
   312      */
   312      */
   313     public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE=
   313     public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE=
   314         "java.lang:type=MemoryPool";
   314         "java.lang:type=MemoryPool";
   315 
   315 
   316     /**
   316     /**
   355         return getPlatformMXBean(RuntimeMXBean.class);
   355         return getPlatformMXBean(RuntimeMXBean.class);
   356     }
   356     }
   357 
   357 
   358     /**
   358     /**
   359      * Returns the managed bean for the compilation system of
   359      * Returns the managed bean for the compilation system of
   360      * the Java virtual machine.  This method returns <tt>null</tt>
   360      * the Java virtual machine.  This method returns {@code null}
   361      * if the Java virtual machine has no compilation system.
   361      * if the Java virtual machine has no compilation system.
   362      *
   362      *
   363      * @return a {@link CompilationMXBean} object for the Java virtual
   363      * @return a {@link CompilationMXBean} object for the Java virtual
   364      *   machine or <tt>null</tt> if the Java virtual machine has
   364      *   machine or {@code null} if the Java virtual machine has
   365      *   no compilation system.
   365      *   no compilation system.
   366      */
   366      */
   367     public static CompilationMXBean getCompilationMXBean() {
   367     public static CompilationMXBean getCompilationMXBean() {
   368         return getPlatformMXBean(CompilationMXBean.class);
   368         return getPlatformMXBean(CompilationMXBean.class);
   369     }
   369     }
   383      * Returns a list of {@link MemoryPoolMXBean} objects in the
   383      * Returns a list of {@link MemoryPoolMXBean} objects in the
   384      * Java virtual machine.
   384      * Java virtual machine.
   385      * The Java virtual machine can have one or more memory pools.
   385      * The Java virtual machine can have one or more memory pools.
   386      * It may add or remove memory pools during execution.
   386      * It may add or remove memory pools during execution.
   387      *
   387      *
   388      * @return a list of <tt>MemoryPoolMXBean</tt> objects.
   388      * @return a list of {@code MemoryPoolMXBean} objects.
   389      *
   389      *
   390      */
   390      */
   391     public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
   391     public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
   392         return getPlatformMXBeans(MemoryPoolMXBean.class);
   392         return getPlatformMXBeans(MemoryPoolMXBean.class);
   393     }
   393     }
   396      * Returns a list of {@link MemoryManagerMXBean} objects
   396      * Returns a list of {@link MemoryManagerMXBean} objects
   397      * in the Java virtual machine.
   397      * in the Java virtual machine.
   398      * The Java virtual machine can have one or more memory managers.
   398      * The Java virtual machine can have one or more memory managers.
   399      * It may add or remove memory managers during execution.
   399      * It may add or remove memory managers during execution.
   400      *
   400      *
   401      * @return a list of <tt>MemoryManagerMXBean</tt> objects.
   401      * @return a list of {@code MemoryManagerMXBean} objects.
   402      *
   402      *
   403      */
   403      */
   404     public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
   404     public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
   405         return getPlatformMXBeans(MemoryManagerMXBean.class);
   405         return getPlatformMXBeans(MemoryManagerMXBean.class);
   406     }
   406     }
   408 
   408 
   409     /**
   409     /**
   410      * Returns a list of {@link GarbageCollectorMXBean} objects
   410      * Returns a list of {@link GarbageCollectorMXBean} objects
   411      * in the Java virtual machine.
   411      * in the Java virtual machine.
   412      * The Java virtual machine may have one or more
   412      * The Java virtual machine may have one or more
   413      * <tt>GarbageCollectorMXBean</tt> objects.
   413      * {@code GarbageCollectorMXBean} objects.
   414      * It may add or remove <tt>GarbageCollectorMXBean</tt>
   414      * It may add or remove {@code GarbageCollectorMXBean}
   415      * during execution.
   415      * during execution.
   416      *
   416      *
   417      * @return a list of <tt>GarbageCollectorMXBean</tt> objects.
   417      * @return a list of {@code GarbageCollectorMXBean} objects.
   418      *
   418      *
   419      */
   419      */
   420     public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
   420     public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
   421         return getPlatformMXBeans(GarbageCollectorMXBean.class);
   421         return getPlatformMXBeans(GarbageCollectorMXBean.class);
   422     }
   422     }
   483 
   483 
   484     /**
   484     /**
   485      * Returns a proxy for a platform MXBean interface of a
   485      * Returns a proxy for a platform MXBean interface of a
   486      * given <a href="#MXBeanNames">MXBean name</a>
   486      * given <a href="#MXBeanNames">MXBean name</a>
   487      * that forwards its method calls through the given
   487      * that forwards its method calls through the given
   488      * <tt>MBeanServerConnection</tt>.
   488      * {@code MBeanServerConnection}.
   489      *
   489      *
   490      * <p>This method is equivalent to:
   490      * <p>This method is equivalent to:
   491      * <blockquote>
   491      * <blockquote>
   492      * {@link java.lang.reflect.Proxy#newProxyInstance
   492      * {@link java.lang.reflect.Proxy#newProxyInstance
   493      *        Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),
   493      *        Proxy.newProxyInstance}{@code (mxbeanInterface.getClassLoader(),
   494      *        new Class[] { mxbeanInterface }, handler)</tt>
   494      *        new Class[] { mxbeanInterface }, handler)}
   495      * </blockquote>
   495      * </blockquote>
   496      *
   496      *
   497      * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler
   497      * where {@code handler} is an {@link java.lang.reflect.InvocationHandler
   498      * InvocationHandler} to which method invocations to the MXBean interface
   498      * InvocationHandler} to which method invocations to the MXBean interface
   499      * are dispatched. This <tt>handler</tt> converts an input parameter
   499      * are dispatched. This {@code handler} converts an input parameter
   500      * from an MXBean data type to its mapped open type before forwarding
   500      * from an MXBean data type to its mapped open type before forwarding
   501      * to the <tt>MBeanServer</tt> and converts a return value from
   501      * to the {@code MBeanServer} and converts a return value from
   502      * an MXBean method call through the <tt>MBeanServer</tt>
   502      * an MXBean method call through the {@code MBeanServer}
   503      * from an open type to the corresponding return type declared in
   503      * from an open type to the corresponding return type declared in
   504      * the MXBean interface.
   504      * the MXBean interface.
   505      *
   505      *
   506      * <p>
   506      * <p>
   507      * If the MXBean is a notification emitter (i.e.,
   507      * If the MXBean is a notification emitter (i.e.,
   508      * it implements
   508      * it implements
   509      * {@link javax.management.NotificationEmitter NotificationEmitter}),
   509      * {@link javax.management.NotificationEmitter NotificationEmitter}),
   510      * both the <tt>mxbeanInterface</tt> and <tt>NotificationEmitter</tt>
   510      * both the {@code mxbeanInterface} and {@code NotificationEmitter}
   511      * will be implemented by this proxy.
   511      * will be implemented by this proxy.
   512      *
   512      *
   513      * <p>
   513      * <p>
   514      * <b>Notes:</b>
   514      * <b>Notes:</b>
   515      * <ol>
   515      * <ol>
   516      * <li>Using an MXBean proxy is a convenience remote access to
   516      * <li>Using an MXBean proxy is a convenience remote access to
   517      * a platform MXBean of a running virtual machine.  All method
   517      * a platform MXBean of a running virtual machine.  All method
   518      * calls to the MXBean proxy are forwarded to an
   518      * calls to the MXBean proxy are forwarded to an
   519      * <tt>MBeanServerConnection</tt> where
   519      * {@code MBeanServerConnection} where
   520      * {@link java.io.IOException IOException} may be thrown
   520      * {@link java.io.IOException IOException} may be thrown
   521      * when the communication problem occurs with the connector server.
   521      * when the communication problem occurs with the connector server.
   522      * An application remotely accesses the platform MXBeans using
   522      * An application remotely accesses the platform MXBeans using
   523      * proxy should prepare to catch <tt>IOException</tt> as if
   523      * proxy should prepare to catch {@code IOException} as if
   524      * accessing with the <tt>MBeanServerConnector</tt> interface.</li>
   524      * accessing with the {@code MBeanServerConnector} interface.</li>
   525      *
   525      *
   526      * <li>When a client application is designed to remotely access MXBeans
   526      * <li>When a client application is designed to remotely access MXBeans
   527      * for a running virtual machine whose version is different than
   527      * for a running virtual machine whose version is different than
   528      * the version on which the application is running,
   528      * the version on which the application is running,
   529      * it should prepare to catch
   529      * it should prepare to catch
   535      * <li>{@link javax.management.MBeanServerInvocationHandler
   535      * <li>{@link javax.management.MBeanServerInvocationHandler
   536      * MBeanServerInvocationHandler} or its
   536      * MBeanServerInvocationHandler} or its
   537      * {@link javax.management.MBeanServerInvocationHandler#newProxyInstance
   537      * {@link javax.management.MBeanServerInvocationHandler#newProxyInstance
   538      * newProxyInstance} method cannot be used to create
   538      * newProxyInstance} method cannot be used to create
   539      * a proxy for a platform MXBean. The proxy object created
   539      * a proxy for a platform MXBean. The proxy object created
   540      * by <tt>MBeanServerInvocationHandler</tt> does not handle
   540      * by {@code MBeanServerInvocationHandler} does not handle
   541      * the properties of the platform MXBeans described in
   541      * the properties of the platform MXBeans described in
   542      * the <a href="#MXBean">class specification</a>.
   542      * the <a href="#MXBean">class specification</a>.
   543      *</li>
   543      *</li>
   544      * </ol>
   544      * </ol>
   545      *
   545      *
   546      * @param connection the <tt>MBeanServerConnection</tt> to forward to.
   546      * @param connection the {@code MBeanServerConnection} to forward to.
   547      * @param mxbeanName the name of a platform MXBean within
   547      * @param mxbeanName the name of a platform MXBean within
   548      * <tt>connection</tt> to forward to. <tt>mxbeanName</tt> must be
   548      * {@code connection} to forward to. {@code mxbeanName} must be
   549      * in the format of {@link ObjectName ObjectName}.
   549      * in the format of {@link ObjectName ObjectName}.
   550      * @param mxbeanInterface the MXBean interface to be implemented
   550      * @param mxbeanInterface the MXBean interface to be implemented
   551      * by the proxy.
   551      * by the proxy.
   552      * @param <T> an {@code mxbeanInterface} type parameter
   552      * @param <T> an {@code mxbeanInterface} type parameter
   553      *
   553      *
   554      * @return a proxy for a platform MXBean interface of a
   554      * @return a proxy for a platform MXBean interface of a
   555      * given <a href="#MXBeanNames">MXBean name</a>
   555      * given <a href="#MXBeanNames">MXBean name</a>
   556      * that forwards its method calls through the given
   556      * that forwards its method calls through the given
   557      * <tt>MBeanServerConnection</tt>, or {@code null} if not exist.
   557      * {@code MBeanServerConnection}, or {@code null} if not exist.
   558      *
   558      *
   559      * @throws IllegalArgumentException if
   559      * @throws IllegalArgumentException if
   560      * <ul>
   560      * <ul>
   561      * <li><tt>mxbeanName</tt> is not with a valid
   561      * <li>{@code mxbeanName} is not with a valid
   562      *     {@link ObjectName ObjectName} format, or</li>
   562      *     {@link ObjectName ObjectName} format, or</li>
   563      * <li>the named MXBean in the <tt>connection</tt> is
   563      * <li>the named MXBean in the {@code connection} is
   564      *     not a MXBean provided by the platform, or</li>
   564      *     not a MXBean provided by the platform, or</li>
   565      * <li>the named MXBean is not registered in the
   565      * <li>the named MXBean is not registered in the
   566      *     <tt>MBeanServerConnection</tt>, or</li>
   566      *     {@code MBeanServerConnection}, or</li>
   567      * <li>the named MXBean is not an instance of the given
   567      * <li>the named MXBean is not an instance of the given
   568      *     <tt>mxbeanInterface</tt></li>
   568      *     {@code mxbeanInterface}</li>
   569      * </ul>
   569      * </ul>
   570      *
   570      *
   571      * @throws java.io.IOException if a communication problem
   571      * @throws java.io.IOException if a communication problem
   572      * occurred when accessing the <tt>MBeanServerConnection</tt>.
   572      * occurred when accessing the {@code MBeanServerConnection}.
   573      */
   573      */
   574     public static <T> T
   574     public static <T> T
   575         newPlatformMXBeanProxy(MBeanServerConnection connection,
   575         newPlatformMXBeanProxy(MBeanServerConnection connection,
   576                                String mxbeanName,
   576                                String mxbeanName,
   577                                Class<T> mxbeanInterface)
   577                                Class<T> mxbeanInterface)