Frames | No Frames |
1: /* ManagementFactory.java - Factory for obtaining system beans. 2: Copyright (C) 2006 Free Software Foundation 3: 4: This file is part of GNU Classpath. 5: 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) 9: any later version. 10: 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. 15: 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 19: 02110-1301 USA. 20: 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 24: combination. 25: 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. */ 37: 38: package java.lang.management; 39: 40: import gnu.classpath.SystemProperties; 41: 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; 51: 52: import java.util.ArrayList; 53: import java.util.List; 54: 55: import javax.management.NotCompliantMBeanException; 56: 57: /** 58: * <p> 59: * Provides access to the system's management beans via a series 60: * of static methods. 61: * </p> 62: * <p> 63: * An instance of a system management bean can be obtained by 64: * using one of the following methods: 65: * </p> 66: * <ol> 67: * <li>Calling the appropriate static method of this factory. 68: * </li> 69: * </ol> 70: * 71: * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 72: * @since 1.5 73: */ 74: public class ManagementFactory 75: { 76: 77: /** 78: * The operating system management bean. 79: */ 80: private static OperatingSystemMXBean osBean; 81: 82: /** 83: * The runtime management bean. 84: */ 85: private static RuntimeMXBean runtimeBean; 86: 87: /** 88: * The class loading management bean. 89: */ 90: private static ClassLoadingMXBean classLoadingBean; 91: 92: /** 93: * The thread bean. 94: */ 95: private static ThreadMXBean threadBean; 96: 97: /** 98: * The memory bean. 99: */ 100: private static MemoryMXBean memoryBean; 101: 102: /** 103: * The compilation bean (may remain null). 104: */ 105: private static CompilationMXBean compilationBean; 106: 107: /** 108: * Private constructor to prevent instance creation. 109: */ 110: private ManagementFactory() {} 111: 112: /** 113: * Returns the operating system management bean for the 114: * operating system on which the virtual machine is running. 115: * 116: * @return an instance of {@link OperatingSystemMXBean} for 117: * the underlying operating system. 118: */ 119: public static OperatingSystemMXBean getOperatingSystemMXBean() 120: { 121: if (osBean == null) 122: try 123: { 124: osBean = new OperatingSystemMXBeanImpl(); 125: } 126: catch (NotCompliantMBeanException e) 127: { 128: throw new InternalError("The GNU implementation of the " + 129: "operating system bean is not a " + 130: "compliant management bean."); 131: } 132: return osBean; 133: } 134: 135: /** 136: * Returns the runtime management bean for the 137: * running virtual machine. 138: * 139: * @return an instance of {@link RuntimeMXBean} for 140: * this virtual machine. 141: */ 142: public static RuntimeMXBean getRuntimeMXBean() 143: { 144: if (runtimeBean == null) 145: try 146: { 147: runtimeBean = new RuntimeMXBeanImpl(); 148: } 149: catch (NotCompliantMBeanException e) 150: { 151: throw new InternalError("The GNU implementation of the " + 152: "runtime bean is not a compliant " + 153: "management bean."); 154: } 155: return runtimeBean; 156: } 157: 158: /** 159: * Returns the class loading management bean for the 160: * running virtual machine. 161: * 162: * @return an instance of {@link ClassLoadingMXBean} for 163: * this virtual machine. 164: */ 165: public static ClassLoadingMXBean getClassLoadingMXBean() 166: { 167: if (classLoadingBean == null) 168: try 169: { 170: classLoadingBean = new ClassLoadingMXBeanImpl(); 171: } 172: catch (NotCompliantMBeanException e) 173: { 174: throw new InternalError("The GNU implementation of the " + 175: "class loading bean is not a " + 176: "compliant management bean."); 177: } 178: return classLoadingBean; 179: } 180: 181: /** 182: * Returns the thread management bean for the running 183: * virtual machine. 184: * 185: * @return an instance of {@link ThreadMXBean} for 186: * this virtual machine. 187: */ 188: public static ThreadMXBean getThreadMXBean() 189: { 190: if (threadBean == null) 191: try 192: { 193: threadBean = new ThreadMXBeanImpl(); 194: } 195: catch (NotCompliantMBeanException e) 196: { 197: throw new InternalError("The GNU implementation of the " + 198: "thread bean is not a compliant " + 199: "management bean."); 200: } 201: return threadBean; 202: } 203: 204: /** 205: * Returns the memory management bean for the running 206: * virtual machine. 207: * 208: * @return an instance of {@link MemoryMXBean} for 209: * this virtual machine. 210: */ 211: public static MemoryMXBean getMemoryMXBean() 212: { 213: if (memoryBean == null) 214: try 215: { 216: memoryBean = new MemoryMXBeanImpl(); 217: } 218: catch (NotCompliantMBeanException e) 219: { 220: throw new InternalError("The GNU implementation of the " + 221: "memory bean is not a compliant " + 222: "management bean."); 223: } 224: return memoryBean; 225: } 226: 227: /** 228: * Returns the compilation bean for the running 229: * virtual machine, if supported. Otherwise, 230: * it returns <code>null</code>. 231: * 232: * @return an instance of {@link CompilationMXBean} for 233: * this virtual machine, or <code>null</code> 234: * if the virtual machine doesn't include 235: * a Just-In-Time (JIT) compiler. 236: */ 237: public static CompilationMXBean getCompilationMXBean() 238: { 239: if (compilationBean == null && 240: SystemProperties.getProperty("gnu.java.compiler.name") != null) 241: try 242: { 243: compilationBean = new CompilationMXBeanImpl(); 244: } 245: catch (NotCompliantMBeanException e) 246: { 247: throw new InternalError("The GNU implementation of the " + 248: "compilation bean is not a compliant " + 249: "management bean."); 250: } 251: return compilationBean; 252: } 253: 254: /** 255: * Returns the memory pool beans for the running 256: * virtual machine. These may change during the course 257: * of execution. 258: * 259: * @return a list of memory pool beans, one for each pool. 260: */ 261: public static List getMemoryPoolMXBeans() 262: { 263: List poolBeans = new ArrayList(); 264: String[] names = VMManagementFactory.getMemoryPoolNames(); 265: for (int a = 0; a < names.length; ++a) 266: try 267: { 268: poolBeans.add(new MemoryPoolMXBeanImpl(names[a])); 269: } 270: catch (NotCompliantMBeanException e) 271: { 272: throw new InternalError("The GNU implementation of the " + 273: "memory pool bean, " + a + ", is " + 274: "not a compliant management bean."); 275: } 276: return poolBeans; 277: } 278: 279: /** 280: * Returns the memory manager beans for the running 281: * virtual machine. These may change during the course 282: * of execution. 283: * 284: * @return a list of memory manager beans, one for each manager. 285: */ 286: public static List getMemoryManagerMXBeans() 287: { 288: List managerBeans = new ArrayList(); 289: String[] names = VMManagementFactory.getMemoryManagerNames(); 290: for (int a = 0; a < names.length; ++a) 291: try 292: { 293: managerBeans.add(new MemoryManagerMXBeanImpl(names[a])); 294: } 295: catch (NotCompliantMBeanException e) 296: { 297: throw new InternalError("The GNU implementation of the " + 298: "memory manager bean, " + a + ", is " + 299: "not a compliant management bean."); 300: } 301: managerBeans.addAll(getGarbageCollectorMXBeans()); 302: return managerBeans; 303: } 304: 305: /** 306: * Returns the garbage collector beans for the running 307: * virtual machine. These may change during the course 308: * of execution. 309: * 310: * @return a list of garbage collector beans, one for each pool. 311: */ 312: public static List getGarbageCollectorMXBeans() 313: { 314: List gcBeans = new ArrayList(); 315: String[] names = VMManagementFactory.getGarbageCollectorNames(); 316: for (int a = 0; a < names.length; ++a) 317: try 318: { 319: gcBeans.add(new GarbageCollectorMXBeanImpl(names[a])); 320: } 321: catch (NotCompliantMBeanException e) 322: { 323: throw new InternalError("The GNU implementation of the " + 324: "garbage collector bean, " + a + 325: ", is not a compliant management " + 326: "bean."); 327: } 328: return gcBeans; 329: } 330: 331: }