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 sun.management;
  27 
  28 import java.lang.management.*;
  29 

  30 import javax.management.InstanceAlreadyExistsException;
  31 import javax.management.InstanceNotFoundException;
  32 import javax.management.MBeanServer;
  33 import javax.management.MBeanRegistrationException;
  34 import javax.management.NotCompliantMBeanException;
  35 import javax.management.ObjectName;
  36 import javax.management.RuntimeOperationsException;
  37 import java.security.AccessController;
  38 import java.security.PrivilegedActionException;
  39 import java.security.PrivilegedExceptionAction;
  40 
  41 import sun.util.logging.LoggingSupport;
  42 
  43 import java.util.ArrayList;
  44 import java.util.Collections;

  45 import java.util.List;

  46 import com.sun.management.OSMBeanFactory;
  47 import com.sun.management.HotSpotDiagnosticMXBean;
  48 
  49 import static java.lang.management.ManagementFactory.*;
  50 
  51 /**
  52  * ManagementFactoryHelper provides static factory methods to create
  53  * instances of the management interface.
  54  */
  55 public class ManagementFactoryHelper {
  56     private ManagementFactoryHelper() {};
  57 
  58     private static VMManagement jvm;
  59 
  60     private static ClassLoadingImpl    classMBean = null;
  61     private static MemoryImpl          memoryMBean = null;
  62     private static ThreadImpl          threadMBean = null;
  63     private static RuntimeImpl         runtimeMBean = null;
  64     private static CompilationImpl     compileMBean = null;
  65     private static OperatingSystemImpl osMBean = null;
  66 
  67     public static synchronized ClassLoadingMXBean getClassLoadingMXBean() {
  68         if (classMBean == null) {
  69             classMBean = new ClassLoadingImpl(jvm);
  70         }
  71         return classMBean;
  72     }
  73 
  74     public static synchronized MemoryMXBean getMemoryMXBean() {
  75         if (memoryMBean == null) {
  76             memoryMBean = new MemoryImpl(jvm);
  77         }
  78         return memoryMBean;
  79     }
  80 
  81     public static synchronized ThreadMXBean getThreadMXBean() {
  82         if (threadMBean == null) {
  83             threadMBean = new ThreadImpl(jvm);
  84         }
  85         return threadMBean;
  86     }
  87 
  88     public static synchronized RuntimeMXBean getRuntimeMXBean() {
  89         if (runtimeMBean == null) {
  90             runtimeMBean = new RuntimeImpl(jvm);
  91         }
  92         return runtimeMBean;
  93     }
  94 
  95     public static synchronized CompilationMXBean getCompilationMXBean() {
  96         if (compileMBean == null && jvm.getCompilerName() != null) {
  97             compileMBean = new CompilationImpl(jvm);
  98         }
  99         return compileMBean;
 100     }
 101 
 102     public static synchronized OperatingSystemMXBean getOperatingSystemMXBean() {
 103         if (osMBean == null) {
 104             osMBean = (OperatingSystemImpl)
 105                           OSMBeanFactory.getOperatingSystemMXBean(jvm);
 106         }
 107         return osMBean;
 108     }
 109 
 110     public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
 111         MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools();
 112         List<MemoryPoolMXBean> list = new ArrayList<>(pools.length);
 113         for (MemoryPoolMXBean p : pools) {
 114             list.add(p);
 115         }
 116         return list;
 117     }
 118 
 119     public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
 120         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 121         List<MemoryManagerMXBean> result = new ArrayList<>(mgrs.length);
 122         for (MemoryManagerMXBean m : mgrs) {
 123             result.add(m);
 124         }
 125         return result;
 126     }
 127 
 128     public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
 129         MemoryManagerMXBean[]  mgrs = MemoryImpl.getMemoryManagers();
 130         List<GarbageCollectorMXBean> result = new ArrayList<>(mgrs.length);
 131         for (MemoryManagerMXBean m : mgrs) {
 132             if (GarbageCollectorMXBean.class.isInstance(m)) {
 133                  result.add(GarbageCollectorMXBean.class.cast(m));
 134             }
 135         }
 136         return result;
 137     }
 138 
 139     public static PlatformLoggingMXBean getPlatformLoggingMXBean() {
 140         if (LoggingSupport.isAvailable()) {
 141             return PlatformLoggingImpl.instance;
 142         } else {
 143             return null;
 144         }
 145     }
 146 
 147     // The logging MXBean object is an instance of
 148     // PlatformLoggingMXBean and java.util.logging.LoggingMXBean
 149     // but it can't directly implement two MXBean interfaces
 150     // as a compliant MXBean implements exactly one MXBean interface,
 151     // or if it implements one interface that is a subinterface of
 152     // all the others; otherwise, it is a non-compliant MXBean
 153     // and MBeanServer will throw NotCompliantMBeanException.
 154     // See the Definition of an MXBean section in javax.management.MXBean spec.
 155     //
 156     // To create a compliant logging MXBean, define a LoggingMXBean interface
 157     // that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean
 158     interface LoggingMXBean
 159         extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean {
 160     }
 161 
 162     static class PlatformLoggingImpl implements LoggingMXBean
 163     {
 164         final static PlatformLoggingMXBean instance = new PlatformLoggingImpl();
 165         final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging";
 166 
 167         private volatile ObjectName objname;  // created lazily
 168         @Override
 169         public ObjectName getObjectName() {
 170             ObjectName result = objname;
 171             if (result == null) {
 172                 synchronized (this) {
 173                     result = objname;
 174                     if (result == null) {
 175                         result = Util.newObjectName(LOGGING_MXBEAN_NAME);
 176                         objname = result;
 177                     }
 178                 }
 179             }
 180             return result;
 181         }
 182 
 183         @Override
 184         public java.util.List<String> getLoggerNames() {
 185             return LoggingSupport.getLoggerNames();
 186         }
 187 
 188         @Override
 189         public String getLoggerLevel(String loggerName) {
 190             return LoggingSupport.getLoggerLevel(loggerName);
 191         }
 192 
 193         @Override
 194         public void setLoggerLevel(String loggerName, String levelName) {
 195             LoggingSupport.setLoggerLevel(loggerName, levelName);
 196         }
 197 
 198         @Override
 199         public String getParentLoggerName(String loggerName) {
 200             return LoggingSupport.getParentLoggerName(loggerName);
 201         }
 202     }
 203 
 204     private static List<BufferPoolMXBean> bufferPools = null;
 205     public static synchronized List<BufferPoolMXBean> getBufferPoolMXBeans() {
 206         if (bufferPools == null) {
 207             bufferPools = new ArrayList<>(2);
 208             bufferPools.add(createBufferPoolMXBean(sun.misc.SharedSecrets.getJavaNioAccess()
 209                 .getDirectBufferPool()));
 210             bufferPools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl
 211                 .getMappedBufferPool()));
 212         }
 213         return bufferPools;
 214     }
 215 
 216     private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool";
 217 
 218     /**
 219      * Creates management interface for the given buffer pool.
 220      */
 221     private static BufferPoolMXBean
 222         createBufferPoolMXBean(final sun.misc.JavaNioAccess.BufferPool pool)
 223     {
 224         return new BufferPoolMXBean() {
 225             private volatile ObjectName objname;  // created lazily
 226             @Override
 227             public ObjectName getObjectName() {
 228                 ObjectName result = objname;
 229                 if (result == null) {
 230                     synchronized (this) {
 231                         result = objname;
 232                         if (result == null) {
 233                             result = Util.newObjectName(BUFFER_POOL_MXBEAN_NAME +
 234                                 ",name=" + pool.getName());
 235                             objname = result;
 236                         }
 237                     }
 238                 }
 239                 return result;
 240             }
 241             @Override
 242             public String getName() {
 243                 return pool.getName();
 244             }
 245             @Override
 246             public long getCount() {
 247                 return pool.getCount();
 248             }
 249             @Override
 250             public long getTotalCapacity() {
 251                 return pool.getTotalCapacity();
 252             }
 253             @Override
 254             public long getMemoryUsed() {
 255                 return pool.getMemoryUsed();
 256             }
 257         };
 258     }
 259 
 260     private static HotSpotDiagnostic hsDiagMBean = null;
 261     private static HotspotRuntime hsRuntimeMBean = null;
 262     private static HotspotClassLoading hsClassMBean = null;
 263     private static HotspotThread hsThreadMBean = null;
 264     private static HotspotCompilation hsCompileMBean = null;
 265     private static HotspotMemory hsMemoryMBean = null;

 266 
 267     public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() {
 268         if (hsDiagMBean == null) {
 269             hsDiagMBean = new HotSpotDiagnostic();
 270         }
 271         return hsDiagMBean;
 272     }
 273 
 274     /**
 275      * This method is for testing only.
 276      */
 277     public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
 278         if (hsRuntimeMBean == null) {
 279             hsRuntimeMBean = new HotspotRuntime(jvm);
 280         }
 281         return hsRuntimeMBean;
 282     }
 283 
 284     /**
 285      * This method is for testing only.
 286      */
 287     public static synchronized HotspotClassLoadingMBean getHotspotClassLoadingMBean() {
 288         if (hsClassMBean == null) {
 289             hsClassMBean = new HotspotClassLoading(jvm);
 290         }
 291         return hsClassMBean;
 292     }
 293 
 294     /**
 295      * This method is for testing only.
 296      */
 297     public static synchronized HotspotThreadMBean getHotspotThreadMBean() {
 298         if (hsThreadMBean == null) {
 299             hsThreadMBean = new HotspotThread(jvm);
 300         }
 301         return hsThreadMBean;
 302     }
 303 
 304     /**
 305      * This method is for testing only.
 306      */
 307     public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {
 308         if (hsMemoryMBean == null) {
 309             hsMemoryMBean = new HotspotMemory(jvm);
 310         }
 311         return hsMemoryMBean;
 312     }
 313 








 314     /**
 315      * This method is for testing only.
 316      */
 317     public static synchronized HotspotCompilationMBean getHotspotCompilationMBean() {
 318         if (hsCompileMBean == null) {
 319             hsCompileMBean = new HotspotCompilation(jvm);
 320         }
 321         return hsCompileMBean;
 322     }
 323 
 324     /**
 325      * Registers a given MBean if not registered in the MBeanServer;
 326      * otherwise, just return.
 327      */
 328     private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {
 329         try {
 330             final ObjectName objName = Util.newObjectName(mbeanName);
 331 
 332             // inner class requires these fields to be final
 333             final MBeanServer mbs0 = mbs;
 334             final Object mbean0 = mbean;
 335             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
 336                 public Void run() throws MBeanRegistrationException,
 337                                          NotCompliantMBeanException {
 338                     try {
 339                         mbs0.registerMBean(mbean0, objName);
 340                         return null;
 341                     } catch (InstanceAlreadyExistsException e) {
 342                         // if an instance with the object name exists in
 343                         // the MBeanServer ignore the exception
 344                     }
 345                     return null;
 346                 }
 347             });
 348         } catch (PrivilegedActionException e) {
 349             throw Util.newException(e.getException());
 350         }
 351     }
 352 
 353     private final static String HOTSPOT_CLASS_LOADING_MBEAN_NAME =
 354         "sun.management:type=HotspotClassLoading";
 355 
 356     private final static String HOTSPOT_COMPILATION_MBEAN_NAME =
 357         "sun.management:type=HotspotCompilation";
 358 
 359     private final static String HOTSPOT_MEMORY_MBEAN_NAME =
 360         "sun.management:type=HotspotMemory";
 361 
 362     private static final String HOTSPOT_RUNTIME_MBEAN_NAME =
 363         "sun.management:type=HotspotRuntime";
 364 
 365     private final static String HOTSPOT_THREAD_MBEAN_NAME =
 366         "sun.management:type=HotspotThreading";
 367 











 368     static void registerInternalMBeans(MBeanServer mbs) {
 369         // register all internal MBeans if not registered
 370         // No exception is thrown if a MBean with that object name
 371         // already registered
 372         addMBean(mbs, getHotspotClassLoadingMBean(),
 373             HOTSPOT_CLASS_LOADING_MBEAN_NAME);
 374         addMBean(mbs, getHotspotMemoryMBean(),
 375             HOTSPOT_MEMORY_MBEAN_NAME);
 376         addMBean(mbs, getHotspotRuntimeMBean(),
 377             HOTSPOT_RUNTIME_MBEAN_NAME);
 378         addMBean(mbs, getHotspotThreadMBean(),
 379             HOTSPOT_THREAD_MBEAN_NAME);
 380 
 381         // CompilationMBean may not exist
 382         if (getCompilationMXBean() != null) {
 383             addMBean(mbs, getHotspotCompilationMBean(),
 384                 HOTSPOT_COMPILATION_MBEAN_NAME);
 385         }
 386     }
 387 
 388     private static void unregisterMBean(MBeanServer mbs, String mbeanName) {
 389         try {
 390             final ObjectName objName = Util.newObjectName(mbeanName);
 391 
 392             // inner class requires these fields to be final
 393             final MBeanServer mbs0 = mbs;
 394             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
 395                 public Void run() throws MBeanRegistrationException,
 396                                            RuntimeOperationsException  {
 397                     try {
 398                         mbs0.unregisterMBean(objName);
 399                     } catch (InstanceNotFoundException e) {
 400                         // ignore exception if not found
 401                     }
 402                     return null;
 403                 }
 404             });
 405         } catch (PrivilegedActionException e) {
 406             throw Util.newException(e.getException());
 407         }
 408     }
 409 
 410     static void unregisterInternalMBeans(MBeanServer mbs) {
 411         // unregister all internal MBeans
 412         unregisterMBean(mbs, HOTSPOT_CLASS_LOADING_MBEAN_NAME);
 413         unregisterMBean(mbs, HOTSPOT_MEMORY_MBEAN_NAME);
 414         unregisterMBean(mbs, HOTSPOT_RUNTIME_MBEAN_NAME);
 415         unregisterMBean(mbs, HOTSPOT_THREAD_MBEAN_NAME);
 416 
 417         // CompilationMBean may not exist
 418         if (getCompilationMXBean() != null) {
 419             unregisterMBean(mbs, HOTSPOT_COMPILATION_MBEAN_NAME);
 420         }
 421     }
 422 
 423     static {
 424         AccessController.doPrivileged(
 425             new java.security.PrivilegedAction<Void>() {
 426                 public Void run() {
 427                     System.loadLibrary("management");
 428                     return null;
 429                 }
 430             });
 431         jvm = new VMManagementImpl();
 432     }
 433 
 434     public static boolean isThreadSuspended(int state) {
 435         return ((state & JMM_THREAD_STATE_FLAG_SUSPENDED) != 0);
 436     }
 437 
 438     public static boolean isThreadRunningNative(int state) {
 439         return ((state & JMM_THREAD_STATE_FLAG_NATIVE) != 0);
 440     }
 441 
 442     public static Thread.State toThreadState(int state) {
 443         // suspended and native bits may be set in state
 444         int threadStatus = state & ~JMM_THREAD_STATE_FLAG_MASK;
 445         return sun.misc.VM.toThreadState(threadStatus);
 446     }
 447 
 448     // These values are defined in jmm.h
 449     private static final int JMM_THREAD_STATE_FLAG_MASK = 0xFFF00000;
 450     private static final int JMM_THREAD_STATE_FLAG_SUSPENDED = 0x00100000;
 451     private static final int JMM_THREAD_STATE_FLAG_NATIVE = 0x00400000;
 452 
 453 }
--- EOF ---