src/share/classes/java/lang/management/ManagementFactory.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.management;
  27 import javax.management.DynamicMBean;
  28 import javax.management.MBeanServer;
  29 import javax.management.MBeanServerConnection;
  30 import javax.management.MBeanServerFactory;
  31 import javax.management.MBeanServerPermission;

  32 import javax.management.NotificationEmitter;
  33 import javax.management.ObjectInstance;
  34 import javax.management.ObjectName;
  35 import javax.management.InstanceAlreadyExistsException;
  36 import javax.management.InstanceNotFoundException;
  37 import javax.management.MalformedObjectNameException;
  38 import javax.management.MBeanRegistrationException;
  39 import javax.management.NotCompliantMBeanException;
  40 import javax.management.StandardEmitterMBean;
  41 import javax.management.StandardMBean;
  42 import java.util.Collections;
  43 import java.util.List;
  44 import java.util.Set;

  45 import java.util.HashSet;
  46 import java.security.AccessController;
  47 import java.security.Permission;
  48 import java.security.PrivilegedAction;
  49 import java.security.PrivilegedActionException;
  50 import java.security.PrivilegedExceptionAction;
  51 import javax.management.JMX;
  52 import sun.management.ManagementFactoryHelper;
  53 
  54 /**
  55  * The {@code ManagementFactory} class is a factory class for getting
  56  * managed beans for the Java platform.
  57  * This class consists of static methods each of which returns
  58  * one or more <i>platform MXBeans</i> representing
  59  * the management interface of a component of the Java virtual
  60  * machine.
  61  * <p>
  62  * <h4><a name="MXBean">Platform MXBeans</a></h4>
  63  * <p>
  64  * A platform MXBean is a <i>managed bean</i> that


 465         if (platformMBeanServer == null) {
 466             platformMBeanServer = MBeanServerFactory.createMBeanServer();
 467             for (PlatformComponent pc : PlatformComponent.values()) {
 468                 List<? extends PlatformManagedObject> list =
 469                     pc.getMXBeans(pc.getMXBeanInterface());
 470                 for (PlatformManagedObject o : list) {
 471                     // Each PlatformComponent represents one management
 472                     // interface. Some MXBean may extend another one.
 473                     // The MXBean instances for one platform component
 474                     // (returned by pc.getMXBeans()) might be also
 475                     // the MXBean instances for another platform component.
 476                     // e.g. com.sun.management.GarbageCollectorMXBean
 477                     //
 478                     // So need to check if an MXBean instance is registered
 479                     // before registering into the platform MBeanServer
 480                     if (!platformMBeanServer.isRegistered(o.getObjectName())) {
 481                         addMXBean(platformMBeanServer, o);
 482                     }
 483                 }
 484             }




 485         }

 486         return platformMBeanServer;
 487     }
 488 
 489     /**
 490      * Returns a proxy for a platform MXBean interface of a
 491      * given <a href="#MXBeanNames">MXBean name</a>
 492      * that forwards its method calls through the given
 493      * <tt>MBeanServerConnection</tt>.
 494      *
 495      * <p>This method is equivalent to:
 496      * <blockquote>
 497      * {@link java.lang.reflect.Proxy#newProxyInstance
 498      *        Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),
 499      *        new Class[] { mxbeanInterface }, handler)</tt>
 500      * </blockquote>
 501      *
 502      * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler
 503      * InvocationHandler} to which method invocations to the MXBean interface
 504      * are dispatched. This <tt>handler</tt> converts an input parameter
 505      * from an MXBean data type to its mapped open type before forwarding


 808                                          MBeanRegistrationException,
 809                                          NotCompliantMBeanException {
 810                     final DynamicMBean dmbean;
 811                     if (pmo instanceof DynamicMBean) {
 812                         dmbean = DynamicMBean.class.cast(pmo);
 813                     } else if (pmo instanceof NotificationEmitter) {
 814                         dmbean = new StandardEmitterMBean(pmo, null, true, (NotificationEmitter) pmo);
 815                     } else {
 816                         dmbean = new StandardMBean(pmo, null, true);
 817                     }
 818 
 819                     mbs.registerMBean(dmbean, pmo.getObjectName());
 820                     return null;
 821                 }
 822             });
 823         } catch (PrivilegedActionException e) {
 824             throw new RuntimeException(e.getException());
 825         }
 826     }
 827 




















 828 }
   1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.management;
  27 import javax.management.DynamicMBean;
  28 import javax.management.MBeanServer;
  29 import javax.management.MBeanServerConnection;
  30 import javax.management.MBeanServerFactory;
  31 import javax.management.MBeanServerPermission;
  32 import javax.management.MXBean;
  33 import javax.management.NotificationEmitter;
  34 import javax.management.ObjectInstance;
  35 import javax.management.ObjectName;
  36 import javax.management.InstanceAlreadyExistsException;
  37 import javax.management.InstanceNotFoundException;
  38 import javax.management.MalformedObjectNameException;
  39 import javax.management.MBeanRegistrationException;
  40 import javax.management.NotCompliantMBeanException;
  41 import javax.management.StandardEmitterMBean;
  42 import javax.management.StandardMBean;
  43 import java.util.Collections;
  44 import java.util.List;
  45 import java.util.Set;
  46 import java.util.HashMap;
  47 import java.util.HashSet;
  48 import java.security.AccessController;
  49 import java.security.Permission;
  50 import java.security.PrivilegedAction;
  51 import java.security.PrivilegedActionException;
  52 import java.security.PrivilegedExceptionAction;
  53 import javax.management.JMX;
  54 import sun.management.ManagementFactoryHelper;
  55 
  56 /**
  57  * The {@code ManagementFactory} class is a factory class for getting
  58  * managed beans for the Java platform.
  59  * This class consists of static methods each of which returns
  60  * one or more <i>platform MXBeans</i> representing
  61  * the management interface of a component of the Java virtual
  62  * machine.
  63  * <p>
  64  * <h4><a name="MXBean">Platform MXBeans</a></h4>
  65  * <p>
  66  * A platform MXBean is a <i>managed bean</i> that


 467         if (platformMBeanServer == null) {
 468             platformMBeanServer = MBeanServerFactory.createMBeanServer();
 469             for (PlatformComponent pc : PlatformComponent.values()) {
 470                 List<? extends PlatformManagedObject> list =
 471                     pc.getMXBeans(pc.getMXBeanInterface());
 472                 for (PlatformManagedObject o : list) {
 473                     // Each PlatformComponent represents one management
 474                     // interface. Some MXBean may extend another one.
 475                     // The MXBean instances for one platform component
 476                     // (returned by pc.getMXBeans()) might be also
 477                     // the MXBean instances for another platform component.
 478                     // e.g. com.sun.management.GarbageCollectorMXBean
 479                     //
 480                     // So need to check if an MXBean instance is registered
 481                     // before registering into the platform MBeanServer
 482                     if (!platformMBeanServer.isRegistered(o.getObjectName())) {
 483                         addMXBean(platformMBeanServer, o);
 484                     }
 485                 }
 486             }
 487             HashMap<ObjectName, DynamicMBean> dynmbeans =
 488                     ManagementFactoryHelper.getPlatformDynamicMBeans();
 489             for (ObjectName on : dynmbeans.keySet()) {
 490                 addDynamicMBean(platformMBeanServer, dynmbeans.get(on), on);
 491             }
 492         }
 493         return platformMBeanServer;
 494     }
 495 
 496     /**
 497      * Returns a proxy for a platform MXBean interface of a
 498      * given <a href="#MXBeanNames">MXBean name</a>
 499      * that forwards its method calls through the given
 500      * <tt>MBeanServerConnection</tt>.
 501      *
 502      * <p>This method is equivalent to:
 503      * <blockquote>
 504      * {@link java.lang.reflect.Proxy#newProxyInstance
 505      *        Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),
 506      *        new Class[] { mxbeanInterface }, handler)</tt>
 507      * </blockquote>
 508      *
 509      * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler
 510      * InvocationHandler} to which method invocations to the MXBean interface
 511      * are dispatched. This <tt>handler</tt> converts an input parameter
 512      * from an MXBean data type to its mapped open type before forwarding


 815                                          MBeanRegistrationException,
 816                                          NotCompliantMBeanException {
 817                     final DynamicMBean dmbean;
 818                     if (pmo instanceof DynamicMBean) {
 819                         dmbean = DynamicMBean.class.cast(pmo);
 820                     } else if (pmo instanceof NotificationEmitter) {
 821                         dmbean = new StandardEmitterMBean(pmo, null, true, (NotificationEmitter) pmo);
 822                     } else {
 823                         dmbean = new StandardMBean(pmo, null, true);
 824                     }
 825 
 826                     mbs.registerMBean(dmbean, pmo.getObjectName());
 827                     return null;
 828                 }
 829             });
 830         } catch (PrivilegedActionException e) {
 831             throw new RuntimeException(e.getException());
 832         }
 833     }
 834 
 835     /**
 836      * Registers a DynamicMBean.
 837      */
 838     private static void addDynamicMBean(final MBeanServer mbs,
 839                                         final DynamicMBean dmbean,
 840                                         final ObjectName on) {
 841         try {
 842             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
 843                 @Override
 844                 public Void run() throws InstanceAlreadyExistsException,
 845                                          MBeanRegistrationException,
 846                                          NotCompliantMBeanException {
 847                     mbs.registerMBean(dmbean, on);
 848                     return null;
 849                 }
 850             });
 851         } catch (PrivilegedActionException e) {
 852             throw new RuntimeException(e.getException());
 853         }
 854     }
 855 }