0N/A/*
4904N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang.management;
178N/Aimport javax.management.DynamicMBean;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerConnection;
178N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.MBeanServerPermission;
178N/Aimport javax.management.NotificationEmitter;
178N/Aimport javax.management.ObjectInstance;
0N/Aimport javax.management.ObjectName;
178N/Aimport javax.management.InstanceAlreadyExistsException;
0N/Aimport javax.management.InstanceNotFoundException;
0N/Aimport javax.management.MalformedObjectNameException;
178N/Aimport javax.management.MBeanRegistrationException;
178N/Aimport javax.management.NotCompliantMBeanException;
178N/Aimport javax.management.StandardEmitterMBean;
178N/Aimport javax.management.StandardMBean;
178N/Aimport java.util.Collections;
0N/Aimport java.util.List;
3887N/Aimport java.util.Set;
4904N/Aimport java.util.HashSet;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.Permission;
0N/Aimport java.security.PrivilegedAction;
178N/Aimport java.security.PrivilegedActionException;
178N/Aimport java.security.PrivilegedExceptionAction;
0N/Aimport javax.management.JMX;
178N/Aimport sun.management.ManagementFactoryHelper;
0N/A
0N/A/**
3887N/A * The {@code ManagementFactory} class is a factory class for getting
0N/A * managed beans for the Java platform.
0N/A * This class consists of static methods each of which returns
3887N/A * one or more <i>platform MXBeans</i> representing
0N/A * the management interface of a component of the Java virtual
0N/A * machine.
3887N/A * <p>
3887N/A * <h4><a name="MXBean">Platform MXBeans</a></h4>
3887N/A * <p>
3887N/A * A platform MXBean is a <i>managed bean</i> that
3887N/A * conforms to the <a href="../../../javax/management/package-summary.html">JMX</a>
3887N/A * Instrumentation Specification and only uses a set of basic data types.
3887N/A * A JMX management application and the {@linkplain
3887N/A * #getPlatformMBeanServer platform MBeanServer}
3887N/A * can interoperate without requiring classes for MXBean specific
3887N/A * data types.
3887N/A * The data types being transmitted between the JMX connector
3887N/A * server and the connector client are
3887N/A * {@linkplain javax.management.openmbean.OpenType open types}
3887N/A * and this allows interoperation across versions.
3887N/A * See <a href="../../../javax/management/MXBean.html#MXBean-spec">
3887N/A * the specification of MXBeans</a> for details.
3887N/A *
3887N/A * <a name="MXBeanNames"></a>
3887N/A * <p>Each platform MXBean is a {@link PlatformManagedObject}
3887N/A * and it has a unique
3887N/A * {@link javax.management.ObjectName ObjectName} for
3887N/A * registration in the platform {@code MBeanServer} as returned by
3887N/A * by the {@link PlatformManagedObject#getObjectName getObjectName}
3887N/A * method.
0N/A *
0N/A * <p>
0N/A * An application can access a platform MXBean in the following ways:
3887N/A * <h5>1. Direct access to an MXBean interface</h5>
3887N/A * <blockquote>
0N/A * <ul>
3887N/A * <li>Get an MXBean instance by calling the
3887N/A * {@link #getPlatformMXBean(Class) getPlatformMXBean} or
3887N/A * {@link #getPlatformMXBeans(Class) getPlatformMXBeans} method
3887N/A * and access the MXBean locally in the running
0N/A * virtual machine.
0N/A * </li>
0N/A * <li>Construct an MXBean proxy instance that forwards the
0N/A * method calls to a given {@link MBeanServer MBeanServer} by calling
3887N/A * the {@link #getPlatformMXBean(MBeanServerConnection, Class)} or
3887N/A * {@link #getPlatformMXBeans(MBeanServerConnection, Class)} method.
3887N/A * The {@link #newPlatformMXBeanProxy newPlatformMXBeanProxy} method
3887N/A * can also be used to construct an MXBean proxy instance of
3887N/A * a given {@code ObjectName}.
0N/A * A proxy is typically constructed to remotely access
0N/A * an MXBean of another running virtual machine.
0N/A * </li>
3887N/A * </ul>
3887N/A * <h5>2. Indirect access to an MXBean interface via MBeanServer</h5>
3887N/A * <ul>
3887N/A * <li>Go through the platform {@code MBeanServer} to access MXBeans
3887N/A * locally or a specific <tt>MBeanServerConnection</tt> to access
0N/A * MXBeans remotely.
0N/A * The attributes and operations of an MXBean use only
0N/A * <em>JMX open types</em> which include basic data types,
0N/A * {@link javax.management.openmbean.CompositeData CompositeData},
0N/A * and {@link javax.management.openmbean.TabularData TabularData}
0N/A * defined in
0N/A * {@link javax.management.openmbean.OpenType OpenType}.
3887N/A * The mapping is specified in
3887N/A * the {@linkplain javax.management.MXBean MXBean} specification
3887N/A * for details.
0N/A * </li>
0N/A * </ul>
3887N/A * </blockquote>
0N/A *
0N/A * <p>
3887N/A * The {@link #getPlatformManagementInterfaces getPlatformManagementInterfaces}
3887N/A * method returns all management interfaces supported in the Java virtual machine
3887N/A * including the standard management interfaces listed in the tables
3887N/A * below as well as the management interfaces extended by the JDK implementation.
0N/A * <p>
0N/A * A Java virtual machine has a single instance of the following management
0N/A * interfaces:
0N/A *
0N/A * <blockquote>
0N/A * <table border>
0N/A * <tr>
0N/A * <th>Management Interface</th>
0N/A * <th>ObjectName</th>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link ClassLoadingMXBean} </td>
0N/A * <td> {@link #CLASS_LOADING_MXBEAN_NAME
3887N/A * java.lang:type=ClassLoading}</td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link MemoryMXBean} </td>
0N/A * <td> {@link #MEMORY_MXBEAN_NAME
3887N/A * java.lang:type=Memory}</td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link ThreadMXBean} </td>
0N/A * <td> {@link #THREAD_MXBEAN_NAME
3887N/A * java.lang:type=Threading}</td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link RuntimeMXBean} </td>
0N/A * <td> {@link #RUNTIME_MXBEAN_NAME
3887N/A * java.lang:type=Runtime}</td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link OperatingSystemMXBean} </td>
0N/A * <td> {@link #OPERATING_SYSTEM_MXBEAN_NAME
3887N/A * java.lang:type=OperatingSystem}</td>
3887N/A * </tr>
3887N/A * <tr>
3887N/A * <td> {@link PlatformLoggingMXBean} </td>
3887N/A * <td> {@link java.util.logging.LogManager#LOGGING_MXBEAN_NAME
3887N/A * java.util.logging:type=Logging}</td>
0N/A * </tr>
0N/A * </table>
0N/A * </blockquote>
0N/A *
0N/A * <p>
0N/A * A Java virtual machine has zero or a single instance of
0N/A * the following management interfaces.
0N/A *
0N/A * <blockquote>
0N/A * <table border>
0N/A * <tr>
0N/A * <th>Management Interface</th>
0N/A * <th>ObjectName</th>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link CompilationMXBean} </td>
0N/A * <td> {@link #COMPILATION_MXBEAN_NAME
3887N/A * java.lang:type=Compilation}</td>
0N/A * </tr>
0N/A * </table>
0N/A * </blockquote>
0N/A *
0N/A * <p>
0N/A * A Java virtual machine may have one or more instances of the following
0N/A * management interfaces.
0N/A * <blockquote>
0N/A * <table border>
0N/A * <tr>
0N/A * <th>Management Interface</th>
0N/A * <th>ObjectName</th>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link GarbageCollectorMXBean} </td>
0N/A * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
3887N/A * java.lang:type=GarbageCollector}<tt>,name=</tt><i>collector's name</i></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link MemoryManagerMXBean} </td>
0N/A * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
3887N/A * java.lang:type=MemoryManager}<tt>,name=</tt><i>manager's name</i></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td> {@link MemoryPoolMXBean} </td>
0N/A * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
3887N/A * java.lang:type=MemoryPool}<tt>,name=</tt><i>pool's name</i></td>
3887N/A * </tr>
3887N/A * <tr>
3887N/A * <td> {@link BufferPoolMXBean} </td>
3887N/A * <td> {@code java.nio:type=BufferPool,name=}<i>pool name</i></td>
0N/A * </tr>
0N/A * </table>
0N/A * </blockquote>
0N/A *
0N/A * @see <a href="../../../javax/management/package-summary.html">
178N/A * JMX Specification</a>
0N/A * @see <a href="package-summary.html#examples">
0N/A * Ways to Access Management Metrics</a>
0N/A * @see javax.management.MXBean
0N/A *
0N/A * @author Mandy Chung
0N/A * @since 1.5
0N/A */
0N/Apublic class ManagementFactory {
0N/A // A class with only static fields and methods.
0N/A private ManagementFactory() {};
0N/A
0N/A /**
0N/A * String representation of the
0N/A * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
0N/A */
0N/A public final static String CLASS_LOADING_MXBEAN_NAME =
0N/A "java.lang:type=ClassLoading";
0N/A
0N/A /**
0N/A * String representation of the
0N/A * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
0N/A */
0N/A public final static String COMPILATION_MXBEAN_NAME =
0N/A "java.lang:type=Compilation";
0N/A
0N/A /**
0N/A * String representation of the
0N/A * <tt>ObjectName</tt> for the {@link MemoryMXBean}.
0N/A */
0N/A public final static String MEMORY_MXBEAN_NAME =
0N/A "java.lang:type=Memory";
0N/A
0N/A /**
0N/A * String representation of the
0N/A * <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.
0N/A */
0N/A public final static String OPERATING_SYSTEM_MXBEAN_NAME =
0N/A "java.lang:type=OperatingSystem";
0N/A
0N/A /**
0N/A * String representation of the
0N/A * <tt>ObjectName</tt> for the {@link RuntimeMXBean}.
0N/A */
0N/A public final static String RUNTIME_MXBEAN_NAME =
0N/A "java.lang:type=Runtime";
0N/A
0N/A /**
0N/A * String representation of the
0N/A * <tt>ObjectName</tt> for the {@link ThreadMXBean}.
0N/A */
0N/A public final static String THREAD_MXBEAN_NAME =
0N/A "java.lang:type=Threading";
0N/A
0N/A /**
0N/A * The domain name and the type key property in
0N/A * the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.
0N/A * The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>
0N/A * can be formed by appending this string with
0N/A * "<tt>,name=</tt><i>collector's name</i>".
0N/A */
0N/A public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
0N/A "java.lang:type=GarbageCollector";
0N/A
0N/A /**
0N/A * The domain name and the type key property in
0N/A * the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.
0N/A * The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>
0N/A * can be formed by appending this string with
0N/A * "<tt>,name=</tt><i>manager's name</i>".
0N/A */
0N/A public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=
0N/A "java.lang:type=MemoryManager";
0N/A
0N/A /**
0N/A * The domain name and the type key property in
0N/A * the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.
0N/A * The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>
0N/A * can be formed by appending this string with
0N/A * <tt>,name=</tt><i>pool's name</i>.
0N/A */
0N/A public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE=
0N/A "java.lang:type=MemoryPool";
0N/A
0N/A /**
0N/A * Returns the managed bean for the class loading system of
0N/A * the Java virtual machine.
0N/A *
0N/A * @return a {@link ClassLoadingMXBean} object for
0N/A * the Java virtual machine.
0N/A */
0N/A public static ClassLoadingMXBean getClassLoadingMXBean() {
178N/A return ManagementFactoryHelper.getClassLoadingMXBean();
0N/A }
0N/A
0N/A /**
0N/A * Returns the managed bean for the memory system of
0N/A * the Java virtual machine.
0N/A *
0N/A * @return a {@link MemoryMXBean} object for the Java virtual machine.
0N/A */
0N/A public static MemoryMXBean getMemoryMXBean() {
178N/A return ManagementFactoryHelper.getMemoryMXBean();
0N/A }
0N/A
0N/A /**
0N/A * Returns the managed bean for the thread system of
0N/A * the Java virtual machine.
0N/A *
0N/A * @return a {@link ThreadMXBean} object for the Java virtual machine.
0N/A */
0N/A public static ThreadMXBean getThreadMXBean() {
178N/A return ManagementFactoryHelper.getThreadMXBean();
0N/A }
0N/A
0N/A /**
0N/A * Returns the managed bean for the runtime system of
0N/A * the Java virtual machine.
0N/A *
0N/A * @return a {@link RuntimeMXBean} object for the Java virtual machine.
0N/A
0N/A */
0N/A public static RuntimeMXBean getRuntimeMXBean() {
178N/A return ManagementFactoryHelper.getRuntimeMXBean();
0N/A }
0N/A
0N/A /**
0N/A * Returns the managed bean for the compilation system of
0N/A * the Java virtual machine. This method returns <tt>null</tt>
0N/A * if the Java virtual machine has no compilation system.
0N/A *
0N/A * @return a {@link CompilationMXBean} object for the Java virtual
0N/A * machine or <tt>null</tt> if the Java virtual machine has
0N/A * no compilation system.
0N/A */
0N/A public static CompilationMXBean getCompilationMXBean() {
178N/A return ManagementFactoryHelper.getCompilationMXBean();
0N/A }
0N/A
0N/A /**
0N/A * Returns the managed bean for the operating system on which
0N/A * the Java virtual machine is running.
0N/A *
0N/A * @return an {@link OperatingSystemMXBean} object for
0N/A * the Java virtual machine.
0N/A */
0N/A public static OperatingSystemMXBean getOperatingSystemMXBean() {
178N/A return ManagementFactoryHelper.getOperatingSystemMXBean();
0N/A }
0N/A
0N/A /**
0N/A * Returns a list of {@link MemoryPoolMXBean} objects in the
0N/A * Java virtual machine.
0N/A * The Java virtual machine can have one or more memory pools.
0N/A * It may add or remove memory pools during execution.
0N/A *
0N/A * @return a list of <tt>MemoryPoolMXBean</tt> objects.
0N/A *
0N/A */
0N/A public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
178N/A return ManagementFactoryHelper.getMemoryPoolMXBeans();
0N/A }
0N/A
0N/A /**
0N/A * Returns a list of {@link MemoryManagerMXBean} objects
0N/A * in the Java virtual machine.
0N/A * The Java virtual machine can have one or more memory managers.
0N/A * It may add or remove memory managers during execution.
0N/A *
0N/A * @return a list of <tt>MemoryManagerMXBean</tt> objects.
0N/A *
0N/A */
0N/A public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
178N/A return ManagementFactoryHelper.getMemoryManagerMXBeans();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a list of {@link GarbageCollectorMXBean} objects
0N/A * in the Java virtual machine.
0N/A * The Java virtual machine may have one or more
0N/A * <tt>GarbageCollectorMXBean</tt> objects.
0N/A * It may add or remove <tt>GarbageCollectorMXBean</tt>
0N/A * during execution.
0N/A *
0N/A * @return a list of <tt>GarbageCollectorMXBean</tt> objects.
0N/A *
0N/A */
0N/A public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
178N/A return ManagementFactoryHelper.getGarbageCollectorMXBeans();
0N/A }
0N/A
0N/A private static MBeanServer platformMBeanServer;
0N/A /**
0N/A * Returns the platform {@link javax.management.MBeanServer MBeanServer}.
0N/A * On the first call to this method, it first creates the platform
3887N/A * {@code MBeanServer} by calling the
0N/A * {@link javax.management.MBeanServerFactory#createMBeanServer
0N/A * MBeanServerFactory.createMBeanServer}
3887N/A * method and registers each platform MXBean in this platform
3887N/A * {@code MBeanServer} with its
3887N/A * {@link PlatformManagedObject#getObjectName ObjectName}.
0N/A * This method, in subsequent calls, will simply return the
3887N/A * initially created platform {@code MBeanServer}.
0N/A * <p>
0N/A * MXBeans that get created and destroyed dynamically, for example,
0N/A * memory {@link MemoryPoolMXBean pools} and
0N/A * {@link MemoryManagerMXBean managers},
0N/A * will automatically be registered and deregistered into the platform
3887N/A * {@code MBeanServer}.
0N/A * <p>
3887N/A * If the system property {@code javax.management.builder.initial}
3887N/A * is set, the platform {@code MBeanServer} creation will be done
0N/A * by the specified {@link javax.management.MBeanServerBuilder}.
0N/A * <p>
0N/A * It is recommended that this platform MBeanServer also be used
0N/A * to register other application managed beans
0N/A * besides the platform MXBeans.
0N/A * This will allow all MBeans to be published through the same
3887N/A * {@code MBeanServer} and hence allow for easier network publishing
0N/A * and discovery.
0N/A * Name conflicts with the platform MXBeans should be avoided.
0N/A *
3887N/A * @return the platform {@code MBeanServer}; the platform
3887N/A * MXBeans are registered into the platform {@code MBeanServer}
0N/A * at the first time this method is called.
0N/A *
0N/A * @exception SecurityException if there is a security manager
0N/A * and the caller does not have the permission required by
0N/A * {@link javax.management.MBeanServerFactory#createMBeanServer}.
0N/A *
0N/A * @see javax.management.MBeanServerFactory
0N/A * @see javax.management.MBeanServerFactory#createMBeanServer
0N/A */
0N/A public static synchronized MBeanServer getPlatformMBeanServer() {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A Permission perm = new MBeanServerPermission("createMBeanServer");
0N/A sm.checkPermission(perm);
0N/A }
0N/A
0N/A if (platformMBeanServer == null) {
178N/A platformMBeanServer = MBeanServerFactory.createMBeanServer();
178N/A for (PlatformComponent pc : PlatformComponent.values()) {
178N/A List<? extends PlatformManagedObject> list =
178N/A pc.getMXBeans(pc.getMXBeanInterface());
178N/A for (PlatformManagedObject o : list) {
178N/A // Each PlatformComponent represents one management
178N/A // interface. Some MXBean may extend another one.
178N/A // The MXBean instances for one platform component
178N/A // (returned by pc.getMXBeans()) might be also
178N/A // the MXBean instances for another platform component.
178N/A // e.g. com.sun.management.GarbageCollectorMXBean
178N/A //
178N/A // So need to check if an MXBean instance is registered
178N/A // before registering into the platform MBeanServer
178N/A if (!platformMBeanServer.isRegistered(o.getObjectName())) {
178N/A addMXBean(platformMBeanServer, o);
178N/A }
178N/A }
178N/A }
0N/A }
0N/A return platformMBeanServer;
0N/A }
0N/A
0N/A /**
0N/A * Returns a proxy for a platform MXBean interface of a
0N/A * given <a href="#MXBeanNames">MXBean name</a>
0N/A * that forwards its method calls through the given
0N/A * <tt>MBeanServerConnection</tt>.
0N/A *
0N/A * <p>This method is equivalent to:
0N/A * <blockquote>
0N/A * {@link java.lang.reflect.Proxy#newProxyInstance
0N/A * Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),
0N/A * new Class[] { mxbeanInterface }, handler)</tt>
0N/A * </blockquote>
0N/A *
0N/A * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler
0N/A * InvocationHandler} to which method invocations to the MXBean interface
0N/A * are dispatched. This <tt>handler</tt> converts an input parameter
0N/A * from an MXBean data type to its mapped open type before forwarding
0N/A * to the <tt>MBeanServer</tt> and converts a return value from
0N/A * an MXBean method call through the <tt>MBeanServer</tt>
0N/A * from an open type to the corresponding return type declared in
0N/A * the MXBean interface.
0N/A *
0N/A * <p>
0N/A * If the MXBean is a notification emitter (i.e.,
0N/A * it implements
0N/A * {@link javax.management.NotificationEmitter NotificationEmitter}),
0N/A * both the <tt>mxbeanInterface</tt> and <tt>NotificationEmitter</tt>
0N/A * will be implemented by this proxy.
0N/A *
0N/A * <p>
0N/A * <b>Notes:</b>
0N/A * <ol>
0N/A * <li>Using an MXBean proxy is a convenience remote access to
0N/A * a platform MXBean of a running virtual machine. All method
0N/A * calls to the MXBean proxy are forwarded to an
0N/A * <tt>MBeanServerConnection</tt> where
0N/A * {@link java.io.IOException IOException} may be thrown
0N/A * when the communication problem occurs with the connector server.
0N/A * An application remotely accesses the platform MXBeans using
0N/A * proxy should prepare to catch <tt>IOException</tt> as if
0N/A * accessing with the <tt>MBeanServerConnector</tt> interface.</li>
0N/A *
0N/A * <li>When a client application is designed to remotely access MXBeans
0N/A * for a running virtual machine whose version is different than
0N/A * the version on which the application is running,
0N/A * it should prepare to catch
0N/A * {@link java.io.InvalidObjectException InvalidObjectException}
0N/A * which is thrown when an MXBean proxy receives a name of an
0N/A * enum constant which is missing in the enum class loaded in
0N/A * the client application. </li>
0N/A *
0N/A * <li>{@link javax.management.MBeanServerInvocationHandler
0N/A * MBeanServerInvocationHandler} or its
0N/A * {@link javax.management.MBeanServerInvocationHandler#newProxyInstance
0N/A * newProxyInstance} method cannot be used to create
0N/A * a proxy for a platform MXBean. The proxy object created
0N/A * by <tt>MBeanServerInvocationHandler</tt> does not handle
0N/A * the properties of the platform MXBeans described in
0N/A * the <a href="#MXBean">class specification</a>.
0N/A *</li>
0N/A * </ol>
0N/A *
0N/A * @param connection the <tt>MBeanServerConnection</tt> to forward to.
0N/A * @param mxbeanName the name of a platform MXBean within
0N/A * <tt>connection</tt> to forward to. <tt>mxbeanName</tt> must be
0N/A * in the format of {@link ObjectName ObjectName}.
0N/A * @param mxbeanInterface the MXBean interface to be implemented
0N/A * by the proxy.
0N/A *
0N/A * @throws IllegalArgumentException if
0N/A * <ul>
0N/A * <li><tt>mxbeanName</tt> is not with a valid
0N/A * {@link ObjectName ObjectName} format, or</li>
0N/A * <li>the named MXBean in the <tt>connection</tt> is
0N/A * not a MXBean provided by the platform, or</li>
0N/A * <li>the named MXBean is not registered in the
0N/A * <tt>MBeanServerConnection</tt>, or</li>
0N/A * <li>the named MXBean is not an instance of the given
0N/A * <tt>mxbeanInterface</tt></li>
0N/A * </ul>
0N/A *
0N/A * @throws java.io.IOException if a communication problem
0N/A * occurred when accessing the <tt>MBeanServerConnection</tt>.
0N/A */
0N/A public static <T> T
0N/A newPlatformMXBeanProxy(MBeanServerConnection connection,
0N/A String mxbeanName,
0N/A Class<T> mxbeanInterface)
0N/A throws java.io.IOException {
0N/A
0N/A final Class interfaceClass = mxbeanInterface;
0N/A // Only allow MXBean interfaces from rt.jar loaded by the
0N/A // bootstrap class loader
0N/A final ClassLoader loader =
0N/A AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
0N/A public ClassLoader run() {
0N/A return interfaceClass.getClassLoader();
0N/A }
0N/A });
0N/A if (loader != null) {
0N/A throw new IllegalArgumentException(mxbeanName +
0N/A " is not a platform MXBean");
0N/A }
0N/A
0N/A try {
0N/A final ObjectName objName = new ObjectName(mxbeanName);
3887N/A // skip the isInstanceOf check for LoggingMXBean
3887N/A String intfName = interfaceClass.getName();
3887N/A if (!connection.isInstanceOf(objName, intfName)) {
0N/A throw new IllegalArgumentException(mxbeanName +
0N/A " is not an instance of " + interfaceClass);
0N/A }
0N/A
0N/A final Class[] interfaces;
0N/A // check if the registered MBean is a notification emitter
0N/A boolean emitter = connection.isInstanceOf(objName, NOTIF_EMITTER);
0N/A
0N/A // create an MXBean proxy
0N/A return JMX.newMXBeanProxy(connection, objName, mxbeanInterface,
0N/A emitter);
3887N/A } catch (InstanceNotFoundException|MalformedObjectNameException e) {
3887N/A throw new IllegalArgumentException(e);
0N/A }
0N/A }
0N/A
178N/A /**
3887N/A * Returns the platform MXBean implementing
3887N/A * the given {@code mxbeanInterface} which is specified
3887N/A * to have one single instance in the Java virtual machine.
3887N/A * This method may return {@code null} if the management interface
3887N/A * is not implemented in the Java virtual machine (for example,
3887N/A * a Java virtual machine with no compilation system does not
3887N/A * implement {@link CompilationMXBean});
3887N/A * otherwise, this method is equivalent to calling:
3887N/A * <pre>
3887N/A * {@link #getPlatformMXBeans(Class)
3887N/A * getPlatformMXBeans(mxbeanInterface)}.get(0);
3887N/A * </pre>
3887N/A *
3887N/A * @param mxbeanInterface a management interface for a platform
3887N/A * MXBean with one single instance in the Java virtual machine
3887N/A * if implemented.
3887N/A *
3887N/A * @return the platform MXBean that implements
3887N/A * {@code mxbeanInterface}, or {@code null} if not exist.
3887N/A *
3887N/A * @throws IllegalArgumentException if {@code mxbeanInterface}
3887N/A * is not a platform management interface or
3887N/A * not a singleton platform MXBean.
3887N/A *
3887N/A * @since 1.7
3887N/A */
3887N/A public static <T extends PlatformManagedObject>
3887N/A T getPlatformMXBean(Class<T> mxbeanInterface) {
3887N/A PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
3887N/A if (pc == null)
3887N/A throw new IllegalArgumentException(mxbeanInterface.getName() +
3887N/A " is not a platform management interface");
3887N/A if (!pc.isSingleton())
3887N/A throw new IllegalArgumentException(mxbeanInterface.getName() +
3887N/A " can have zero or more than one instances");
3887N/A
3887N/A return pc.getSingletonMXBean(mxbeanInterface);
3887N/A }
3887N/A
3887N/A /**
3887N/A * Returns the list of platform MXBeans implementing
3887N/A * the given {@code mxbeanInterface} in the Java
178N/A * virtual machine.
178N/A * The returned list may contain zero, one, or more instances.
178N/A * The number of instances in the returned list is defined
178N/A * in the specification of the given management interface.
3887N/A * The order is undefined and there is no guarantee that
3887N/A * the list returned is in the same order as previous invocations.
178N/A *
178N/A * @param mxbeanInterface a management interface for a platform
178N/A * MXBean
178N/A *
3887N/A * @return the list of platform MXBeans that implement
178N/A * {@code mxbeanInterface}.
178N/A *
178N/A * @throws IllegalArgumentException if {@code mxbeanInterface}
3887N/A * is not a platform management interface.
178N/A *
178N/A * @since 1.7
178N/A */
178N/A public static <T extends PlatformManagedObject> List<T>
178N/A getPlatformMXBeans(Class<T> mxbeanInterface) {
3887N/A PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
3887N/A if (pc == null)
3887N/A throw new IllegalArgumentException(mxbeanInterface.getName() +
3887N/A " is not a platform management interface");
3887N/A return Collections.unmodifiableList(pc.getMXBeans(mxbeanInterface));
3887N/A }
3887N/A
3887N/A /**
3887N/A * Returns the platform MXBean proxy for
3887N/A * {@code mxbeanInterface} which is specified to have one single
3887N/A * instance in a Java virtual machine and the proxy will
3887N/A * forward the method calls through the given {@code MBeanServerConnection}.
3887N/A * This method may return {@code null} if the management interface
3887N/A * is not implemented in the Java virtual machine being monitored
3887N/A * (for example, a Java virtual machine with no compilation system
3887N/A * does not implement {@link CompilationMXBean});
3887N/A * otherwise, this method is equivalent to calling:
3887N/A * <pre>
3887N/A * {@link #getPlatformMXBeans(MBeanServerConnection, Class)
3887N/A * getPlatformMXBeans(connection, mxbeanInterface)}.get(0);
3887N/A * </pre>
3887N/A *
3887N/A * @param connection the {@code MBeanServerConnection} to forward to.
3887N/A * @param mxbeanInterface a management interface for a platform
3887N/A * MXBean with one single instance in the Java virtual machine
3887N/A * being monitored, if implemented.
3887N/A *
3887N/A * @return the platform MXBean proxy for
3887N/A * forwarding the method calls of the {@code mxbeanInterface}
3887N/A * through the given {@code MBeanServerConnection},
3887N/A * or {@code null} if not exist.
3887N/A *
3887N/A * @throws IllegalArgumentException if {@code mxbeanInterface}
3887N/A * is not a platform management interface or
3887N/A * not a singleton platform MXBean.
3887N/A * @throws java.io.IOException if a communication problem
3887N/A * occurred when accessing the {@code MBeanServerConnection}.
3887N/A *
3887N/A * @see #newPlatformMXBeanProxy
3887N/A * @since 1.7
3887N/A */
3887N/A public static <T extends PlatformManagedObject>
3887N/A T getPlatformMXBean(MBeanServerConnection connection,
3887N/A Class<T> mxbeanInterface)
3887N/A throws java.io.IOException
3887N/A {
3887N/A PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
3887N/A if (pc == null)
3887N/A throw new IllegalArgumentException(mxbeanInterface.getName() +
3887N/A " is not a platform management interface");
3887N/A if (!pc.isSingleton())
3887N/A throw new IllegalArgumentException(mxbeanInterface.getName() +
3887N/A " can have zero or more than one instances");
3887N/A return pc.getSingletonMXBean(connection, mxbeanInterface);
178N/A }
178N/A
178N/A /**
178N/A * Returns the list of the platform MXBean proxies for
178N/A * forwarding the method calls of the {@code mxbeanInterface}
178N/A * through the given {@code MBeanServerConnection}.
178N/A * The returned list may contain zero, one, or more instances.
178N/A * The number of instances in the returned list is defined
178N/A * in the specification of the given management interface.
3887N/A * The order is undefined and there is no guarantee that
3887N/A * the list returned is in the same order as previous invocations.
178N/A *
178N/A * @param connection the {@code MBeanServerConnection} to forward to.
178N/A * @param mxbeanInterface a management interface for a platform
178N/A * MXBean
178N/A *
178N/A * @return the list of platform MXBean proxies for
178N/A * forwarding the method calls of the {@code mxbeanInterface}
178N/A * through the given {@code MBeanServerConnection}.
178N/A *
178N/A * @throws IllegalArgumentException if {@code mxbeanInterface}
3887N/A * is not a platform management interface.
178N/A *
178N/A * @throws java.io.IOException if a communication problem
178N/A * occurred when accessing the {@code MBeanServerConnection}.
178N/A *
3887N/A * @see #newPlatformMXBeanProxy
178N/A * @since 1.7
178N/A */
178N/A public static <T extends PlatformManagedObject>
3887N/A List<T> getPlatformMXBeans(MBeanServerConnection connection,
3887N/A Class<T> mxbeanInterface)
178N/A throws java.io.IOException
178N/A {
3887N/A PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
3887N/A if (pc == null) {
3887N/A throw new IllegalArgumentException(mxbeanInterface.getName() +
3887N/A " is not a platform management interface");
178N/A }
3887N/A return Collections.unmodifiableList(pc.getMXBeans(connection, mxbeanInterface));
178N/A }
178N/A
178N/A /**
3887N/A * Returns the set of {@code Class} objects, subinterface of
178N/A * {@link PlatformManagedObject}, representing
178N/A * all management interfaces for
178N/A * monitoring and managing the Java platform.
178N/A *
3887N/A * @return the set of {@code Class} objects, subinterface of
178N/A * {@link PlatformManagedObject} representing
178N/A * the management interfaces for
178N/A * monitoring and managing the Java platform.
178N/A *
178N/A * @since 1.7
178N/A */
3887N/A public static Set<Class<? extends PlatformManagedObject>>
3887N/A getPlatformManagementInterfaces()
3887N/A {
3887N/A Set<Class<? extends PlatformManagedObject>> result =
4904N/A new HashSet<>();
178N/A for (PlatformComponent component: PlatformComponent.values()) {
178N/A result.add(component.getMXBeanInterface());
178N/A }
3887N/A return Collections.unmodifiableSet(result);
178N/A }
178N/A
0N/A private static final String NOTIF_EMITTER =
0N/A "javax.management.NotificationEmitter";
178N/A
178N/A /**
178N/A * Registers an MXBean.
178N/A */
178N/A private static void addMXBean(final MBeanServer mbs, final PlatformManagedObject pmo) {
178N/A // Make DynamicMBean out of MXBean by wrapping it with a StandardMBean
178N/A try {
178N/A AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
178N/A public Void run() throws InstanceAlreadyExistsException,
178N/A MBeanRegistrationException,
178N/A NotCompliantMBeanException {
5789N/A final DynamicMBean dmbean;
5789N/A if (pmo instanceof DynamicMBean) {
5789N/A dmbean = DynamicMBean.class.cast(pmo);
5789N/A } else if (pmo instanceof NotificationEmitter) {
5789N/A dmbean = new StandardEmitterMBean(pmo, null, true, (NotificationEmitter) pmo);
5789N/A } else {
5789N/A dmbean = new StandardMBean(pmo, null, true);
5789N/A }
5789N/A
178N/A mbs.registerMBean(dmbean, pmo.getObjectName());
178N/A return null;
178N/A }
178N/A });
178N/A } catch (PrivilegedActionException e) {
178N/A throw new RuntimeException(e.getException());
178N/A }
178N/A }
178N/A
0N/A}