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.MBeanServer; 31 import javax.management.ObjectName; 32 import javax.management.InstanceAlreadyExistsException; 33 import javax.management.InstanceNotFoundException; 34 import javax.management.MBeanRegistrationException; 35 import javax.management.NotCompliantMBeanException; 36 import javax.management.RuntimeOperationsException; 37 import java.nio.BufferPoolMXBean; 38 import java.security.AccessController; 39 import java.security.PrivilegedActionException; 40 import java.security.PrivilegedExceptionAction; 41 import sun.security.action.LoadLibraryAction; 42 43 import java.util.logging.PlatformLoggingMXBean; 44 import sun.util.logging.LoggingSupport; 45 46 import java.util.ArrayList; 47 import java.util.Collections; 48 import java.util.List; 49 import com.sun.management.OSMBeanFactory; 50 import com.sun.management.HotSpotDiagnosticMXBean; 51 52 import static java.lang.management.ManagementFactory.*; 53 54 /** 55 * ManagementFactoryHelper provides static factory methods to create 56 * instances of the management interface. 57 */ 58 public class ManagementFactoryHelper { 59 private ManagementFactoryHelper() {}; 60 61 private static VMManagement jvm; 62 63 private static ClassLoadingImpl classMBean = null; 122 public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() { 123 MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers(); 124 List<MemoryManagerMXBean> result = new ArrayList<MemoryManagerMXBean>(mgrs.length); 125 for (MemoryManagerMXBean m : mgrs) { 126 result.add(m); 127 } 128 return result; 129 } 130 131 public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() { 132 MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers(); 133 List<GarbageCollectorMXBean> result = new ArrayList<GarbageCollectorMXBean>(mgrs.length); 134 for (MemoryManagerMXBean m : mgrs) { 135 if (GarbageCollectorMXBean.class.isInstance(m)) { 136 result.add(GarbageCollectorMXBean.class.cast(m)); 137 } 138 } 139 return result; 140 } 141 142 public static List<PlatformLoggingMXBean> getLoggingMXBean() { 143 if (LoggingSupport.isAvailable()) { 144 return Collections.singletonList(createPlatformLoggingMXBean()); 145 } else { 146 return Collections.emptyList(); 147 } 148 } 149 150 private final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging"; 151 private static PlatformLoggingMXBean createPlatformLoggingMXBean() { 152 return new PlatformLoggingMXBean() { 153 private volatile ObjectName objname; // created lazily 154 @Override 155 public ObjectName getObjectName() { 156 ObjectName result = objname; 157 if (result == null) { 158 synchronized (this) { 159 if (objname == null) { 160 result = Util.newObjectName(LOGGING_MXBEAN_NAME); 161 objname = result; 162 } 163 } 164 } 165 return result; 166 } 167 168 @Override 169 public java.util.List<String> getLoggerNames() { 170 return LoggingSupport.getLoggerNames(); 171 } 172 173 @Override 174 public String getLoggerLevel(String loggerName) { 175 return LoggingSupport.getLoggerLevel(loggerName); 176 } 177 178 @Override 179 public void setLoggerLevel(String loggerName, String levelName) { 180 LoggingSupport.setLoggerLevel(loggerName, levelName); 181 } 182 183 @Override 184 public String getParentLoggerName(String loggerName) { 185 return LoggingSupport.getParentLoggerName(loggerName); 186 } 187 }; 188 } 189 190 public static List<BufferPoolMXBean> getBufferPoolMXBeans() { 191 List<BufferPoolMXBean> pools = new ArrayList<BufferPoolMXBean>(2); 192 pools.add(createBufferPoolMXBean(sun.misc.SharedSecrets.getJavaNioAccess() 193 .getDirectBufferPool())); 194 pools.add(createBufferPoolMXBean(sun.nio.ch.FileChannelImpl 195 .getMappedBufferPool())); 196 return pools; 197 } 198 199 private final static String BUFFER_POOL_MXBEAN_NAME = "java.nio:type=BufferPool"; 200 201 /** 202 * Creates management interface for the given buffer pool. 203 */ 204 private static BufferPoolMXBean 205 createBufferPoolMXBean(final sun.misc.JavaNioAccess.BufferPool pool) 206 { 207 return new BufferPoolMXBean() { 208 private volatile ObjectName objname; // created lazily 209 @Override 210 public ObjectName getObjectName() { 211 ObjectName result = objname; 212 if (result == null) { 213 synchronized (this) { 214 if (objname == null) { 215 result = Util.newObjectName(BUFFER_POOL_MXBEAN_NAME + 216 ",name=" + pool.getName()); 217 objname = result; | 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 import sun.security.action.LoadLibraryAction; 41 42 import sun.util.logging.LoggingSupport; 43 44 import java.util.ArrayList; 45 import java.util.Collections; 46 import java.util.List; 47 import com.sun.management.OSMBeanFactory; 48 import com.sun.management.HotSpotDiagnosticMXBean; 49 50 import static java.lang.management.ManagementFactory.*; 51 52 /** 53 * ManagementFactoryHelper provides static factory methods to create 54 * instances of the management interface. 55 */ 56 public class ManagementFactoryHelper { 57 private ManagementFactoryHelper() {}; 58 59 private static VMManagement jvm; 60 61 private static ClassLoadingImpl classMBean = null; 120 public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() { 121 MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers(); 122 List<MemoryManagerMXBean> result = new ArrayList<MemoryManagerMXBean>(mgrs.length); 123 for (MemoryManagerMXBean m : mgrs) { 124 result.add(m); 125 } 126 return result; 127 } 128 129 public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() { 130 MemoryManagerMXBean[] mgrs = MemoryImpl.getMemoryManagers(); 131 List<GarbageCollectorMXBean> result = new ArrayList<GarbageCollectorMXBean>(mgrs.length); 132 for (MemoryManagerMXBean m : mgrs) { 133 if (GarbageCollectorMXBean.class.isInstance(m)) { 134 result.add(GarbageCollectorMXBean.class.cast(m)); 135 } 136 } 137 return result; 138 } 139 140 public static PlatformLoggingMXBean getPlatformLoggingMXBean() { 141 if (LoggingSupport.isAvailable()) { 142 return PlatformLoggingImpl.instance; 143 } else { 144 return null; 145 } 146 } 147 148 // The logging MXBean object is an instance of 149 // PlatformLoggingMXBean and java.util.logging.LoggingMXBean 150 // but it can't directly implement two MXBean interfaces 151 // as a compliant MXBean implements exactly one MXBean interface, 152 // or if it implements one interface that is a subinterface of 153 // all the others; otherwise, it is a non-complaint MXBean 154 // MBeanServer will throw NotCompliantMBeanException. 155 // See the Definition of an MXBean section in javax.management.MXBean spec. 156 // 157 // To create a compliant logging MXBean, define a LoggingMXBean interface 158 // that extend PlatformLoggingMXBean and j.u.l.LoggingMXBean 159 interface LoggingMXBean 160 extends PlatformLoggingMXBean, java.util.logging.LoggingMXBean { 161 } 162 163 static class PlatformLoggingImpl implements LoggingMXBean 164 { 165 final static PlatformLoggingMXBean instance = new PlatformLoggingImpl(); 166 final static String LOGGING_MXBEAN_NAME = "java.util.logging:type=Logging"; 167 168 private volatile ObjectName objname; // created lazily 169 @Override 170 public ObjectName getObjectName() { 171 ObjectName result = objname; 172 if (result == null) { 173 synchronized (this) { 174 if (objname == 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 if (objname == null) { 232 result = Util.newObjectName(BUFFER_POOL_MXBEAN_NAME + 233 ",name=" + pool.getName()); 234 objname = result; |