828 lines · 32.4 KB · Java
RawDownload
/* * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.lang.management;import javax.management.DynamicMBean;import javax.management.MBeanServer;import javax.management.MBeanServerConnection;import javax.management.MBeanServerFactory;import javax.management.MBeanServerPermission;import javax.management.NotificationEmitter;import javax.management.ObjectInstance;import javax.management.ObjectName;import javax.management.InstanceAlreadyExistsException;import javax.management.InstanceNotFoundException;import javax.management.MalformedObjectNameException;import javax.management.MBeanRegistrationException;import javax.management.NotCompliantMBeanException;import javax.management.StandardEmitterMBean;import javax.management.StandardMBean;import java.util.Collections;import java.util.List;import java.util.Set;import java.util.TreeSet;import java.security.AccessController;import java.security.Permission;import java.security.PrivilegedAction;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import javax.management.JMX;import sun.management.ManagementFactoryHelper; /** * The {@code ManagementFactory} class is a factory class for getting * managed beans for the Java platform. * This class consists of static methods each of which returns * one or more <i>platform MXBeans</i> representing * the management interface of a component of the Java virtual * machine. * <p> * <h4><a name="MXBean">Platform MXBeans</a></h4> * <p> * A platform MXBean is a <i>managed bean</i> that * conforms to the <a href="../../../javax/management/package-summary.html">JMX</a> * Instrumentation Specification and only uses a set of basic data types. * A JMX management application and the {@linkplain * #getPlatformMBeanServer platform MBeanServer} * can interoperate without requiring classes for MXBean specific * data types. * The data types being transmitted between the JMX connector * server and the connector client are * {@linkplain javax.management.openmbean.OpenType open types} * and this allows interoperation across versions. * See <a href="../../../javax/management/MXBean.html#MXBean-spec"> * the specification of MXBeans</a> for details. * * <a name="MXBeanNames"></a> * <p>Each platform MXBean is a {@link PlatformManagedObject} * and it has a unique * {@link javax.management.ObjectName ObjectName} for * registration in the platform {@code MBeanServer} as returned by * by the {@link PlatformManagedObject#getObjectName getObjectName} * method. * * <p> * An application can access a platform MXBean in the following ways: * <h5>1. Direct access to an MXBean interface</h5> * <blockquote> * <ul> *     <li>Get an MXBean instance by calling the *         {@link #getPlatformMXBean(Class) getPlatformMXBean} or *         {@link #getPlatformMXBeans(Class) getPlatformMXBeans} method *         and access the MXBean locally in the running *         virtual machine. *         </li> *     <li>Construct an MXBean proxy instance that forwards the *         method calls to a given {@link MBeanServer MBeanServer} by calling *         the {@link #getPlatformMXBean(MBeanServerConnection, Class)} or *         {@link #getPlatformMXBeans(MBeanServerConnection, Class)} method. *         The {@link #newPlatformMXBeanProxy newPlatformMXBeanProxy} method *         can also be used to construct an MXBean proxy instance of *         a given {@code ObjectName}. *         A proxy is typically constructed to remotely access *         an MXBean of another running virtual machine. *         </li> * </ul> * <h5>2. Indirect access to an MXBean interface via MBeanServer</h5> * <ul> *     <li>Go through the platform {@code MBeanServer} to access MXBeans *         locally or a specific <tt>MBeanServerConnection</tt> to access *         MXBeans remotely. *         The attributes and operations of an MXBean use only *         <em>JMX open types</em> which include basic data types, *         {@link javax.management.openmbean.CompositeData CompositeData}, *         and {@link javax.management.openmbean.TabularData TabularData} *         defined in *         {@link javax.management.openmbean.OpenType OpenType}. *         The mapping is specified in *         the {@linkplain javax.management.MXBean MXBean} specification *         for details. *        </li> * </ul> * </blockquote> * * <p> * The {@link #getPlatformManagementInterfaces getPlatformManagementInterfaces} * method returns all management interfaces supported in the Java virtual machine * including the standard management interfaces listed in the tables * below as well as the management interfaces extended by the JDK implementation. * <p> * A Java virtual machine has a single instance of the following management * interfaces: * * <blockquote> * <table border> * <tr> * <th>Management Interface</th> * <th>ObjectName</th> * </tr> * <tr> * <td> {@link ClassLoadingMXBean} </td> * <td> {@link #CLASS_LOADING_MXBEAN_NAME *             java.lang:type=ClassLoading}</td> * </tr> * <tr> * <td> {@link MemoryMXBean} </td> * <td> {@link #MEMORY_MXBEAN_NAME *             java.lang:type=Memory}</td> * </tr> * <tr> * <td> {@link ThreadMXBean} </td> * <td> {@link #THREAD_MXBEAN_NAME *             java.lang:type=Threading}</td> * </tr> * <tr> * <td> {@link RuntimeMXBean} </td> * <td> {@link #RUNTIME_MXBEAN_NAME *             java.lang:type=Runtime}</td> * </tr> * <tr> * <td> {@link OperatingSystemMXBean} </td> * <td> {@link #OPERATING_SYSTEM_MXBEAN_NAME *             java.lang:type=OperatingSystem}</td> * </tr> * <tr> * <td> {@link PlatformLoggingMXBean} </td> * <td> {@link java.util.logging.LogManager#LOGGING_MXBEAN_NAME *             java.util.logging:type=Logging}</td> * </tr> * </table> * </blockquote> * * <p> * A Java virtual machine has zero or a single instance of * the following management interfaces. * * <blockquote> * <table border> * <tr> * <th>Management Interface</th> * <th>ObjectName</th> * </tr> * <tr> * <td> {@link CompilationMXBean} </td> * <td> {@link #COMPILATION_MXBEAN_NAME *             java.lang:type=Compilation}</td> * </tr> * </table> * </blockquote> * * <p> * A Java virtual machine may have one or more instances of the following * management interfaces. * <blockquote> * <table border> * <tr> * <th>Management Interface</th> * <th>ObjectName</th> * </tr> * <tr> * <td> {@link GarbageCollectorMXBean} </td> * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE *             java.lang:type=GarbageCollector}<tt>,name=</tt><i>collector's name</i></td> * </tr> * <tr> * <td> {@link MemoryManagerMXBean} </td> * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE *             java.lang:type=MemoryManager}<tt>,name=</tt><i>manager's name</i></td> * </tr> * <tr> * <td> {@link MemoryPoolMXBean} </td> * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE *             java.lang:type=MemoryPool}<tt>,name=</tt><i>pool's name</i></td> * </tr> * <tr> * <td> {@link BufferPoolMXBean} </td> * <td> {@code java.nio:type=BufferPool,name=}<i>pool name</i></td> * </tr> * </table> * </blockquote> * * @see <a href="../../../javax/management/package-summary.html"> *      JMX Specification</a> * @see <a href="package-summary.html#examples"> *      Ways to Access Management Metrics</a> * @see javax.management.MXBean * * @author  Mandy Chung * @since   1.5 */public class ManagementFactory {    // A class with only static fields and methods.    private ManagementFactory() {};     /**     * String representation of the     * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.     */    public final static String CLASS_LOADING_MXBEAN_NAME =        "java.lang:type=ClassLoading";     /**     * String representation of the     * <tt>ObjectName</tt> for the {@link CompilationMXBean}.     */    public final static String COMPILATION_MXBEAN_NAME =        "java.lang:type=Compilation";     /**     * String representation of the     * <tt>ObjectName</tt> for the {@link MemoryMXBean}.     */    public final static String MEMORY_MXBEAN_NAME =        "java.lang:type=Memory";     /**     * String representation of the     * <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.     */    public final static String OPERATING_SYSTEM_MXBEAN_NAME =        "java.lang:type=OperatingSystem";     /**     * String representation of the     * <tt>ObjectName</tt> for the {@link RuntimeMXBean}.     */    public final static String RUNTIME_MXBEAN_NAME =        "java.lang:type=Runtime";     /**     * String representation of the     * <tt>ObjectName</tt> for the {@link ThreadMXBean}.     */    public final static String THREAD_MXBEAN_NAME =        "java.lang:type=Threading";     /**     * The domain name and the type key property in     * the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.     * The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>     * can be formed by appending this string with     * "<tt>,name=</tt><i>collector's name</i>".     */    public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =        "java.lang:type=GarbageCollector";     /**     * The domain name and the type key property in     * the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.     * The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>     * can be formed by appending this string with     * "<tt>,name=</tt><i>manager's name</i>".     */    public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=        "java.lang:type=MemoryManager";     /**     * The domain name and the type key property in     * the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.     * The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>     * can be formed by appending this string with     * <tt>,name=</tt><i>pool's name</i>.     */    public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE=        "java.lang:type=MemoryPool";     /**     * Returns the managed bean for the class loading system of     * the Java virtual machine.     *     * @return a {@link ClassLoadingMXBean} object for     * the Java virtual machine.     */    public static ClassLoadingMXBean getClassLoadingMXBean() {        return ManagementFactoryHelper.getClassLoadingMXBean();    }     /**     * Returns the managed bean for the memory system of     * the Java virtual machine.     *     * @return a {@link MemoryMXBean} object for the Java virtual machine.     */    public static MemoryMXBean getMemoryMXBean() {        return ManagementFactoryHelper.getMemoryMXBean();    }     /**     * Returns the managed bean for the thread system of     * the Java virtual machine.     *     * @return a {@link ThreadMXBean} object for the Java virtual machine.     */    public static ThreadMXBean getThreadMXBean() {        return ManagementFactoryHelper.getThreadMXBean();    }     /**     * Returns the managed bean for the runtime system of     * the Java virtual machine.     *     * @return a {@link RuntimeMXBean} object for the Java virtual machine.      */    public static RuntimeMXBean getRuntimeMXBean() {        return ManagementFactoryHelper.getRuntimeMXBean();    }     /**     * Returns the managed bean for the compilation system of     * the Java virtual machine.  This method returns <tt>null</tt>     * if the Java virtual machine has no compilation system.     *     * @return a {@link CompilationMXBean} object for the Java virtual     *   machine or <tt>null</tt> if the Java virtual machine has     *   no compilation system.     */    public static CompilationMXBean getCompilationMXBean() {        return ManagementFactoryHelper.getCompilationMXBean();    }     /**     * Returns the managed bean for the operating system on which     * the Java virtual machine is running.     *     * @return an {@link OperatingSystemMXBean} object for     * the Java virtual machine.     */    public static OperatingSystemMXBean getOperatingSystemMXBean() {        return ManagementFactoryHelper.getOperatingSystemMXBean();    }     /**     * Returns a list of {@link MemoryPoolMXBean} objects in the     * Java virtual machine.     * The Java virtual machine can have one or more memory pools.     * It may add or remove memory pools during execution.     *     * @return a list of <tt>MemoryPoolMXBean</tt> objects.     *     */    public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {        return ManagementFactoryHelper.getMemoryPoolMXBeans();    }     /**     * Returns a list of {@link MemoryManagerMXBean} objects     * in the Java virtual machine.     * The Java virtual machine can have one or more memory managers.     * It may add or remove memory managers during execution.     *     * @return a list of <tt>MemoryManagerMXBean</tt> objects.     *     */    public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {        return ManagementFactoryHelper.getMemoryManagerMXBeans();    }      /**     * Returns a list of {@link GarbageCollectorMXBean} objects     * in the Java virtual machine.     * The Java virtual machine may have one or more     * <tt>GarbageCollectorMXBean</tt> objects.     * It may add or remove <tt>GarbageCollectorMXBean</tt>     * during execution.     *     * @return a list of <tt>GarbageCollectorMXBean</tt> objects.     *     */    public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {        return ManagementFactoryHelper.getGarbageCollectorMXBeans();    }     private static MBeanServer platformMBeanServer;    /**     * Returns the platform {@link javax.management.MBeanServer MBeanServer}.     * On the first call to this method, it first creates the platform     * {@code MBeanServer} by calling the     * {@link javax.management.MBeanServerFactory#createMBeanServer     * MBeanServerFactory.createMBeanServer}     * method and registers each platform MXBean in this platform     * {@code MBeanServer} with its     * {@link PlatformManagedObject#getObjectName ObjectName}.     * This method, in subsequent calls, will simply return the     * initially created platform {@code MBeanServer}.     * <p>     * MXBeans that get created and destroyed dynamically, for example,     * memory {@link MemoryPoolMXBean pools} and     * {@link MemoryManagerMXBean managers},     * will automatically be registered and deregistered into the platform     * {@code MBeanServer}.     * <p>     * If the system property {@code javax.management.builder.initial}     * is set, the platform {@code MBeanServer} creation will be done     * by the specified {@link javax.management.MBeanServerBuilder}.     * <p>     * It is recommended that this platform MBeanServer also be used     * to register other application managed beans     * besides the platform MXBeans.     * This will allow all MBeans to be published through the same     * {@code MBeanServer} and hence allow for easier network publishing     * and discovery.     * Name conflicts with the platform MXBeans should be avoided.     *     * @return the platform {@code MBeanServer}; the platform     *         MXBeans are registered into the platform {@code MBeanServer}     *         at the first time this method is called.     *     * @exception SecurityException if there is a security manager     * and the caller does not have the permission required by     * {@link javax.management.MBeanServerFactory#createMBeanServer}.     *     * @see javax.management.MBeanServerFactory     * @see javax.management.MBeanServerFactory#createMBeanServer     */    public static synchronized MBeanServer getPlatformMBeanServer() {        SecurityManager sm = System.getSecurityManager();        if (sm != null) {            Permission perm = new MBeanServerPermission("createMBeanServer");            sm.checkPermission(perm);        }         if (platformMBeanServer == null) {            platformMBeanServer = MBeanServerFactory.createMBeanServer();            for (PlatformComponent pc : PlatformComponent.values()) {                List<? extends PlatformManagedObject> list =                    pc.getMXBeans(pc.getMXBeanInterface());                for (PlatformManagedObject o : list) {                    // Each PlatformComponent represents one management                    // interface. Some MXBean may extend another one.                    // The MXBean instances for one platform component                    // (returned by pc.getMXBeans()) might be also                    // the MXBean instances for another platform component.                    // e.g. com.sun.management.GarbageCollectorMXBean                    //                    // So need to check if an MXBean instance is registered                    // before registering into the platform MBeanServer                    if (!platformMBeanServer.isRegistered(o.getObjectName())) {                        addMXBean(platformMBeanServer, o);                    }                }            }        }        return platformMBeanServer;    }     /**     * Returns a proxy for a platform MXBean interface of a     * given <a href="#MXBeanNames">MXBean name</a>     * that forwards its method calls through the given     * <tt>MBeanServerConnection</tt>.     *     * <p>This method is equivalent to:     * <blockquote>     * {@link java.lang.reflect.Proxy#newProxyInstance     *        Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),     *        new Class[] { mxbeanInterface }, handler)</tt>     * </blockquote>

Lines 1–500 of 828.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t