0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.harmony.lang.management;
0019:
0020: import java.lang.management.ClassLoadingMXBean;
0021: import java.lang.management.CompilationMXBean;
0022: import java.lang.management.GarbageCollectorMXBean;
0023: import java.lang.management.MemoryMXBean;
0024: import java.lang.management.MemoryManagerMXBean;
0025: import java.lang.management.MemoryNotificationInfo;
0026: import java.lang.management.MemoryPoolMXBean;
0027: import java.lang.management.MemoryType;
0028: import java.lang.management.MemoryUsage;
0029: import java.lang.management.OperatingSystemMXBean;
0030: import java.lang.management.RuntimeMXBean;
0031: import java.lang.management.ThreadInfo;
0032: import java.lang.management.ThreadMXBean;
0033: import java.lang.reflect.Array;
0034: import java.lang.reflect.InvocationTargetException;
0035: import java.lang.reflect.Method;
0036: import java.security.AccessController;
0037: import java.security.PrivilegedAction;
0038: import java.util.ArrayList;
0039: import java.util.Collection;
0040: import java.util.HashMap;
0041: import java.util.Iterator;
0042: import java.util.LinkedList;
0043: import java.util.List;
0044: import java.util.Map;
0045: import java.util.Set;
0046: import java.util.logging.LoggingMXBean;
0047:
0048: import javax.management.MBeanAttributeInfo;
0049: import javax.management.MBeanInfo;
0050: import javax.management.MBeanNotificationInfo;
0051: import javax.management.MBeanOperationInfo;
0052: import javax.management.MBeanParameterInfo;
0053: import javax.management.openmbean.ArrayType;
0054: import javax.management.openmbean.CompositeData;
0055: import javax.management.openmbean.CompositeDataSupport;
0056: import javax.management.openmbean.CompositeType;
0057: import javax.management.openmbean.OpenDataException;
0058: import javax.management.openmbean.OpenType;
0059: import javax.management.openmbean.SimpleType;
0060: import javax.management.openmbean.TabularData;
0061: import javax.management.openmbean.TabularDataSupport;
0062: import javax.management.openmbean.TabularType;
0063:
0064: /**
0065: * Support methods for org.apache.harmony.lang.management classes.
0066: *
0067: */
0068: public class ManagementUtils {
0069:
0070: private static Map<String, MBeanInfo> infoMap = buildInfoMap();
0071:
0072: private static CompositeType MEMORYUSAGE_COMPOSITETYPE;
0073:
0074: private static CompositeType MEMORYNOTIFICATIONINFO_COMPOSITETYPE;
0075:
0076: private static CompositeType THREADINFO_COMPOSITETYPE;
0077:
0078: private static CompositeType STACKTRACEELEMENT_COMPOSITETYPE;
0079:
0080: /**
0081: * System property setting used to decide if non-fatal exceptions should be
0082: * written out to console.
0083: */
0084: public static final boolean VERBOSE_MODE = checkVerboseProperty();
0085:
0086: /**
0087: * @return the singleton <code>ClassLoadingMXBean</code> instance.
0088: */
0089: public static ClassLoadingMXBeanImpl getClassLoadingBean() {
0090: return ClassLoadingMXBeanImpl.getInstance();
0091: }
0092:
0093: /**
0094: * @return boolean indication of whether or not the system property
0095: * <code>org.apache.harmony.lang.management.verbose</code> has been set.
0096: */
0097: private static boolean checkVerboseProperty() {
0098: return AccessController
0099: .doPrivileged(new PrivilegedAction<Boolean>() {
0100: public Boolean run() {
0101: return System
0102: .getProperty("org.apache.harmony.lang.management.verbose") != null;
0103: }// end method run
0104: });
0105: }
0106:
0107: /**
0108: * Convenenience method to return the {@link MBeanInfo} object that
0109: * corresponds to the specified <code>MXBean</code> type.
0110: *
0111: * @param name
0112: * the fully qualified name of an <code>MXBean</code>
0113: * @return if <code>name</code> has the value of a known
0114: * <code>MXBean</code> type then returns the
0115: * <code>MBeanInfo</code> meta data for that type. If
0116: * <code>name</code> is not the name of a known
0117: * <code>MXBean</code> kind then returns <code>null</code>.
0118: */
0119: static MBeanInfo getMBeanInfo(String name) {
0120: return infoMap.get(name);
0121: }
0122:
0123: /**
0124: * Builds a <code>Map</code> of all the {@link MBeanInfo} instances for
0125: * each of the platform beans. The map is keyed off the name of each bean
0126: * type.
0127: *
0128: * @return a <code>Map</code> of all the platform beans'
0129: * <code>MBeanInfo</code> instances.
0130: */
0131: private static Map<String, MBeanInfo> buildInfoMap() {
0132: HashMap<String, MBeanInfo> map = new HashMap<String, MBeanInfo>();
0133: addClassLoadingInfo(map);
0134: addCompilationBeanInfo(map);
0135: addLoggingBeanInfo(map);
0136: addMemoryManagerBeanInfo(map);
0137: addGarbageCollectorBeanInfo(map);
0138: addMemoryBeanInfo(map);
0139: addMemoryPoolBeanInfo(map);
0140: addOperatingSystemBeanInfo(map);
0141: addRuntimeBeanInfo(map);
0142: addThreadBeanInfo(map);
0143: return map;
0144: }
0145:
0146: /**
0147: * Creates the metadata for the {@link java.lang.management.ThreadMXBean}.
0148: * For this type of platform bean the metadata covers :
0149: * <ul>
0150: * <li>12 attributes
0151: * <li>0 constructors
0152: * <li>8 operations
0153: * <li>0 notifications
0154: * </ul>
0155: *
0156: * @param map
0157: */
0158: private static void addThreadBeanInfo(HashMap<String, MBeanInfo> map) {
0159: // Attributes
0160: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[12];
0161: attributes[0] = new MBeanAttributeInfo("AllThreadIds", "[J",
0162: "AllThreadIds", true, false, false);
0163:
0164: attributes[1] = new MBeanAttributeInfo("CurrentThreadCpuTime",
0165: Long.TYPE.getName(), "CurrentThreadCpuTime", true,
0166: false, false);
0167:
0168: attributes[2] = new MBeanAttributeInfo("CurrentThreadUserTime",
0169: Long.TYPE.getName(), "CurrentThreaduserTime", true,
0170: false, false);
0171:
0172: attributes[3] = new MBeanAttributeInfo("DaemonThreadCount",
0173: Integer.TYPE.getName(), "DaemonThreadCount", true,
0174: false, false);
0175:
0176: attributes[4] = new MBeanAttributeInfo("PeakThreadCount",
0177: Integer.TYPE.getName(), "PeakThreadCount", true, false,
0178: false);
0179:
0180: attributes[5] = new MBeanAttributeInfo("ThreadCount",
0181: Integer.TYPE.getName(), "ThreadCount", true, false,
0182: false);
0183:
0184: attributes[6] = new MBeanAttributeInfo(
0185: "TotalStartedThreadCount", Long.TYPE.getName(),
0186: "TotalStartedThreadCount", true, false, false);
0187:
0188: attributes[7] = new MBeanAttributeInfo(
0189: "CurrentThreadCpuTimeSupported",
0190: Boolean.TYPE.getName(),
0191: "CurrentThreadCpuTimeSupported", true, false, true);
0192:
0193: attributes[8] = new MBeanAttributeInfo(
0194: "ThreadContentionMonitoringEnabled", Boolean.TYPE
0195: .getName(),
0196: "ThreadContentionMonitoringEnabled", true, true, true);
0197:
0198: attributes[9] = new MBeanAttributeInfo(
0199: "ThreadContentionMonitoringSupported", Boolean.TYPE
0200: .getName(),
0201: "ThreadContentionMonitoringSupported", true, false,
0202: true);
0203:
0204: attributes[10] = new MBeanAttributeInfo("ThreadCpuTimeEnabled",
0205: Boolean.TYPE.getName(), "ThreadCpuTimeEnabled", true,
0206: true, true);
0207:
0208: attributes[11] = new MBeanAttributeInfo(
0209: "ThreadCpuTimeSupported", Boolean.TYPE.getName(),
0210: "ThreadCpuTimeSupported", true, false, true);
0211:
0212: // Operations
0213: MBeanOperationInfo[] operations = new MBeanOperationInfo[8];
0214: MBeanParameterInfo[] nullParams = new MBeanParameterInfo[0];
0215:
0216: operations[0] = new MBeanOperationInfo(
0217: "findMonitorDeadlockedThreads",
0218: "findMonitorDeadlockedThreads", nullParams, "[J",
0219: MBeanOperationInfo.ACTION_INFO);
0220:
0221: {
0222: MBeanParameterInfo[] params = new MBeanParameterInfo[1];
0223: params[0] = new MBeanParameterInfo("id", Long.TYPE
0224: .getName(), "id");
0225: operations[1] = new MBeanOperationInfo("getThreadCpuTime",
0226: "getThreadCpuTime", params, Long.TYPE.getName(),
0227: MBeanOperationInfo.ACTION_INFO);
0228: }
0229:
0230: {
0231: MBeanParameterInfo[] params = new MBeanParameterInfo[1];
0232: params[0] = new MBeanParameterInfo("id", Long.TYPE
0233: .getName(), "id");
0234: operations[2] = new MBeanOperationInfo("getThreadInfo",
0235: "getThreadInfo", params, CompositeData.class
0236: .getName(), MBeanOperationInfo.ACTION_INFO);
0237: }
0238:
0239: {
0240: MBeanParameterInfo[] params = new MBeanParameterInfo[1];
0241: params[0] = new MBeanParameterInfo("ids", "[J", "ids");
0242: operations[3] = new MBeanOperationInfo("getThreadInfo",
0243: "getThreadInfo", params, "[L"
0244: + CompositeData.class.getName() + ";",
0245: MBeanOperationInfo.ACTION_INFO);
0246: }
0247:
0248: {
0249: MBeanParameterInfo[] params = new MBeanParameterInfo[2];
0250: params[0] = new MBeanParameterInfo("ids", "[J", "ids");
0251: params[1] = new MBeanParameterInfo("maxDepth", Integer.TYPE
0252: .getName(), "maxDepth");
0253: operations[4] = new MBeanOperationInfo("getThreadInfo",
0254: "getThreadInfo", params, "[L"
0255: + CompositeData.class.getName() + ";",
0256: MBeanOperationInfo.ACTION_INFO);
0257: }
0258:
0259: {
0260: MBeanParameterInfo[] params = new MBeanParameterInfo[2];
0261: params[0] = new MBeanParameterInfo("id", Long.TYPE
0262: .getName(), "id");
0263: params[1] = new MBeanParameterInfo("maxDepth", Integer.TYPE
0264: .getName(), "maxDepth");
0265: operations[5] = new MBeanOperationInfo("getThreadInfo",
0266: "getThreadInfo", params, CompositeData.class
0267: .getName(), MBeanOperationInfo.ACTION_INFO);
0268: }
0269:
0270: {
0271: MBeanParameterInfo[] params = new MBeanParameterInfo[1];
0272: params[0] = new MBeanParameterInfo("id", Long.TYPE
0273: .getName(), "id");
0274: operations[6] = new MBeanOperationInfo("getThreadUserTime",
0275: "getThreadUserTime", params, Long.TYPE.getName(),
0276: MBeanOperationInfo.ACTION_INFO);
0277: }
0278:
0279: operations[7] = new MBeanOperationInfo("resetPeakThreadCount",
0280: "resetPeakThreadCount", nullParams,
0281: Void.TYPE.getName(), MBeanOperationInfo.ACTION_INFO);
0282:
0283: map.put(ThreadMXBean.class.getName(), new MBeanInfo(
0284: ThreadMXBeanImpl.class.getName(),
0285: ThreadMXBeanImpl.class.getName(), attributes, null,
0286: operations, null));
0287: }
0288:
0289: /**
0290: * Creates the metadata for the {@link java.lang.management.RuntimeMXBean}.
0291: * For this type of platform bean the metadata covers :
0292: * <ul>
0293: * <li>16 attributes
0294: * <li>0 constructors
0295: * <li>0 operations
0296: * <li>0 notifications
0297: * </ul>
0298: *
0299: * @param map
0300: */
0301: private static void addRuntimeBeanInfo(
0302: HashMap<String, MBeanInfo> map) {
0303: // Attributes
0304: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[16];
0305: attributes[0] = new MBeanAttributeInfo("BootClassPath",
0306: String.class.getName(), "BootClassPath", true, false,
0307: false);
0308: attributes[1] = new MBeanAttributeInfo("ClassPath",
0309: String.class.getName(), "ClassPath", true, false, false);
0310: attributes[2] = new MBeanAttributeInfo("InputArguments",
0311: "[Ljava.lang.String;", "InputArguments", true, false,
0312: false);
0313: attributes[3] = new MBeanAttributeInfo("LibraryPath",
0314: String.class.getName(), "LibraryPath", true, false,
0315: false);
0316: attributes[4] = new MBeanAttributeInfo("ManagementSpecVersion",
0317: String.class.getName(), "ManagementSpecVersion", true,
0318: false, false);
0319: attributes[5] = new MBeanAttributeInfo("Name", String.class
0320: .getName(), "Name", true, false, false);
0321: attributes[6] = new MBeanAttributeInfo("SpecName", String.class
0322: .getName(), "SpecName", true, false, false);
0323: attributes[7] = new MBeanAttributeInfo("SpecVendor",
0324: String.class.getName(), "SpecVendor", true, false,
0325: false);
0326: attributes[8] = new MBeanAttributeInfo("SpecVersion",
0327: String.class.getName(), "SpecVersion", true, false,
0328: false);
0329: attributes[9] = new MBeanAttributeInfo("StartTime", Long.TYPE
0330: .getName(), "StartTime", true, false, false);
0331: attributes[10] = new MBeanAttributeInfo("SystemProperties",
0332: TabularData.class.getName(), "SystemProperties", true,
0333: false, false);
0334: attributes[11] = new MBeanAttributeInfo("Uptime", Long.TYPE
0335: .getName(), "Uptime", true, false, false);
0336: attributes[12] = new MBeanAttributeInfo("VmName", String.class
0337: .getName(), "VmName", true, false, false);
0338: attributes[13] = new MBeanAttributeInfo("VmVendor",
0339: String.class.getName(), "VmVendor", true, false, false);
0340: attributes[14] = new MBeanAttributeInfo("VmVersion",
0341: String.class.getName(), "VmVersion", true, false, false);
0342: attributes[15] = new MBeanAttributeInfo(
0343: "BootClassPathSupported", Boolean.TYPE.getName(),
0344: "BootClassPathSupported", true, false, true);
0345: map.put(RuntimeMXBean.class.getName(), new MBeanInfo(
0346: RuntimeMXBeanImpl.class.getName(),
0347: RuntimeMXBeanImpl.class.getName(), attributes, null,
0348: null, null));
0349: }
0350:
0351: /**
0352: * Creates the metadata for the
0353: * {@link java.lang.management.OperatingSystemMXBean}. For this type of
0354: * platform bean the metadata covers :
0355: * <ul>
0356: * <li>4 attributes
0357: * <li>0 constructors
0358: * <li>0 operations
0359: * <li>0 notifications
0360: * </ul>
0361: *
0362: * @param map
0363: */
0364: private static void addOperatingSystemBeanInfo(
0365: HashMap<String, MBeanInfo> map) {
0366: // Attributes
0367: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[4];
0368: // Standard attributes...
0369: attributes[0] = new MBeanAttributeInfo("Arch", String.class
0370: .getName(), "Arch", true, false, false);
0371: attributes[1] = new MBeanAttributeInfo("AvailableProcessors",
0372: Integer.TYPE.getName(), "AvailableProcessors", true,
0373: false, false);
0374: attributes[2] = new MBeanAttributeInfo("Name", String.class
0375: .getName(), "Name", true, false, false);
0376: attributes[3] = new MBeanAttributeInfo("Version", String.class
0377: .getName(), "Version", true, false, false);
0378:
0379: // No operations
0380:
0381: // No notifications
0382:
0383: MBeanInfo mbeanInfo = new MBeanInfo(
0384: OperatingSystemMXBeanImpl.class.getName(),
0385: OperatingSystemMXBeanImpl.class.getName(), attributes,
0386: null, null, null);
0387:
0388: map.put(OperatingSystemMXBean.class.getName(), mbeanInfo);
0389: }
0390:
0391: /**
0392: * Creates the metadata for the
0393: * {@link java.lang.management.MemoryPoolMXBean}. For this type of platform
0394: * bean the metadata covers :
0395: * <ul>
0396: * <li>15 attributes
0397: * <li>0 constructors
0398: * <li>1 operations
0399: * <li>0 notifications
0400: * </ul>
0401: *
0402: * @param map
0403: */
0404: private static void addMemoryPoolBeanInfo(
0405: HashMap<String, MBeanInfo> map) {
0406: // Attributes
0407: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[15];
0408: attributes[0] = new MBeanAttributeInfo("CollectionUsage",
0409: CompositeData.class.getName(), "CollectionUsage", true,
0410: false, false);
0411: attributes[1] = new MBeanAttributeInfo(
0412: "CollectionUsageThreshold", Long.TYPE.getName(),
0413: "CollectionUsageThreshold", true, true, false);
0414: attributes[2] = new MBeanAttributeInfo(
0415: "CollectionUsageThresholdCount", Long.TYPE.getName(),
0416: "CollectionUsageThresholdCount", true, false, false);
0417: attributes[3] = new MBeanAttributeInfo("MemoryManagerNames",
0418: "[Ljava.lang.String;", "MemoryManagerNames", true,
0419: false, false);
0420: attributes[4] = new MBeanAttributeInfo("Name", String.class
0421: .getName(), "Name", true, false, false);
0422: attributes[5] = new MBeanAttributeInfo("PeakUsage",
0423: CompositeData.class.getName(), "PeakUsage", true,
0424: false, false);
0425: attributes[6] = new MBeanAttributeInfo("Type", String.class
0426: .getName(), "Type", true, false, false);
0427: attributes[7] = new MBeanAttributeInfo("Usage",
0428: CompositeData.class.getName(), "Usage", true, false,
0429: false);
0430: attributes[8] = new MBeanAttributeInfo("UsageThreshold",
0431: Long.TYPE.getName(), "UsageThreshold", true, true,
0432: false);
0433: attributes[9] = new MBeanAttributeInfo("UsageThresholdCount",
0434: Long.TYPE.getName(), "UsageThresholdCount", true,
0435: false, false);
0436: attributes[10] = new MBeanAttributeInfo(
0437: "CollectionUsageThresholdExceeded", Boolean.TYPE
0438: .getName(), "CollectionUsageThresholdExceeded",
0439: true, false, true);
0440: attributes[11] = new MBeanAttributeInfo(
0441: "CollectionUsageThresholdSupported", Boolean.TYPE
0442: .getName(),
0443: "CollectionUsageThresholdSupported", true, false, true);
0444: attributes[12] = new MBeanAttributeInfo(
0445: "UsageThresholdExceeded", Boolean.TYPE.getName(),
0446: "UsageThresholdExceeded", true, false, true);
0447: attributes[13] = new MBeanAttributeInfo(
0448: "UsageThresholdSupported", Boolean.TYPE.getName(),
0449: "UsageThresholdSupported", true, false, true);
0450: attributes[14] = new MBeanAttributeInfo("Valid", Boolean.TYPE
0451: .getName(), "Valid", true, false, true);
0452:
0453: // Operations
0454: MBeanOperationInfo[] operations = new MBeanOperationInfo[1];
0455: MBeanParameterInfo[] params = new MBeanParameterInfo[0];
0456: operations[0] = new MBeanOperationInfo("resetPeakUsage",
0457: "resetPeakUsage", params, Void.TYPE.getName(),
0458: MBeanOperationInfo.ACTION_INFO);
0459:
0460: MBeanInfo mbeanInfo = new MBeanInfo(MemoryPoolMXBeanImpl.class
0461: .getName(), MemoryPoolMXBeanImpl.class.getName(),
0462: attributes, null, operations, null);
0463: map.put(MemoryPoolMXBean.class.getName(), mbeanInfo);
0464: }
0465:
0466: /**
0467: * Creates the metadata for the {@link java.lang.management.MemoryMXBean}.
0468: * For this type of platform bean the metadata covers :
0469: * <ul>
0470: * <li>4 attributes
0471: * <li>0 constructors
0472: * <li>1 operations
0473: * <li>1 notification
0474: * </ul>
0475: *
0476: * @param map
0477: */
0478: private static void addMemoryBeanInfo(HashMap<String, MBeanInfo> map) {
0479: // Attributes
0480: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[4];
0481: attributes[0] = new MBeanAttributeInfo("HeapMemoryUsage",
0482: CompositeData.class.getName(), "HeapMemoryUsage", true,
0483: false, false);
0484: attributes[1] = new MBeanAttributeInfo("NonHeapMemoryUsage",
0485: CompositeData.class.getName(), "NonHeapMemoryUsage",
0486: true, false, false);
0487: attributes[2] = new MBeanAttributeInfo(
0488: "ObjectPendingFinalizationCount", Integer.TYPE
0489: .getName(), "ObjectPendingFinalizationCount",
0490: true, false, false);
0491: attributes[3] = new MBeanAttributeInfo("Verbose", Boolean.TYPE
0492: .getName(), "Verbose", true, true, true);
0493:
0494: // Operations
0495: MBeanOperationInfo[] operations = new MBeanOperationInfo[1];
0496: MBeanParameterInfo[] params = new MBeanParameterInfo[0];
0497: operations[0] = new MBeanOperationInfo("gc", "gc", params,
0498: Void.TYPE.getName(), MBeanOperationInfo.ACTION_INFO);
0499:
0500: // Notifications
0501: MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[1];
0502: String[] notifTypes = new String[2];
0503: notifTypes[0] = MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED;
0504: notifTypes[1] = MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED;
0505: notifications[0] = new MBeanNotificationInfo(notifTypes,
0506: javax.management.Notification.class.getName(),
0507: "Memory Notification");
0508:
0509: MBeanInfo mbeanInfo = new MBeanInfo(MemoryMXBeanImpl.class
0510: .getName(), MemoryMXBeanImpl.class.getName(),
0511: attributes, null, operations, notifications);
0512: map.put(MemoryMXBean.class.getName(), mbeanInfo);
0513: }
0514:
0515: /**
0516: * Creates the metadata for the
0517: * {@link java.lang.management.MemoryManagerMXBean}. For this type of
0518: * platform bean the metadata covers :
0519: * <ul>
0520: * <li>3 attribute
0521: * <li>0 constructors
0522: * <li>0 operations
0523: * <li>0 notifications
0524: * </ul>
0525: *
0526: * @param map
0527: */
0528: private static void addMemoryManagerBeanInfo(
0529: HashMap<String, MBeanInfo> map) {
0530: // Attributes
0531: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[3];
0532: attributes[0] = new MBeanAttributeInfo("MemoryPoolNames",
0533: "[Ljava.lang.String;", "MemoryPoolNames", true, false,
0534: false);
0535: attributes[1] = new MBeanAttributeInfo("Name", String.class
0536: .getName(), "Name", true, false, false);
0537: attributes[2] = new MBeanAttributeInfo("Valid", Boolean.TYPE
0538: .getName(), "Valid", true, false, true);
0539:
0540: map.put(MemoryManagerMXBean.class.getName(), new MBeanInfo(
0541: MemoryManagerMXBeanImpl.class.getName(),
0542: MemoryManagerMXBeanImpl.class.getName(), attributes,
0543: null, null, null));
0544: }
0545:
0546: /**
0547: * Creates the metadata for the {@link java.util.logging.LoggingMXBean}.
0548: * For this type of platform bean the metadata covers :
0549: * <ul>
0550: * <li>1 attribute
0551: * <li>0 constructors
0552: * <li>3 operations
0553: * <li>0 notifications
0554: * </ul>
0555: *
0556: * @param result
0557: */
0558: private static void addLoggingBeanInfo(
0559: HashMap<String, MBeanInfo> result) {
0560: // Attributes
0561: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[1];
0562: attributes[0] = new MBeanAttributeInfo("LoggerNames",
0563: "[Ljava.lang.String;", "LoggerNames", true, false,
0564: false);
0565:
0566: // Operations
0567: MBeanOperationInfo[] operations = new MBeanOperationInfo[3];
0568:
0569: {
0570: MBeanParameterInfo[] params = new MBeanParameterInfo[1];
0571: params[0] = new MBeanParameterInfo("loggerName",
0572: String.class.getName(), "loggerName");
0573: operations[0] = new MBeanOperationInfo("getLoggerLevel",
0574: "getLoggerLevel", params, String.class.getName(),
0575: MBeanOperationInfo.ACTION_INFO);
0576: }
0577:
0578: {
0579: MBeanParameterInfo[] params = new MBeanParameterInfo[1];
0580: params[0] = new MBeanParameterInfo("loggerName",
0581: String.class.getName(), "loggerName");
0582: operations[1] = new MBeanOperationInfo(
0583: "getParentLoggerName", "getParentLoggerName",
0584: params, String.class.getName(),
0585: MBeanOperationInfo.ACTION_INFO);
0586: }
0587:
0588: {
0589: MBeanParameterInfo[] params = new MBeanParameterInfo[2];
0590: params[0] = new MBeanParameterInfo("loggerName",
0591: String.class.getName(), "loggerName");
0592: params[1] = new MBeanParameterInfo("levelName",
0593: String.class.getName(), "levelName");
0594: operations[2] = new MBeanOperationInfo("setLoggerLevel",
0595: "setLoggerLevel", params, Void.TYPE.getName(),
0596: MBeanOperationInfo.ACTION_INFO);
0597: }
0598: result.put(LoggingMXBean.class.getName(), new MBeanInfo(
0599: LoggingMXBeanImpl.class.getName(),
0600: LoggingMXBeanImpl.class.getName(), attributes, null,
0601: operations, null));
0602: }
0603:
0604: /**
0605: * Creates the metadata for the {@link GarbageCollectorMXBean}. For this
0606: * type of platform bean the metadata covers :
0607: * <ul>
0608: * <li>5 attributes
0609: * <li>0 constructors
0610: * <li>0 operations
0611: * <li>0 notifications
0612: * </ul>
0613: *
0614: * @param map
0615: */
0616: private static void addGarbageCollectorBeanInfo(
0617: HashMap<String, MBeanInfo> map) {
0618: // Note that GarbageCollectorMXBean extends MemoryManagerMXBean.
0619: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[5];
0620:
0621: // Standard attributes...
0622: attributes[0] = new MBeanAttributeInfo("MemoryPoolNames",
0623: "[Ljava.lang.String;", "MemoryPoolNames", true, false,
0624: false);
0625: attributes[1] = new MBeanAttributeInfo("Name", String.class
0626: .getName(), "Name", true, false, false);
0627: attributes[2] = new MBeanAttributeInfo("Valid", Boolean.TYPE
0628: .getName(), "Valid", true, false, true);
0629: attributes[3] = new MBeanAttributeInfo("CollectionCount",
0630: Long.TYPE.getName(), "CollectionCount", true, false,
0631: false);
0632: attributes[4] = new MBeanAttributeInfo("CollectionTime",
0633: Long.TYPE.getName(), "CollectionTime", true, false,
0634: false);
0635:
0636: MBeanInfo mbeanInfo = new MBeanInfo(
0637: GarbageCollectorMXBeanImpl.class.getName(),
0638: GarbageCollectorMXBeanImpl.class.getName(), attributes,
0639: null, null, null);
0640: map.put(GarbageCollectorMXBean.class.getName(), mbeanInfo);
0641: }
0642:
0643: /**
0644: * Creates the metadata for the
0645: * {@link java.lang.management.CompilationMXBean}. For this type of
0646: * platform bean the metadata covers :
0647: * <ul>
0648: * <li>3 attributes
0649: * <li>0 constructors
0650: * <li>0 operations
0651: * <li>0 notifications
0652: * </ul>
0653: *
0654: * @param result
0655: */
0656: private static void addCompilationBeanInfo(
0657: HashMap<String, MBeanInfo> result) {
0658: // Attributes
0659: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[3];
0660: attributes[0] = new MBeanAttributeInfo("Name", String.class
0661: .getName(), "Name", true, false, false);
0662: attributes[1] = new MBeanAttributeInfo("TotalCompilationTime",
0663: Long.TYPE.getName(), "TotalCompilationTime", true,
0664: false, false);
0665: attributes[2] = new MBeanAttributeInfo(
0666: "CompilationTimeMonitoringSupported", Boolean.TYPE
0667: .getName(),
0668: "CompilationTimeMonitoringSupported", true, false, true);
0669: result.put(CompilationMXBean.class.getName(), new MBeanInfo(
0670: CompilationMXBeanImpl.class.getName(),
0671: CompilationMXBeanImpl.class.getName(), attributes,
0672: null, null, null));
0673: }
0674:
0675: /**
0676: * Creates the metadata for the {@link ClassLoadingMXBean}bean. For this
0677: * type of platform bean the metadata covers :
0678: * <ul>
0679: * <li>4 attributes
0680: * <li>0 constructors
0681: * <li>0 operations
0682: * <li>0 notifications
0683: * </ul>
0684: *
0685: * @param result
0686: */
0687: private static void addClassLoadingInfo(
0688: HashMap<String, MBeanInfo> result) {
0689: // Attributes
0690: MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[4];
0691: attributes[0] = new MBeanAttributeInfo("LoadedClassCount",
0692: Integer.TYPE.getName(), "LoadedClassCount", true,
0693: false, false);
0694: attributes[1] = new MBeanAttributeInfo("TotalLoadedClassCount",
0695: Long.TYPE.getName(), "TotalLoadedClassCount", true,
0696: false, false);
0697: attributes[2] = new MBeanAttributeInfo("UnloadedClassCount",
0698: Long.TYPE.getName(), "UnloadedClassCount", true, false,
0699: false);
0700: attributes[3] = new MBeanAttributeInfo("Verbose", Boolean.TYPE
0701: .getName(), "Verbose", true, true, true);
0702: result.put(ClassLoadingMXBean.class.getName(), new MBeanInfo(
0703: ClassLoadingMXBeanImpl.class.getName(),
0704: ClassLoadingMXBeanImpl.class.getName(), attributes,
0705: null, null, null));
0706: }
0707:
0708: /**
0709: * @return the singleton <code>MemoryMXBean</code> instance.
0710: */
0711: public static MemoryMXBeanImpl getMemoryBean() {
0712: return MemoryMXBeanImpl.getInstance();
0713: }
0714:
0715: /**
0716: * @return the singleton <code>ThreadMXBean</code> instance.
0717: */
0718: public static ThreadMXBeanImpl getThreadBean() {
0719: return ThreadMXBeanImpl.getInstance();
0720: }
0721:
0722: /**
0723: * @return the singleton <code>RuntimeMXBean</code> instance.
0724: */
0725: public static RuntimeMXBeanImpl getRuntimeBean() {
0726: return RuntimeMXBeanImpl.getInstance();
0727: }
0728:
0729: /**
0730: * @return the singleton <code>RuntimeMXBean</code> instance.
0731: */
0732: public static OperatingSystemMXBeanImpl getOperatingSystemBean() {
0733: return OperatingSystemMXBeanImpl.getInstance();
0734: }
0735:
0736: /**
0737: * @return the singleton <code>CompilationMXBean</code> if available.
0738: */
0739: public static CompilationMXBeanImpl getCompliationBean() {
0740: return CompilationMXBeanImpl.getInstance();
0741: }
0742:
0743: /**
0744: * @return the singleton <code>LoggingMXBean</code> instance.
0745: */
0746: public static LoggingMXBeanImpl getLoggingBean() {
0747: return LoggingMXBeanImpl.getInstance();
0748: }
0749:
0750: /**
0751: * Returns a list of all of the instances of {@link MemoryManagerMXBean}in
0752: * this virtual machine. Owing to the dynamic nature of this kind of
0753: * <code>MXBean</code>, it is possible that instances may be created or
0754: * destroyed between the invocation and return of this method.
0755: *
0756: * @return a list of all known <code>MemoryManagerMXBean</code> s in this
0757: * virtual machine.
0758: */
0759: public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
0760: return new LinkedList<MemoryManagerMXBean>(getMemoryBean()
0761: .getMemoryManagerMXBeans());
0762: }
0763:
0764: /**
0765: * Returns a list of all of the instances of {@link MemoryPoolMXBean}in
0766: * this virtual machine. Owing to the dynamic nature of this kind of
0767: * <code>MXBean</code>, it is possible that instances may be created or
0768: * destroyed between the invocation and return of this method.
0769: *
0770: * @return a list of all known <code>MemoryPoolMXBean</code> s in this
0771: * virtual machine.
0772: */
0773: public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
0774: List<MemoryPoolMXBean> result = new LinkedList<MemoryPoolMXBean>();
0775: Iterator<MemoryManagerMXBean> iter = getMemoryManagerMXBeans()
0776: .iterator();
0777: while (iter.hasNext()) {
0778: MemoryManagerMXBeanImpl b = (MemoryManagerMXBeanImpl) iter
0779: .next();
0780: result.addAll(b.getMemoryPoolMXBeans());
0781: }
0782: return result;
0783: }
0784:
0785: /**
0786: * Returns a list of all of the instances of {@link GarbageCollectorMXBean}
0787: * in this virtual machine. Owing to the dynamic nature of this kind of
0788: * <code>MXBean</code>, it is possible that instances may be created or
0789: * destroyed between the invocation and return of this method.
0790: *
0791: * @return a list of all known <code>GarbageCollectorMXBean</code> s in
0792: * this virtual machine.
0793: */
0794: public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
0795: List<GarbageCollectorMXBean> result = new LinkedList<GarbageCollectorMXBean>();
0796: Iterator<MemoryManagerMXBean> iter = getMemoryBean()
0797: .getMemoryManagerMXBeans().iterator();
0798: while (iter.hasNext()) {
0799: MemoryManagerMXBean b = iter.next();
0800: if (b instanceof GarbageCollectorMXBean) {
0801: result.add((GarbageCollectorMXBean) b);
0802: }
0803: }
0804: return result;
0805: }
0806:
0807: /**
0808: * Throws an {@link IllegalArgumentException}if the {@link CompositeData}
0809: * argument <code>cd</code> contains attributes that are not of the exact
0810: * types specified in the <code>expectedTypes</code> argument. The
0811: * attribute types of <code>cd</code> must also match the order of types
0812: * in <code>expectedTypes</code>.
0813: *
0814: * @param cd
0815: * a <code>CompositeData</code> object
0816: * @param expectedNames
0817: * an array of expected attribute names
0818: * @param expectedTypes
0819: * an array of type names
0820: */
0821: public static void verifyFieldTypes(CompositeData cd,
0822: String[] expectedNames, String[] expectedTypes) {
0823: Object[] allVals = cd.getAll(expectedNames);
0824: // Check that the number of elements match
0825: if (allVals.length != expectedTypes.length) {
0826: throw new IllegalArgumentException(
0827: "CompositeData does not contain the expected number of attributes.");
0828: }
0829:
0830: // Type of corresponding elements must be the same
0831: for (int i = 0; i < allVals.length; i++) {
0832: String expectedType = expectedTypes[i];
0833: Object actualVal = allVals[i];
0834: // It is permissible that a value in the CompositeData object is
0835: // null in which case we cannot test its type. Move on.
0836: if (actualVal == null) {
0837: continue;
0838: }
0839: String actualType = actualVal.getClass().getName();
0840: if (!actualType.equals(expectedType)) {
0841: // Handle CompositeData and CompositeDataSupport
0842: if (expectedType.equals(CompositeData.class.getName())) {
0843: if (allVals[i] instanceof CompositeData) {
0844: continue;
0845: }
0846: } else {
0847: throw new IllegalArgumentException(
0848: "CompositeData contains an attribute not of expected type. "
0849: + "Expected " + expectedType
0850: + ", found " + actualType);
0851: }
0852: }
0853: }// end for
0854: }
0855:
0856: /**
0857: * Throws an {@link IllegalArgumentException}if the {@link CompositeData}
0858: * argument <code>cd</code> does not have any of the attributes named in
0859: * the <code>expected</code> array of strings.
0860: *
0861: * @param cd
0862: * a <code>CompositeData</code> object
0863: * @param expected
0864: * an array of attribute names expected in <code>cd</code>.
0865: */
0866: public static void verifyFieldNames(CompositeData cd,
0867: String[] expected) {
0868: for (int i = 0; i < expected.length; i++) {
0869: if (!cd.containsKey(expected[i])) {
0870: throw new IllegalArgumentException(
0871: "CompositeData object does not contain expected key : " //$NON-NLS-1$
0872: + expected[i]);
0873: }
0874: }// end for all elements in expected
0875: }
0876:
0877: /**
0878: * Throws an {@link IllegalArgumentException}if the {@link CompositeData}
0879: * argument <code>cd</code> does not have the number of attributes
0880: * specified in <code>i</code>.
0881: *
0882: * @param cd
0883: * a <code>CompositeData</code> object
0884: * @param i
0885: * the number of expected attributes in <code>cd</code>
0886: */
0887: public static void verifyFieldNumber(CompositeData cd, int i) {
0888: if (cd.values().size() != i) {
0889: throw new IllegalArgumentException(
0890: "CompositeData object does not have the expected number of attributes"); //$NON-NLS-1$
0891: }
0892: }
0893:
0894: /**
0895: * @param usage
0896: * a {@link MemoryUsage}object.
0897: * @return a {@link CompositeData}object that represents the supplied
0898: * <code>usage</code> object.
0899: */
0900: public static CompositeData toMemoryUsageCompositeData(
0901: MemoryUsage usage) {
0902: // Bail out early on null input.
0903: if (usage == null) {
0904: return null;
0905: }
0906:
0907: CompositeData result = null;
0908: String[] names = { "init", "used", "committed", "max" };
0909: Object[] values = { new Long(usage.getInit()),
0910: new Long(usage.getUsed()),
0911: new Long(usage.getCommitted()),
0912: new Long(usage.getMax()) };
0913: CompositeType cType = getMemoryUsageCompositeType();
0914: try {
0915: result = new CompositeDataSupport(cType, names, values);
0916: } catch (OpenDataException e) {
0917: if (ManagementUtils.VERBOSE_MODE) {
0918: e.printStackTrace(System.err);
0919: }// end if
0920: }
0921: return result;
0922: }
0923:
0924: /**
0925: * @return an instance of {@link CompositeType}for the {@link MemoryUsage}
0926: * class.
0927: */
0928: public static CompositeType getMemoryUsageCompositeType() {
0929: if (MEMORYUSAGE_COMPOSITETYPE == null) {
0930: String[] typeNames = { "init", "used", "committed", "max" };
0931: String[] typeDescs = { "init", "used", "committed", "max" };
0932: OpenType[] typeTypes = { SimpleType.LONG, SimpleType.LONG,
0933: SimpleType.LONG, SimpleType.LONG };
0934: try {
0935: MEMORYUSAGE_COMPOSITETYPE = new CompositeType(
0936: MemoryUsage.class.getName(), MemoryUsage.class
0937: .getName(), typeNames, typeDescs,
0938: typeTypes);
0939: } catch (OpenDataException e) {
0940: if (ManagementUtils.VERBOSE_MODE) {
0941: e.printStackTrace(System.err);
0942: }// end if
0943: }
0944: }
0945: return MEMORYUSAGE_COMPOSITETYPE;
0946: }
0947:
0948: /**
0949: * @param info
0950: * a {@link java.lang.management.MemoryNotificationInfo}object.
0951: * @return a {@link CompositeData}object that represents the supplied
0952: * <code>info</code> object.
0953: */
0954: public static CompositeData toMemoryNotificationInfoCompositeData(
0955: MemoryNotificationInfo info) {
0956: // Bail out early on null input.
0957: if (info == null) {
0958: return null;
0959: }
0960:
0961: CompositeData result = null;
0962: String[] names = { "poolName", "usage", "count" };
0963: Object[] values = { new String(info.getPoolName()),
0964: toMemoryUsageCompositeData(info.getUsage()),
0965: new Long(info.getCount()) };
0966: CompositeType cType = getMemoryNotificationInfoCompositeType();
0967: try {
0968: result = new CompositeDataSupport(cType, names, values);
0969: } catch (OpenDataException e) {
0970: if (ManagementUtils.VERBOSE_MODE) {
0971: e.printStackTrace(System.err);
0972: }// end if
0973: }
0974: return result;
0975: }
0976:
0977: /**
0978: * @return an instance of {@link CompositeType}for the
0979: * {@link MemoryNotificationInfo}class.
0980: */
0981: private static CompositeType getMemoryNotificationInfoCompositeType() {
0982: if (MEMORYNOTIFICATIONINFO_COMPOSITETYPE == null) {
0983: String[] typeNames = { "poolName", "usage", "count" };
0984: String[] typeDescs = { "poolName", "usage", "count" };
0985: OpenType[] typeTypes = { SimpleType.STRING,
0986: getMemoryUsageCompositeType(), SimpleType.LONG };
0987: try {
0988: MEMORYNOTIFICATIONINFO_COMPOSITETYPE = new CompositeType(
0989: MemoryNotificationInfo.class.getName(),
0990: MemoryNotificationInfo.class.getName(),
0991: typeNames, typeDescs, typeTypes);
0992: } catch (OpenDataException e) {
0993: if (ManagementUtils.VERBOSE_MODE) {
0994: e.printStackTrace(System.err);
0995: }// end if
0996: }
0997: }
0998: return MEMORYNOTIFICATIONINFO_COMPOSITETYPE;
0999: }
1000:
1001: /**
1002: * @param info
1003: * a {@link ThreadInfo}object.
1004: * @return a {@link CompositeData}object that represents the supplied
1005: * <code>info</code> object.
1006: */
1007: public static CompositeData toThreadInfoCompositeData(
1008: ThreadInfo info) {
1009: // Bail out early on null input.
1010: if (info == null) {
1011: return null;
1012: }
1013:
1014: CompositeData result = null;
1015: StackTraceElement[] st = info.getStackTrace();
1016: CompositeData[] stArray = new CompositeData[st.length];
1017: for (int i = 0; i < st.length; i++) {
1018: stArray[i] = toStackTraceElementCompositeData(st[i]);
1019: }// end for
1020:
1021: String[] names = { "threadId", "threadName", "threadState",
1022: "suspended", "inNative", "blockedCount", "blockedTime",
1023: "waitedCount", "waitedTime", "lockName", "lockOwnerId",
1024: "lockOwnerName", "stackTrace" };
1025: Object[] values = {
1026: new Long(info.getThreadId()),
1027: new String(info.getThreadName()),
1028: new String(info.getThreadState().name()),
1029: new Boolean(info.isSuspended()),
1030: new Boolean(info.isInNative()),
1031: new Long(info.getBlockedCount()),
1032: new Long(info.getBlockedTime()),
1033: new Long(info.getWaitedCount()),
1034: new Long(info.getWaitedTime()),
1035: info.getLockName() != null ? new String(info
1036: .getLockName()) : null,
1037: new Long(info.getLockOwnerId()),
1038: info.getLockOwnerName() != null ? new String(info
1039: .getLockOwnerName()) : null, stArray };
1040: CompositeType cType = getThreadInfoCompositeType();
1041: try {
1042: result = new CompositeDataSupport(cType, names, values);
1043: } catch (OpenDataException e) {
1044: if (ManagementUtils.VERBOSE_MODE) {
1045: e.printStackTrace(System.err);
1046: }// end if
1047: }
1048: return result;
1049: }
1050:
1051: /**
1052: * @param element
1053: * a {@link StackTraceElement}object.
1054: * @return a {@link CompositeData}object that represents the supplied
1055: * <code>element</code> object.
1056: */
1057: public static CompositeData toStackTraceElementCompositeData(
1058: StackTraceElement element) {
1059: // Bail out early on null input.
1060: if (element == null) {
1061: return null;
1062: }
1063:
1064: CompositeData result = null;
1065: String[] names = { "className", "methodName", "fileName",
1066: "lineNumber", "nativeMethod" };
1067:
1068: // A file name of null is permissable
1069: String fileName = element.getFileName();
1070: String fileNameValue = (fileName == null) ? null : new String(
1071: fileName);
1072:
1073: Object[] values = { new String(element.getClassName()),
1074: new String(element.getMethodName()), fileNameValue,
1075: new Integer(element.getLineNumber()),
1076: new Boolean(element.isNativeMethod()) };
1077: CompositeType cType = getStackTraceElementCompositeType();
1078: try {
1079: result = new CompositeDataSupport(cType, names, values);
1080: } catch (OpenDataException e) {
1081: if (ManagementUtils.VERBOSE_MODE) {
1082: e.printStackTrace(System.err);
1083: }// end if
1084: }
1085: return result;
1086: }
1087:
1088: /**
1089: * @return an instance of {@link CompositeType}for the {@link ThreadInfo}
1090: * class.
1091: */
1092: private static CompositeType getThreadInfoCompositeType() {
1093: if (THREADINFO_COMPOSITETYPE == null) {
1094: try {
1095: String[] typeNames = { "threadId", "threadName",
1096: "threadState", "suspended", "inNative",
1097: "blockedCount", "blockedTime", "waitedCount",
1098: "waitedTime", "lockName", "lockOwnerId",
1099: "lockOwnerName", "stackTrace" };
1100: String[] typeDescs = { "threadId", "threadName",
1101: "threadState", "suspended", "inNative",
1102: "blockedCount", "blockedTime", "waitedCount",
1103: "waitedTime", "lockName", "lockOwnerId",
1104: "lockOwnerName", "stackTrace" };
1105: OpenType[] typeTypes = {
1106: SimpleType.LONG,
1107: SimpleType.STRING,
1108: SimpleType.STRING,
1109: SimpleType.BOOLEAN,
1110: SimpleType.BOOLEAN,
1111: SimpleType.LONG,
1112: SimpleType.LONG,
1113: SimpleType.LONG,
1114: SimpleType.LONG,
1115: SimpleType.STRING,
1116: SimpleType.LONG,
1117: SimpleType.STRING,
1118: new ArrayType(1,
1119: getStackTraceElementCompositeType()) };
1120: THREADINFO_COMPOSITETYPE = new CompositeType(
1121: ThreadInfo.class.getName(), ThreadInfo.class
1122: .getName(), typeNames, typeDescs,
1123: typeTypes);
1124: } catch (OpenDataException e) {
1125: if (ManagementUtils.VERBOSE_MODE) {
1126: e.printStackTrace(System.err);
1127: }// end if
1128: }
1129: }
1130: return THREADINFO_COMPOSITETYPE;
1131: }
1132:
1133: /**
1134: * @return an instance of {@link CompositeType}for the
1135: * {@link StackTraceElement}class.
1136: */
1137: private static CompositeType getStackTraceElementCompositeType() {
1138: if (STACKTRACEELEMENT_COMPOSITETYPE == null) {
1139: String[] typeNames = { "className", "methodName",
1140: "fileName", "lineNumber", "nativeMethod" };
1141: String[] typeDescs = { "className", "methodName",
1142: "fileName", "lineNumber", "nativeMethod" };
1143: OpenType[] typeTypes = { SimpleType.STRING,
1144: SimpleType.STRING, SimpleType.STRING,
1145: SimpleType.INTEGER, SimpleType.BOOLEAN };
1146: try {
1147: STACKTRACEELEMENT_COMPOSITETYPE = new CompositeType(
1148: StackTraceElement.class.getName(),
1149: StackTraceElement.class.getName(), typeNames,
1150: typeDescs, typeTypes);
1151: } catch (OpenDataException e) {
1152: if (ManagementUtils.VERBOSE_MODE) {
1153: e.printStackTrace(System.err);
1154: }// end if
1155: }
1156: }
1157: return STACKTRACEELEMENT_COMPOSITETYPE;
1158: }
1159:
1160: /**
1161: * Convenience method to converts an array of <code>String</code> to a
1162: * <code>List<String></code>.
1163: *
1164: * @param data
1165: * an array of <code>String</code>
1166: * @return a new <code>List<String></code>
1167: */
1168: public static List<String> convertStringArrayToList(String[] data) {
1169: List<String> result = new ArrayList<String>();
1170: for (int i = 0; i < data.length; i++) {
1171: result.add(data[i]);
1172: }// end for
1173: return result;
1174: }
1175:
1176: /**
1177: * Receives an instance of a {@link TabularData}whose data is wrapping a
1178: * <code>Map</code> and returns a new instance of <code>Map</code>
1179: * containing the input information.
1180: *
1181: * @param data
1182: * an instance of <code>TabularData</code> that may be mapped
1183: * to a <code>Map</code>.
1184: * @return a new {@link Map}containing the information originally wrapped
1185: * in the <code>data</code> input.
1186: * @throws IllegalArgumentException
1187: * if <code>data</code> has a <code>CompositeType</code>
1188: * that does not contain exactly two items (i.e. a key and a
1189: * value).
1190: */
1191: @SuppressWarnings("unchecked")
1192: public static Object convertTabularDataToMap(TabularData data) {
1193: // Bail out early on null input.
1194: if (data == null) {
1195: return null;
1196: }
1197:
1198: Map<Object, Object> result = new HashMap<Object, Object>();
1199: Set<String> cdKeySet = data.getTabularType().getRowType()
1200: .keySet();
1201: // The key set for the CompositeData instances comprising each row
1202: // must contain only two elements.
1203: if (cdKeySet.size() != 2) {
1204: throw new IllegalArgumentException(
1205: "TabularData's row type is not a CompositeType with two items.");
1206: }
1207: String[] keysArray = new String[2];
1208: int count = 0;
1209: Iterator<String> keysIt = cdKeySet.iterator();
1210: while (keysIt.hasNext()) {
1211: keysArray[count++] = keysIt.next();
1212: }// end while
1213:
1214: Collection<CompositeData> rows = data.values();
1215: Iterator<CompositeData> rowIterator = rows.iterator();
1216: while (rowIterator.hasNext()) {
1217: CompositeData rowCD = rowIterator.next();
1218: result
1219: .put(rowCD.get(keysArray[0]), rowCD
1220: .get(keysArray[1]));
1221: }// end while a row to process
1222: return result;
1223: }
1224:
1225: /**
1226: * Return a new instance of type <code>T</code> from the supplied
1227: * {@link CompositeData} object whose type maps to <code>T</code>.
1228: *
1229: * @param <T>
1230: * the type of object wrapped by the <code>CompositeData</code>.
1231: * @param data
1232: * an instance of <code>CompositeData</code> that maps to an
1233: * instance of <code>T</code>
1234: * @param realClass
1235: * the {@link Class} object for type <code>T</code>
1236: * @return a new instance of <code>T</code>
1237: * @throws NoSuchMethodException
1238: * @throws SecurityException
1239: * @throws InvocationTargetException
1240: * @throws IllegalAccessException
1241: * @throws IllegalArgumentException
1242: */
1243: @SuppressWarnings("unchecked")
1244: public static <T> T convertFromCompositeData(CompositeData data,
1245: Class<T> realClass) throws SecurityException,
1246: NoSuchMethodException, IllegalArgumentException,
1247: IllegalAccessException, InvocationTargetException {
1248: // Bail out early on null input.
1249: if (data == null) {
1250: return null;
1251: }
1252:
1253: // See if the realClass has a static for method that takes a
1254: // CompositeData and returns a new instance of T.
1255: Method forMethod = realClass.getMethod("from",
1256: new Class[] { CompositeData.class });
1257: return (T) forMethod.invoke(null, data);
1258: }
1259:
1260: /**
1261: * Receive data of the type specified in <code>openClass</code> and return
1262: * it in an instance of the type specified in <code>realClass</code>.
1263: *
1264: * @param <T>
1265: *
1266: * @param data
1267: * an instance of the type named <code>openTypeName</code>
1268: * @param openClass
1269: * @param realClass
1270: * @return a new instance of the type <code>realTypeName</code> containing
1271: * all the state in the input <code>data</code> object.
1272: * @throws ClassNotFoundException
1273: * @throws IllegalAccessException
1274: * @throws InstantiationException
1275: * @throws InvocationTargetException
1276: * @throws NoSuchMethodException
1277: * @throws IllegalArgumentException
1278: * @throws SecurityException
1279: */
1280: @SuppressWarnings("unchecked")
1281: public static <T> T convertFromOpenType(Object data,
1282: Class<?> openClass, Class<T> realClass)
1283: throws ClassNotFoundException, InstantiationException,
1284: IllegalAccessException, SecurityException,
1285: IllegalArgumentException, NoSuchMethodException,
1286: InvocationTargetException {
1287: // Bail out early on null input.
1288: if (data == null) {
1289: return null;
1290: }
1291:
1292: T result = null;
1293:
1294: if (openClass.isArray() && realClass.isArray()) {
1295: Class openElementClass = openClass.getComponentType();
1296: Class<?> realElementClass = realClass.getComponentType();
1297:
1298: Object[] dataArray = (Object[]) data;
1299: result = (T) Array.newInstance(realElementClass,
1300: dataArray.length);
1301: for (int i = 0; i < Array.getLength(result); i++) {
1302: Array.set(result, i, convertFromOpenType(dataArray[i],
1303: openElementClass, realElementClass));
1304: }// end for
1305: } else if (openClass.equals(CompositeData.class)) {
1306: result = ManagementUtils.convertFromCompositeData(
1307: (CompositeData) data, realClass);
1308: } else if (openClass.equals(TabularData.class)) {
1309: if (realClass.equals(Map.class)) {
1310: result = (T) ManagementUtils
1311: .convertTabularDataToMap((TabularData) data);
1312: }
1313: } else if (openClass.equals(String[].class)) {
1314: if (realClass.equals(List.class)) {
1315: result = (T) ManagementUtils
1316: .convertStringArrayToList((String[]) data);
1317: }
1318: } else if (openClass.equals(String.class)) {
1319: if (realClass.equals(MemoryType.class)) {
1320: result = (T) ManagementUtils
1321: .convertStringToMemoryType((String) data);
1322: }
1323: }
1324: return result;
1325: }
1326:
1327: /**
1328: * Convenience method that receives a string representation of a
1329: * <code>MemoryType</code> instance and returns the actual
1330: * <code>MemoryType</code> that corresponds to that value.
1331: *
1332: * @param data
1333: * a string
1334: * @return if <code>data</code> can be used to obtain an instance of
1335: * <code>MemoryType</code> then a <code>MemoryType</code>,
1336: * otherwise <code>null</code>.
1337: */
1338: private static MemoryType convertStringToMemoryType(String data) {
1339: MemoryType result = null;
1340: try {
1341: result = MemoryType.valueOf(data);
1342: } catch (IllegalArgumentException e) {
1343: if (ManagementUtils.VERBOSE_MODE) {
1344: e.printStackTrace(System.err);
1345: }// end if
1346: }
1347: return result;
1348: }
1349:
1350: /**
1351: * Convenience method to convert an object, <code>data</code> from its
1352: * Java type <code>realClass</code> to the specified open MBean type
1353: * <code>openClass</code>.
1354: *
1355: * @param <T>
1356: * the open MBean class
1357: * @param data
1358: * the object to be converted
1359: * @param openClass
1360: * the open MBean class
1361: * @param realClass
1362: * the real Java type of <code>data</code>
1363: * @return a new instance of type <code>openClass</code>
1364: */
1365: @SuppressWarnings("unchecked")
1366: public static <T> T convertToOpenType(Object data,
1367: Class<T> openClass, Class<?> realClass) {
1368: // Bail out early on null input.
1369: if (data == null) {
1370: return null;
1371: }
1372:
1373: T result = null;
1374:
1375: if (openClass.isArray() && realClass.isArray()) {
1376: Class<?> openElementClass = openClass.getComponentType();
1377: Class<?> realElementClass = realClass.getComponentType();
1378:
1379: Object[] dataArray = (Object[]) data;
1380: result = (T) Array.newInstance(openElementClass,
1381: dataArray.length);
1382: for (int i = 0; i < Array.getLength(result); i++) {
1383: Array.set(result, i, convertToOpenType(dataArray[i],
1384: openElementClass, realElementClass));
1385: }// end for
1386: } else if (openClass.equals(CompositeData.class)) {
1387: if (realClass.equals(ThreadInfo.class)) {
1388: result = (T) ManagementUtils
1389: .toThreadInfoCompositeData((ThreadInfo) data);
1390: } else if (realClass.equals(MemoryUsage.class)) {
1391: result = (T) ManagementUtils
1392: .toMemoryUsageCompositeData((MemoryUsage) data);
1393: }
1394: } else if (openClass.equals(TabularData.class)) {
1395: if (realClass.equals(Map.class)) {
1396: result = (T) ManagementUtils
1397: .toSystemPropertiesTabularData((Map) data);
1398: }
1399: } else if (openClass.equals(String[].class)) {
1400: if (realClass.equals(List.class)) {
1401: result = (T) ManagementUtils.convertListToArray(
1402: (List) data, openClass, openClass
1403: .getComponentType());
1404: }
1405: } else if (openClass.equals(String.class)) {
1406: if (realClass.isEnum()) {
1407: result = (T) ((Enum) data).name();
1408: }
1409: }
1410: return result;
1411: }
1412:
1413: /**
1414: * Convenience method to convert a {@link List} instance to an instance of
1415: * an array. The element type of the returned array will be of the same type
1416: * as the <code>List</code> component values.
1417: *
1418: * @param <T>
1419: * the array type named <code>arrayType</code>
1420: * @param <E>
1421: * the type of the elements in the array,
1422: * <code>elementType</code>
1423: * @param list
1424: * the <code>List</code> to be converted
1425: * @param arrayType
1426: * the array type
1427: * @param elementType
1428: * the type of the array's elements
1429: * @return a new instance of <code>arrayType</code> initialised with the
1430: * data stored in <code>list</code>
1431: */
1432: @SuppressWarnings("unchecked")
1433: private static <T, E> T convertListToArray(List<E> list,
1434: Class<T> arrayType, Class<E> elementType) {
1435: T result = (T) Array.newInstance(elementType, list.size());
1436: Iterator<E> it = list.iterator();
1437: int count = 0;
1438: while (it.hasNext()) {
1439: E element = it.next();
1440: Array.set(result, count++, element);
1441: }
1442: return result;
1443: }
1444:
1445: /**
1446: * @param propsMap
1447: * a <code>Map<String, String%gt;</code> of the system
1448: * properties.
1449: * @return the system properties (e.g. as obtained from
1450: * {@link RuntimeMXBean#getSystemProperties()}) wrapped in a
1451: * {@link TabularData}.
1452: */
1453: public static TabularData toSystemPropertiesTabularData(
1454: Map<String, String> propsMap) {
1455: // Bail out early on null input.
1456: if (propsMap == null) {
1457: return null;
1458: }
1459:
1460: TabularData result = null;
1461: try {
1462: // Obtain the row type for the TabularType
1463: String[] rtItemNames = { "key", "value" };
1464: String[] rtItemDescs = { "key", "value" };
1465: OpenType[] rtItemTypes = { SimpleType.STRING,
1466: SimpleType.STRING };
1467:
1468: CompositeType rowType = new CompositeType(propsMap
1469: .getClass().getName(), propsMap.getClass()
1470: .getName(), rtItemNames, rtItemDescs, rtItemTypes);
1471:
1472: // Obtain the required TabularType
1473: TabularType sysPropsType = new TabularType(propsMap
1474: .getClass().getName(), propsMap.getClass()
1475: .getName(), rowType, new String[] { "key" });
1476:
1477: // Create an empty TabularData
1478: result = new TabularDataSupport(sysPropsType);
1479:
1480: // Take each entry out of the input propsMap, put it into a new
1481: // instance of CompositeData and put into the TabularType
1482: Set<String> keys = propsMap.keySet();
1483: for (Iterator<String> iter = keys.iterator(); iter
1484: .hasNext();) {
1485: String propKey = iter.next();
1486: String propVal = propsMap.get(propKey);
1487: result
1488: .put(new CompositeDataSupport(rowType,
1489: rtItemNames, new String[] { propKey,
1490: propVal }));
1491: }// end for
1492: } catch (OpenDataException e) {
1493: if (ManagementUtils.VERBOSE_MODE) {
1494: e.printStackTrace(System.err);
1495: }// end if
1496: result = null;
1497: }
1498: return result;
1499: }
1500:
1501: /**
1502: * Convenience method that sets out to return the {@link Class}object for
1503: * the specified type named <code>name</code>. Unlike the
1504: * {@link Class#forName(java.lang.String)}method, this will work even for
1505: * primitive types.
1506: *
1507: * @param name
1508: * the name of a Java type
1509: * @return the <code>Class</code> object for the type <code>name</code>
1510: * @throws ClassNotFoundException
1511: * if <code>name</code> does not correspond to any known type
1512: * (including primitive types).
1513: */
1514: public static Class getClassMaybePrimitive(String name)
1515: throws ClassNotFoundException {
1516: Class result = null;
1517:
1518: try {
1519: result = Class.forName(name);
1520: } catch (ClassNotFoundException e) {
1521: if (name.equals(boolean.class.getName())) {
1522: result = boolean.class;
1523: } else if (name.equals(char.class.getName())) {
1524: result = char.class;
1525: } else if (name.equals(byte.class.getName())) {
1526: result = byte.class;
1527: } else if (name.equals(short.class.getName())) {
1528: result = short.class;
1529: } else if (name.equals(int.class.getName())) {
1530: result = int.class;
1531: } else if (name.equals(long.class.getName())) {
1532: result = long.class;
1533: } else if (name.equals(float.class.getName())) {
1534: result = float.class;
1535: } else if (name.equals(double.class.getName())) {
1536: result = double.class;
1537: } else if (name.equals(void.class.getName())) {
1538: result = void.class;
1539: } else {
1540: if (ManagementUtils.VERBOSE_MODE) {
1541: e.printStackTrace(System.err);
1542: }// end if
1543: // Rethrow the original ClassNotFoundException
1544: throw e;
1545: }// end else
1546: }// end catch
1547: return result;
1548: }
1549:
1550: /**
1551: * Convenience method to determine if the <code>wrapper</code>
1552: * <code>Class</code>
1553: * object is really the wrapper class for the
1554: * <code>primitive</code> <code>Class</code> object.
1555: *
1556: * @param wrapper
1557: * @param primitive
1558: * @return <code>true</code> if the <code>wrapper</code> class is the
1559: * wrapper class for <code>primitive</code>. Otherwise
1560: * <code>false</code>.
1561: */
1562: public static boolean isWrapperClass(
1563: Class<? extends Object> wrapper, Class primitive) {
1564: boolean result = true;
1565: if (primitive.equals(boolean.class)
1566: && !wrapper.equals(Boolean.class)) {
1567: result = false;
1568: } else if (primitive.equals(char.class)
1569: && !wrapper.equals(Character.class)) {
1570: result = false;
1571: } else if (primitive.equals(byte.class)
1572: && !wrapper.equals(Byte.class)) {
1573: result = false;
1574: } else if (primitive.equals(short.class)
1575: && !wrapper.equals(Short.class)) {
1576: result = false;
1577: } else if (primitive.equals(int.class)
1578: && !wrapper.equals(Integer.class)) {
1579: result = false;
1580: } else if (primitive.equals(long.class)
1581: && !wrapper.equals(Long.class)) {
1582: result = false;
1583: } else if (primitive.equals(float.class)
1584: && !wrapper.equals(Float.class)) {
1585: result = false;
1586: } else if (primitive.equals(double.class)
1587: && !wrapper.equals(Double.class)) {
1588: result = false;
1589: }
1590:
1591: return result;
1592: }
1593:
1594: /**
1595: * Convenience method that returns a boolean indication of whether or not
1596: * concrete instances of the the supplied interface type
1597: * <code>mxbeanInterface</code> should also be implementors of the
1598: * interface <code>javax.management.NotificationEmitter</code>.
1599: *
1600: * @param <T>
1601: * @param mxbeanInterface
1602: * @return <code>true</code> if instances of type
1603: * <code>mxbeanInterface</code> should also implement
1604: * <code>javax.management.NotificationEmitter</code>. Otherwise,
1605: * <code>false</code>.
1606: */
1607: public static <T> boolean isANotificationEmitter(
1608: Class<T> mxbeanInterface) {
1609: boolean result = false;
1610: MBeanInfo info = getMBeanInfo(mxbeanInterface.getName());
1611: if (info != null) {
1612: MBeanNotificationInfo[] notifications = info
1613: .getNotifications();
1614: if ((notifications != null) && (notifications.length > 0)) {
1615: result = true;
1616: }
1617: }
1618: return result;
1619: }
1620: }
|