/* * Copyright 2003-2006 Sun Microsystems, Inc. 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. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ package java.lang.management; import javax.management.MBeanServer; import javax.management.MBeanServerConnection; import javax.management.MBeanServerPermission; import javax.management.ObjectName; import javax.management.InstanceNotFoundException; import javax.management.MalformedObjectNameException; import java.util.List; import java.security.AccessController; import java.security.Permission; import java.security.PrivilegedAction; import javax.management.JMX; /** * The 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 platform MXBean(s) representing * the management interface of a component of the Java virtual * machine. * *
* An application can access a platform MXBean in the following ways: *
* The platform MXBean interfaces use only the following data types: *
* When an attribute or operation of a platform MXBean * is accessed via an MBeanServer, the data types are mapped * as follows: *
* For example, the {@link MemoryMXBean} * interface has the following getter and setter methods: * *
* * These attributes in the MBeanInfo * of the MemoryMXBean have the following names and types: * ** public MemoryUsage getHeapMemoryUsage(); * public boolean isVerbose(); * public void setVerbose(boolean value); *
** **
** *Attribute Name *Type ** *HeapMemoryUsage *{@link MemoryUsage#from * CompositeData representing MemoryUsage} ** *Verbose *boolean *
** **
** *Management Interface *ObjectName ** *{@link ClassLoadingMXBean} *{@link #CLASS_LOADING_MXBEAN_NAME * java.lang:type=ClassLoading} ** *{@link MemoryMXBean} *{@link #MEMORY_MXBEAN_NAME * java.lang:type=Memory} ** *{@link ThreadMXBean} *{@link #THREAD_MXBEAN_NAME * java.lang:type=Threading} ** *{@link RuntimeMXBean} *{@link #RUNTIME_MXBEAN_NAME * java.lang:type=Runtime} ** *{@link OperatingSystemMXBean} *{@link #OPERATING_SYSTEM_MXBEAN_NAME * java.lang:type=OperatingSystem} *
* A Java virtual machine has zero or a single instance of * the following management interfaces. * *
** **
** *Management Interface *ObjectName ** *{@link CompilationMXBean} *{@link #COMPILATION_MXBEAN_NAME * java.lang:type=Compilation} *
* A Java virtual machine may have one or more instances of the following * management interfaces. *
** * @see * JMX Specification. * @see * Ways to Access Management Metrics * @see java.util.logging.LoggingMXBean * @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 * ObjectName for the {@link ClassLoadingMXBean}. */ public final static String CLASS_LOADING_MXBEAN_NAME = "java.lang:type=ClassLoading"; /** * String representation of the * ObjectName for the {@link CompilationMXBean}. */ public final static String COMPILATION_MXBEAN_NAME = "java.lang:type=Compilation"; /** * String representation of the * ObjectName for the {@link MemoryMXBean}. */ public final static String MEMORY_MXBEAN_NAME = "java.lang:type=Memory"; /** * String representation of the * ObjectName for the {@link OperatingSystemMXBean}. */ public final static String OPERATING_SYSTEM_MXBEAN_NAME = "java.lang:type=OperatingSystem"; /** * String representation of the * ObjectName for the {@link RuntimeMXBean}. */ public final static String RUNTIME_MXBEAN_NAME = "java.lang:type=Runtime"; /** * String representation of the * ObjectName 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 ObjectName for a {@link GarbageCollectorMXBean}. * The unique ObjectName for a GarbageCollectorMXBean * can be formed by appending this string with * ",name=collector's name". */ public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE = "java.lang:type=GarbageCollector"; /** * The domain name and the type key property in * the ObjectName for a {@link MemoryManagerMXBean}. * The unique ObjectName for a MemoryManagerMXBean * can be formed by appending this string with * ",name=manager's name". */ public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE= "java.lang:type=MemoryManager"; /** * The domain name and the type key property in * the ObjectName for a {@link MemoryPoolMXBean}. * The unique ObjectName for a MemoryPoolMXBean * can be formed by appending this string with * ,name=pool's name. */ 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 sun.management.ManagementFactory.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 sun.management.ManagementFactory.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 sun.management.ManagementFactory.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 sun.management.ManagementFactory.getRuntimeMXBean(); } /** * Returns the managed bean for the compilation system of * the Java virtual machine. This method returns null * if the Java virtual machine has no compilation system. * * @return a {@link CompilationMXBean} object for the Java virtual * machine or null if the Java virtual machine has * no compilation system. */ public static CompilationMXBean getCompilationMXBean() { return sun.management.ManagementFactory.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 sun.management.ManagementFactory.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 MemoryPoolMXBean objects. * */ public static List*
** *Management Interface *ObjectName ** *{@link GarbageCollectorMXBean} *{@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE * java.lang:type=GarbageCollector},name=collector's name ** *{@link MemoryManagerMXBean} *{@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE * java.lang:type=MemoryManager},name=manager's name ** *{@link MemoryPoolMXBean} *{@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE * java.lang:type=MemoryPool},name=pool's name *
* 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 * MBeanServer. *
* If the system property javax.management.builder.initial * is set, the platform MBeanServer creation will be done * by the specified {@link javax.management.MBeanServerBuilder}. *
* 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 * MBeanServer and hence allow for easier network publishing * and discovery. * Name conflicts with the platform MXBeans should be avoided. * * @return the platform MBeanServer; the platform * MXBeans are registered into the platform 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 = sun.management.ManagementFactory.createPlatformMBeanServer(); } return platformMBeanServer; } /** * Returns a proxy for a platform MXBean interface of a * given MXBean name * that forwards its method calls through the given * MBeanServerConnection. * *
This method is equivalent to: *
* {@link java.lang.reflect.Proxy#newProxyInstance * Proxy.newProxyInstance}(mxbeanInterface.getClassLoader(), * new Class[] { mxbeanInterface }, handler) ** * where handler is an {@link java.lang.reflect.InvocationHandler * InvocationHandler} to which method invocations to the MXBean interface * are dispatched. This handler converts an input parameter * from an MXBean data type to its mapped open type before forwarding * to the MBeanServer and converts a return value from * an MXBean method call through the MBeanServer * from an open type to the corresponding return type declared in * the MXBean interface. * *
* If the MXBean is a notification emitter (i.e., * it implements * {@link javax.management.NotificationEmitter NotificationEmitter}), * both the mxbeanInterface and NotificationEmitter * will be implemented by this proxy. * *
* Notes: *