< prev index next >

jdk/src/java.management/share/classes/sun/management/ManagementFactoryHelper.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.management;
  27 
  28 import java.lang.management.*;
  29 import javax.management.InstanceAlreadyExistsException;
  30 import javax.management.InstanceNotFoundException;
  31 import javax.management.MBeanServer;
  32 import javax.management.MBeanRegistrationException;
  33 import javax.management.NotCompliantMBeanException;
  34 import javax.management.ObjectName;
  35 import javax.management.RuntimeOperationsException;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedActionException;
  38 import java.security.PrivilegedExceptionAction;
  39 
  40 import jdk.internal.misc.JavaNioAccess;
  41 import jdk.internal.misc.SharedSecrets;
  42 import sun.util.logging.LoggingSupport;
  43 import java.util.ArrayList;
  44 import java.util.List;
  45 




  46 /**
  47  * ManagementFactoryHelper provides static factory methods to create
  48  * instances of the management interface.
  49  */
  50 public class ManagementFactoryHelper {
  51     static {
  52         // make sure that the management lib is loaded within
  53         // java.lang.management.ManagementFactory
  54         sun.misc.Unsafe.getUnsafe().ensureClassInitialized(ManagementFactory.class);
  55     }
  56 
  57     private static final VMManagement jvm = new VMManagementImpl();
  58 
  59     private ManagementFactoryHelper() {};
  60 
  61     public static VMManagement getVMManagement() {
  62         return jvm;
  63     }
  64 
  65     private static ClassLoadingImpl    classMBean = null;


 124         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 125         List<MemoryManagerMXBean> result = new ArrayList<>(mgrs.length);
 126         for (MemoryManagerMXBean m : mgrs) {
 127             result.add(m);
 128         }
 129         return result;
 130     }
 131 
 132      public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
 133         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 134         List<GarbageCollectorMXBean> result = new ArrayList<>(mgrs.length);
 135         for (MemoryManagerMXBean m : mgrs) {
 136             if (GarbageCollectorMXBean.class.isInstance(m)) {
 137                  result.add(GarbageCollectorMXBean.class.cast(m));
 138             }
 139         }
 140         return result;
 141     }
 142 
 143     public static PlatformLoggingMXBean getPlatformLoggingMXBean() {
 144         if (LoggingSupport.isAvailable()) {
 145             return PlatformLoggingImpl.instance;
 146         } else {
 147             return null;
 148         }
 149     }
 150 




 151     /**
 152      * The logging MXBean object is an instance of
 153      * PlatformLoggingMXBean and java.util.logging.LoggingMXBean
 154      * but it can't directly implement two MXBean interfaces
 155      * as a compliant MXBean implements exactly one MXBean interface,
 156      * or if it implements one interface that is a subinterface of
 157      * all the others; otherwise, it is a non-compliant MXBean
 158      * and MBeanServer will throw NotCompliantMBeanException.
 159      * See the Definition of an MXBean section in javax.management.MXBean spec.
 160      *
 161      * To create a compliant logging MXBean, define a LoggingMXBean interface
 162      * that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean
 163     */
 164     public interface LoggingMXBean
 165         extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean {
 166     }
 167 


































 168     static class PlatformLoggingImpl implements LoggingMXBean
 169     {


 170         final static PlatformLoggingMXBean instance = new PlatformLoggingImpl();
 171         final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging";
 172 
 173         private volatile ObjectName objname;  // created lazily
 174         @Override
 175         public ObjectName getObjectName() {
 176             ObjectName result = objname;
 177             if (result == null) {
 178                 synchronized (this) {
 179                     result = objname;
 180                     if (result == null) {
 181                         result = Util.newObjectName(LOGGING_MXBEAN_NAME);
 182                         objname = result;
 183                     }
 184                 }
 185             }
 186             return result;
 187         }
 188 
 189         @Override
 190         public java.util.List<String> getLoggerNames() {
 191             return LoggingSupport.getLoggerNames();
 192         }
 193 
 194         @Override
 195         public String getLoggerLevel(String loggerName) {
 196             return LoggingSupport.getLoggerLevel(loggerName);
 197         }
 198 
 199         @Override
 200         public void setLoggerLevel(String loggerName, String levelName) {
 201             LoggingSupport.setLoggerLevel(loggerName, levelName);
 202         }
 203 
 204         @Override
 205         public String getParentLoggerName(String loggerName) {
 206             return LoggingSupport.getParentLoggerName(loggerName);
 207         }
 208     }
 209 
 210     private static List<BufferPoolMXBean> bufferPools = null;
 211     public static synchronized List<BufferPoolMXBean> getBufferPoolMXBeans() {
 212         if (bufferPools == null) {
 213             bufferPools = new ArrayList<>(2);
 214             bufferPools.add(createBufferPoolMXBean(SharedSecrets.getJavaNioAccess()
 215                 .getDirectBufferPool()));
 216             bufferPools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl
 217                 .getMappedBufferPool()));
 218         }
 219         return bufferPools;
 220     }
 221 
 222     private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";
 223 
 224     /**
 225      * Creates management interface for the given buffer pool.
 226      */




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.management;
  27 
  28 import java.lang.management.*;
  29 import javax.management.InstanceAlreadyExistsException;
  30 import javax.management.InstanceNotFoundException;
  31 import javax.management.MBeanServer;
  32 import javax.management.MBeanRegistrationException;
  33 import javax.management.NotCompliantMBeanException;
  34 import javax.management.ObjectName;
  35 import javax.management.RuntimeOperationsException;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedActionException;
  38 import java.security.PrivilegedExceptionAction;
  39 
  40 import jdk.internal.misc.JavaNioAccess;
  41 import jdk.internal.misc.SharedSecrets;
  42 
  43 import java.util.ArrayList;
  44 import java.util.List;
  45 
  46 import java.lang.reflect.Constructor;
  47 import java.lang.reflect.InvocationTargetException;
  48 import java.security.PrivilegedAction;
  49 
  50 /**
  51  * ManagementFactoryHelper provides static factory methods to create
  52  * instances of the management interface.
  53  */
  54 public class ManagementFactoryHelper {
  55     static {
  56         // make sure that the management lib is loaded within
  57         // java.lang.management.ManagementFactory
  58         sun.misc.Unsafe.getUnsafe().ensureClassInitialized(ManagementFactory.class);
  59     }
  60 
  61     private static final VMManagement jvm = new VMManagementImpl();
  62 
  63     private ManagementFactoryHelper() {};
  64 
  65     public static VMManagement getVMManagement() {
  66         return jvm;
  67     }
  68 
  69     private static ClassLoadingImpl    classMBean = null;


 128         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 129         List<MemoryManagerMXBean> result = new ArrayList<>(mgrs.length);
 130         for (MemoryManagerMXBean m : mgrs) {
 131             result.add(m);
 132         }
 133         return result;
 134     }
 135 
 136      public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
 137         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 138         List<GarbageCollectorMXBean> result = new ArrayList<>(mgrs.length);
 139         for (MemoryManagerMXBean m : mgrs) {
 140             if (GarbageCollectorMXBean.class.isInstance(m)) {
 141                  result.add(GarbageCollectorMXBean.class.cast(m));
 142             }
 143         }
 144         return result;
 145     }
 146 
 147     public static PlatformLoggingMXBean getPlatformLoggingMXBean() {
 148         if (LoggingMXBeanSupport.isAvailable()) {
 149             return PlatformLoggingImpl.instance;
 150         } else {
 151             return null;
 152         }
 153     }
 154 
 155     public static boolean isPlatformLoggingMXBeanAvailable() {
 156         return LoggingMXBeanSupport.isAvailable();
 157     }
 158 
 159     /**
 160      * The logging MXBean object is an instance of
 161      * PlatformLoggingMXBean and java.util.logging.LoggingMXBean
 162      * but it can't directly implement two MXBean interfaces
 163      * as a compliant MXBean implements exactly one MXBean interface,
 164      * or if it implements one interface that is a subinterface of
 165      * all the others; otherwise, it is a non-compliant MXBean
 166      * and MBeanServer will throw NotCompliantMBeanException.
 167      * See the Definition of an MXBean section in javax.management.MXBean spec.
 168      *
 169      * To create a compliant logging MXBean, define a LoggingMXBean interface
 170      * that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean
 171     */
 172     public interface LoggingMXBean
 173         extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean {
 174     }
 175 
 176     // This is a trick: if java.util.logging is not present then
 177     // attempting to access something that implements
 178     // java.util.logging.LoggingMXBean will trigger a CNFE.
 179     // So we cannot directly call any static method or access any static field
 180     // on PlatformLoggingImpl, as we would risk raising a CNFE.
 181     // Instead we use this intermediate LoggingMXBeanSupport class to determine
 182     // whether java.util.logging is present, and load the actual LoggingMXBean
 183     // implementation.
 184     //
 185     static final class LoggingMXBeanSupport {
 186         final static Object loggingImpl =
 187                 AccessController.doPrivileged(new PrivilegedAction<Object>() {
 188             @Override
 189             public Object run() {
 190                 try {
 191                     // create a LoggingProxyImpl instance when
 192                     // java.util.logging classes exist
 193                     Class<?> c = Class.forName("java.util.logging.Logging", true, null);
 194                     Constructor<?> cons = c.getDeclaredConstructor();
 195                     cons.setAccessible(true);
 196                     return cons.newInstance();
 197                 } catch (ClassNotFoundException cnf) {
 198                     return null;
 199                 } catch (NoSuchMethodException | InstantiationException
 200                         | IllegalAccessException | InvocationTargetException e) {
 201                     throw new AssertionError(e);
 202                 }
 203             }});
 204 
 205         static boolean isAvailable() {
 206             return loggingImpl != null;
 207         }
 208     }
 209 
 210     static class PlatformLoggingImpl implements LoggingMXBean
 211     {
 212         final static java.util.logging.LoggingMXBean impl =
 213                 (java.util.logging.LoggingMXBean) LoggingMXBeanSupport.loggingImpl;
 214         final static PlatformLoggingMXBean instance = new PlatformLoggingImpl();
 215         final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging";
 216 
 217         private volatile ObjectName objname;  // created lazily
 218         @Override
 219         public ObjectName getObjectName() {
 220             ObjectName result = objname;
 221             if (result == null) {
 222                 synchronized (this) {
 223                     result = objname;
 224                     if (result == null) {
 225                         result = Util.newObjectName(LOGGING_MXBEAN_NAME);
 226                         objname = result;
 227                     }
 228                 }
 229             }
 230             return result;
 231         }
 232 
 233         @Override
 234         public java.util.List<String> getLoggerNames() {
 235             return impl.getLoggerNames();
 236         }
 237 
 238         @Override
 239         public String getLoggerLevel(String loggerName) {
 240             return impl.getLoggerLevel(loggerName);
 241         }
 242 
 243         @Override
 244         public void setLoggerLevel(String loggerName, String levelName) {
 245             impl.setLoggerLevel(loggerName, levelName);
 246         }
 247 
 248         @Override
 249         public String getParentLoggerName(String loggerName) {
 250             return impl.getParentLoggerName(loggerName);
 251         }
 252     }
 253 
 254     private static List<BufferPoolMXBean> bufferPools = null;
 255     public static synchronized List<BufferPoolMXBean> getBufferPoolMXBeans() {
 256         if (bufferPools == null) {
 257             bufferPools = new ArrayList<>(2);
 258             bufferPools.add(createBufferPoolMXBean(SharedSecrets.getJavaNioAccess()
 259                 .getDirectBufferPool()));
 260             bufferPools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl
 261                 .getMappedBufferPool()));
 262         }
 263         return bufferPools;
 264     }
 265 
 266     private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";
 267 
 268     /**
 269      * Creates management interface for the given buffer pool.
 270      */


< prev index next >