1 /* ManagementFactory.java - Factory for obtaining system beans.
2 Copyright (C) 2006 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.lang.management;
40 /*import gnu.classpath.SystemProperties;
42 import gnu.java.lang.management.ClassLoadingMXBeanImpl;
43 import gnu.java.lang.management.CompilationMXBeanImpl;
44 import gnu.java.lang.management.GarbageCollectorMXBeanImpl;
45 import gnu.java.lang.management.OperatingSystemMXBeanImpl;
46 import gnu.java.lang.management.MemoryMXBeanImpl;
47 import gnu.java.lang.management.MemoryManagerMXBeanImpl;
48 import gnu.java.lang.management.MemoryPoolMXBeanImpl;
49 import gnu.java.lang.management.RuntimeMXBeanImpl;
50 import gnu.java.lang.management.ThreadMXBeanImpl;
52 import java.io.IOException;
54 import java.lang.reflect.InvocationHandler;
55 import java.lang.reflect.Method;
56 import java.lang.reflect.Proxy;
58 import java.util.ArrayList;
59 import java.util.HashMap;
60 import java.util.Iterator;
61 import java.util.List;
64 import java.util.logging.LogManager;
66 import javax.management.Attribute;
67 import javax.management.InstanceAlreadyExistsException;
68 import javax.management.MBeanRegistrationException;
69 import javax.management.MBeanServer;
70 import javax.management.MBeanServerConnection;
71 import javax.management.MBeanServerFactory;
72 import javax.management.MalformedObjectNameException;
73 import javax.management.NotCompliantMBeanException;
74 import javax.management.NotificationEmitter;
75 import javax.management.NotificationFilter;
76 import javax.management.NotificationListener;
77 import javax.management.ObjectName;
79 import javax.management.openmbean.CompositeData;
80 import javax.management.openmbean.TabularData;*/
84 * Provides access to the system's management beans via a series
88 * An instance of a system management bean can be obtained by
89 * using one of the following methods:
92 * <li>Calling the appropriate static method of this factory.
94 * <li>Using the platform {@link javax.management.MBeanServer}
95 * to access the beans locally, or an
96 * {@link javax.management.MBeanServerConnection} for remote
97 * access. The attributes and operations use the limited
98 * range of data types specified below.</li>
100 * <h2>Open Data Types</h2>
102 * The data types used by the management beans are restricted
103 * to <emph>open</emph> data types to aid interoperability. This
104 * allows the beans to be accessed remotely, including from non-Java
105 * clients. Below is a table which lists the types used by the beans
106 * on the left, and the types they are converted to when returned via
107 * a bean server on the right. Type information is provided for each
108 * bean by obtaining its instance of {@link javax.management.MBeanInfo}.
111 * <th><td>Data Type Used</td><td>Data Type Returned</td></th>
113 * <td>Primitive types (<code>int</code>, <code>char</code>, etc.)</td>
116 * <td>Wrapper classes ({@link{java.lang.Integer},
117 * @link{java.lang.Character}, etc.)</td>
120 * <td>An {@link java.lang.Enum}</td>
121 * <td>The <code>name</code> of the enumeration constant</td>
123 * <td>An array of type <code>E</code></td>
124 * <td>An array of the same dimensions with this mapping applied
125 * to <code>E</code>.</td>
127 * <td>A class with `getter' methods and a
128 * <code>from({@link javax.management.openmbean.CompositeData})</code>
130 * <td>The equivalent {@link javax.management.openmbean.CompositeData}
131 * instance, specified by the <code>from</code> method.</td>
133 * <td>A map with keys of type <code>K</code> and values of
134 * type <code>V</code>.</td>
135 * <td>A {@link javax.management.openmbean.TabularData} instance,
136 * with the row type containing two items, <code>"key"</code> and
137 * <code>"value"</code> with the types <code>K</code> and <code>V</code>
138 * respectively (with translation applied).</td>
140 * <td>A list of type <code>E</code>.</td>
141 * <td>An array with this mapping applied to <code>E</code>.</td>
144 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
147 public class ManagementFactory
151 * The object name for the class loading bean.
153 public static final String CLASS_LOADING_MXBEAN_NAME =
154 "java.lang:type=ClassLoading";
157 * The object name for the compilation bean.
159 public static final String COMPILATION_MXBEAN_NAME =
160 "java.lang:type=Compilation";
163 * The domain for the garbage collecting beans.
165 public static final String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
166 "java.lang:type=GarbageCollector";
169 * The domain for the memory manager beans.
171 public static final String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE =
172 "java.lang:type=MemoryManager";
175 * The object name for the memory bean.
177 public static final String MEMORY_MXBEAN_NAME =
178 "java.lang:type=Memory";
181 * The domain for the memory pool beans.
183 public static final String MEMORY_POOL_MXBEAN_DOMAIN_TYPE =
184 "java.lang:type=MemoryPool";
187 * The object name for the operating system bean.
189 public static final String OPERATING_SYSTEM_MXBEAN_NAME =
190 "java.lang:type=OperatingSystem";
193 * The object name for the runtime bean.
195 public static final String RUNTIME_MXBEAN_NAME =
196 "java.lang:type=Runtime";
199 * The object name for the threading bean.
201 public static final String THREAD_MXBEAN_NAME =
202 "java.lang:type=Threading";
205 * The operating system management bean.
207 //private static OperatingSystemMXBean osBean;
210 * The runtime management bean.
212 //private static RuntimeMXBean runtimeBean;
215 * The class loading management bean.
217 //private static ClassLoadingMXBean classLoadingBean;
222 //private static ThreadMXBean threadBean;
227 //private static MemoryMXBean memoryBean;
230 * The compilation bean (may remain null).
232 //private static CompilationMXBean compilationBean;
235 * The platform server.
237 //private static MBeanServer platformServer;
240 * Private constructor to prevent instance creation.
242 private ManagementFactory() {}
245 * Returns the operating system management bean for the
246 * operating system on which the virtual machine is running.
248 * @return an instance of {@link OperatingSystemMXBean} for
249 * the underlying operating system.
251 /*public static OperatingSystemMXBean getOperatingSystemMXBean()
256 osBean = new OperatingSystemMXBeanImpl();
258 catch (NotCompliantMBeanException e)
260 throw new InternalError("The GNU implementation of the " +
261 "operating system bean is not a " +
262 "compliant management bean.");
268 * Returns the runtime management bean for the
269 * running virtual machine.
271 * @return an instance of {@link RuntimeMXBean} for
272 * this virtual machine.
274 /*public static RuntimeMXBean getRuntimeMXBean()
276 if (runtimeBean == null)
279 runtimeBean = new RuntimeMXBeanImpl();
281 catch (NotCompliantMBeanException e)
283 throw new InternalError("The GNU implementation of the " +
284 "runtime bean is not a compliant " +
291 * Returns the class loading management bean for the
292 * running virtual machine.
294 * @return an instance of {@link ClassLoadingMXBean} for
295 * this virtual machine.
297 /*public static ClassLoadingMXBean getClassLoadingMXBean()
299 if (classLoadingBean == null)
302 classLoadingBean = new ClassLoadingMXBeanImpl();
304 catch (NotCompliantMBeanException e)
306 throw new InternalError("The GNU implementation of the " +
307 "class loading bean is not a " +
308 "compliant management bean.");
310 return classLoadingBean;
314 * Returns the thread management bean for the running
317 * @return an instance of {@link ThreadMXBean} for
318 * this virtual machine.
320 /*public static ThreadMXBean getThreadMXBean()
322 if (threadBean == null)
325 threadBean = new ThreadMXBeanImpl();
327 catch (NotCompliantMBeanException e)
329 throw new InternalError("The GNU implementation of the " +
330 "thread bean is not a compliant " +
337 * Returns the memory management bean for the running
340 * @return an instance of {@link MemoryMXBean} for
341 * this virtual machine.
343 /*public static MemoryMXBean getMemoryMXBean()
345 if (memoryBean == null)
348 memoryBean = new MemoryMXBeanImpl();
350 catch (NotCompliantMBeanException e)
352 throw new InternalError("The GNU implementation of the " +
353 "memory bean is not a compliant " +
360 * Returns the compilation bean for the running
361 * virtual machine, if supported. Otherwise,
362 * it returns <code>null</code>.
364 * @return an instance of {@link CompilationMXBean} for
365 * this virtual machine, or <code>null</code>
366 * if the virtual machine doesn't include
367 * a Just-In-Time (JIT) compiler.
369 /*public static CompilationMXBean getCompilationMXBean()
371 if (compilationBean == null &&
372 SystemProperties.getProperty("gnu.java.compiler.name") != null)
375 compilationBean = new CompilationMXBeanImpl();
377 catch (NotCompliantMBeanException e)
379 throw new InternalError("The GNU implementation of the " +
380 "compilation bean is not a compliant " +
383 return compilationBean;
387 * Returns the memory pool beans for the running
388 * virtual machine. These may change during the course
391 * @return a list of memory pool beans, one for each pool.
393 /*public static List<MemoryPoolMXBean> getMemoryPoolMXBeans()
395 List<MemoryPoolMXBean> poolBeans =
396 new ArrayList<MemoryPoolMXBean>();
397 String[] names = VMManagementFactory.getMemoryPoolNames();
398 for (int a = 0; a < names.length; ++a)
401 poolBeans.add(new MemoryPoolMXBeanImpl(names[a]));
403 catch (NotCompliantMBeanException e)
405 throw new InternalError("The GNU implementation of the " +
406 "memory pool bean, " + a + ", is " +
407 "not a compliant management bean.");
413 * Returns the memory manager beans for the running
414 * virtual machine. These may change during the course
417 * @return a list of memory manager beans, one for each manager.
419 /*public static List<MemoryManagerMXBean> getMemoryManagerMXBeans()
421 List<MemoryManagerMXBean> managerBeans =
422 new ArrayList<MemoryManagerMXBean>();
423 String[] names = VMManagementFactory.getMemoryManagerNames();
424 for (int a = 0; a < names.length; ++a)
427 managerBeans.add(new MemoryManagerMXBeanImpl(names[a]));
429 catch (NotCompliantMBeanException e)
431 throw new InternalError("The GNU implementation of the " +
432 "memory manager bean, " + a + ", is " +
433 "not a compliant management bean.");
435 managerBeans.addAll(getGarbageCollectorMXBeans());
440 * Returns the garbage collector beans for the running
441 * virtual machine. These may change during the course
444 * @return a list of garbage collector beans, one for each pool.
446 public static List/*<GarbageCollectorMXBean>*/ getGarbageCollectorMXBeans()
448 List/*<GarbageCollectorMXBean>*/ gcBeans =
449 new ArrayList/*<GarbageCollectorMXBean>*/();
450 /*String[] names = VMManagementFactory.getGarbageCollectorNames();
451 for (int a = 0; a < names.length; ++a)
454 gcBeans.add(new GarbageCollectorMXBeanImpl(names[a]));
456 catch (NotCompliantMBeanException e)
458 throw new InternalError("The GNU implementation of the " +
459 "garbage collector bean, " + a +
460 ", is not a compliant management " +
468 * Returns the platform {@link javax.management.MBeanServer}. On the
469 * first call to this method, a server instance is retrieved from
470 * the {@link javax.management.MBeanServerFactory} and each of the
471 * beans are registered with it. Subsequent calls return the existing
472 * instance. If the property <code>javax.management.builder.initial</code>
473 * is set, its value will be used as the name of the class which is used
474 * to provide the server instance.
477 * It is recommended that the platform server is used for other beans as
478 * well, in order to simplify their discovery and publication. Name conflicts
482 * @return the platform {@link javax.management.MBeanServer}
483 * @throws SecurityException if a security manager exists and the
484 * caller's permissions don't imply {@link
485 * MBeanServerPermission(String)}("createMBeanServer")
486 * @see javax.management.MBeanServerFactory
487 * @see javax.management.MBeanServerFactory#createMBeanServer()
489 /*public static MBeanServer getPlatformMBeanServer()
491 if (platformServer == null)
493 platformServer = MBeanServerFactory.createMBeanServer();
496 platformServer.registerMBean(getOperatingSystemMXBean(),
497 new ObjectName(OPERATING_SYSTEM_MXBEAN_NAME));
498 platformServer.registerMBean(getRuntimeMXBean(),
499 new ObjectName(RUNTIME_MXBEAN_NAME));
500 platformServer.registerMBean(getClassLoadingMXBean(),
501 new ObjectName(CLASS_LOADING_MXBEAN_NAME));
502 platformServer.registerMBean(getThreadMXBean(),
503 new ObjectName(THREAD_MXBEAN_NAME));
504 platformServer.registerMBean(getMemoryMXBean(),
505 new ObjectName(MEMORY_MXBEAN_NAME));
506 CompilationMXBean compBean = getCompilationMXBean();
507 if (compBean != null)
508 platformServer.registerMBean(compBean,
509 new ObjectName(COMPILATION_MXBEAN_NAME));
510 Iterator beans = getMemoryPoolMXBeans().iterator();
511 while (beans.hasNext())
513 MemoryPoolMXBean bean = (MemoryPoolMXBean) beans.next();
514 platformServer.registerMBean(bean,
515 new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE +
519 beans = getMemoryManagerMXBeans().iterator();
520 while (beans.hasNext())
522 MemoryManagerMXBean bean = (MemoryManagerMXBean) beans.next();
523 platformServer.registerMBean(bean,
524 new ObjectName(MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE +
528 beans = getGarbageCollectorMXBeans().iterator();
529 while (beans.hasNext())
531 GarbageCollectorMXBean bean = (GarbageCollectorMXBean) beans.next();
532 platformServer.registerMBean(bean,
533 new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
537 platformServer.registerMBean(LogManager.getLoggingMXBean(),
538 new ObjectName(LogManager.LOGGING_MXBEAN_NAME));
540 catch (InstanceAlreadyExistsException e)
543 (new InternalError("One of the management beans is " +
544 "already registered.").initCause(e));
546 catch (MBeanRegistrationException e)
549 (new InternalError("One of the management beans' preRegister " +
550 "methods threw an exception.").initCause(e));
552 catch (NotCompliantMBeanException e)
555 (new InternalError("One of the management beans is " +
556 "not compliant.").initCause(e));
558 catch (MalformedObjectNameException e)
561 (new InternalError("The object name of a management bean is " +
562 "not compliant.").initCause(e));
565 return platformServer;
570 * Returns a proxy for the specified platform bean. A proxy object is created
571 * using <code>Proxy.newProxyInstance(mxbeanInterface.getClassLoader(),
572 * new Class[] { mxbeanInterface }, handler)</code>. The
573 * {@link javax.management.NotificationEmitter} class is also added to the
574 * array if the bean provides notifications. <code>handler</code> refers
575 * to the invocation handler which forwards calls to the connection, and
576 * also provides translation between the Java data types used in the
577 * bean interfaces and the open data types, as specified in the description
578 * of this class. It is this translation that makes the
579 * usual {@link javax.management.MBeanServerInvocationHandler} inappropriate
580 * for providing such a proxy.
583 * <strong>Note</strong>: use of the proxy may result in
584 * {@link java.io.IOException}s from the underlying {@link MBeanServerConnection}
585 * and a {@link java.io.InvalidObjectException} if enum constants
586 * used on the client and the server don't match.
589 * @param connection the server connection to use to access the bean.
590 * @param mxbeanName the {@link javax.management.ObjectName} of the
591 * bean to provide a proxy for.
592 * @param mxbeanInterface the interface for the bean being proxied.
593 * @return a proxy for the specified bean.
594 * @throws IllegalArgumentException if <code>mxbeanName</code> is not a valid
595 * {@link javax.management.ObjectName},
596 * the interface and name do not match the
597 * same bean, the name does not refer to a
598 * platform bean or the bean is not registered
599 * with the server accessed by <code>connection</code>.
600 * @throws IOException if the connection throws one.
602 /*public static <T> T newPlatformMXBeanProxy(MBeanServerConnection connection,
604 Class<T> mxbeanInterface)
607 if (!(mxbeanName.equals(CLASS_LOADING_MXBEAN_NAME) ||
608 mxbeanName.equals(COMPILATION_MXBEAN_NAME) ||
609 mxbeanName.startsWith(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE) ||
610 mxbeanName.startsWith(MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE) ||
611 mxbeanName.equals(MEMORY_MXBEAN_NAME) ||
612 mxbeanName.startsWith(MEMORY_POOL_MXBEAN_DOMAIN_TYPE) ||
613 mxbeanName.equals(OPERATING_SYSTEM_MXBEAN_NAME) ||
614 mxbeanName.equals(RUNTIME_MXBEAN_NAME) ||
615 mxbeanName.equals(THREAD_MXBEAN_NAME)))
617 throw new IllegalArgumentException("The named bean, " + mxbeanName +
618 ", is not a platform name.");
620 if ((mxbeanName.equals(CLASS_LOADING_MXBEAN_NAME) &&
621 mxbeanInterface != ClassLoadingMXBean.class) ||
622 (mxbeanName.equals(COMPILATION_MXBEAN_NAME) &&
623 mxbeanInterface != CompilationMXBean.class) ||
624 (mxbeanName.startsWith(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE) &&
625 mxbeanInterface != GarbageCollectorMXBean.class) ||
626 (mxbeanName.startsWith(MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE) &&
627 mxbeanInterface != MemoryManagerMXBean.class) ||
628 (mxbeanName.equals(MEMORY_MXBEAN_NAME) &&
629 mxbeanInterface != MemoryMXBean.class) ||
630 (mxbeanName.startsWith(MEMORY_POOL_MXBEAN_DOMAIN_TYPE) &&
631 mxbeanInterface != MemoryPoolMXBean.class) ||
632 (mxbeanName.equals(OPERATING_SYSTEM_MXBEAN_NAME) &&
633 mxbeanInterface != OperatingSystemMXBean.class) ||
634 (mxbeanName.equals(RUNTIME_MXBEAN_NAME) &&
635 mxbeanInterface != RuntimeMXBean.class) ||
636 (mxbeanName.equals(THREAD_MXBEAN_NAME) &&
637 mxbeanInterface != ThreadMXBean.class))
638 throw new IllegalArgumentException("The interface, " + mxbeanInterface +
639 ", does not match the bean, " + mxbeanName);
643 bean = new ObjectName(mxbeanName);
645 catch (MalformedObjectNameException e)
647 throw new IllegalArgumentException("The named bean is invalid.");
649 if (!(connection.isRegistered(bean)))
650 throw new IllegalArgumentException("The bean is not registered on this connection.");
652 if (mxbeanName.equals(MEMORY_MXBEAN_NAME))
653 interfaces = new Class[] { mxbeanInterface, NotificationEmitter.class };
655 interfaces = new Class[] { mxbeanInterface };
656 return (T) Proxy.newProxyInstance(mxbeanInterface.getClassLoader(),
658 new ManagementInvocationHandler(connection, bean));
662 * This invocation handler provides method calls for a platform bean
663 * by forwarding them to a {@link MBeanServerConnection}. Translation from
664 * Java data types to open data types is performed as specified above.
666 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
669 /*private static class ManagementInvocationHandler
670 implements InvocationHandler
674 * The encapsulated connection.
676 /*private MBeanServerConnection conn;
679 * The bean being proxied.
681 /*private ObjectName bean;
684 * Constructs a new {@link InvocationHandler} which proxies
685 * for the specified bean using the supplied connection.
687 * @param conn the connection on which to forward method calls.
688 * @param bean the bean to proxy.
690 /*public ManagementInvocationHandler(MBeanServerConnection conn,
699 * Called by the proxy class whenever a method is called. The method
700 * is emulated by retrieving an attribute from, setting an attribute on
701 * or invoking a method on the server connection as required. Translation
702 * between the Java data types supplied as arguments to the open types used
703 * by the bean is provided, as well as translation of the return value back
704 * in to the appropriate Java type.
706 * @param proxy the proxy on which the method was called.
707 * @param method the method which was called.
708 * @param args the arguments supplied to the method.
709 * @return the return value from the method.
710 * @throws Throwable if an exception is thrown in performing the
713 /*public Object invoke(Object proxy, Method method, Object[] args)
716 String name = method.getName();
717 if (name.equals("toString"))
718 return "Proxy for " + bean + " using " + conn;
719 if (name.equals("addNotificationListener"))
721 conn.addNotificationListener(bean,
722 (NotificationListener) args[0],
723 (NotificationFilter) args[1],
727 if (name.equals("getNotificationInfo"))
728 return conn.getMBeanInfo(bean).getNotifications();
729 if (name.equals("removeNotificationListener"))
731 if (args.length == 1)
732 conn.removeNotificationListener(bean,
733 (NotificationListener)
736 conn.removeNotificationListener(bean,
737 (NotificationListener)
743 String attrib = null;
744 if (name.startsWith("get"))
745 attrib = name.substring(3);
746 else if (name.startsWith("is"))
747 attrib = name.substring(2);
749 return translate(conn.getAttribute(bean, attrib), method);
750 else if (name.startsWith("set"))
752 conn.setAttribute(bean, new Attribute(name.substring(3),
757 return translate(conn.invoke(bean, name, args, null), method);
761 * Translates the returned open data type to the value
762 * required by the interface.
764 * @param otype the open type returned by the method call.
765 * @param method the method that was called.
766 * @return the equivalent return type required by the interface.
767 * @throws Throwable if an exception is thrown in performing the
770 /*private final Object translate(Object otype, Method method)
773 Class<?> returnType = method.getReturnType();
774 if (returnType.isEnum())
776 String ename = (String) otype;
777 Enum[] constants = (Enum[]) returnType.getEnumConstants();
778 for (Enum c : constants)
779 if (c.name().equals(ename))
782 if (List.class.isAssignableFrom(returnType))
784 Object[] elems = (Object[]) otype;
785 List l = new ArrayList(elems.length);
786 for (Object elem : elems)
790 if (Map.class.isAssignableFrom(returnType))
792 TabularData data = (TabularData) otype;
793 Map m = new HashMap(data.size());
794 for (Object val : data.values())
796 CompositeData vals = (CompositeData) val;
797 m.put(vals.get("key"), vals.get("value"));
803 Method m = returnType.getMethod("from",
805 { CompositeData.class });
806 return m.invoke(null, (CompositeData) otype);
808 catch (NoSuchMethodException e)
810 /* Ignored; we expect this if this
811 isn't a from(CompositeData) class */