001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.management;
027:
028: import java.lang.management.MonitorInfo;
029: import javax.management.openmbean.CompositeType;
030: import javax.management.openmbean.CompositeData;
031: import javax.management.openmbean.CompositeDataSupport;
032: import javax.management.openmbean.OpenDataException;
033: import java.util.Set;
034:
035: /**
036: * A CompositeData for MonitorInfo for the local management support.
037: * This class avoids the performance penalty paid to the
038: * construction of a CompositeData use in the local case.
039: */
040: public class MonitorInfoCompositeData extends LazyCompositeData {
041: private final MonitorInfo lock;
042:
043: private MonitorInfoCompositeData(MonitorInfo mi) {
044: this .lock = mi;
045: }
046:
047: public MonitorInfo getMonitorInfo() {
048: return lock;
049: }
050:
051: public static CompositeData toCompositeData(MonitorInfo mi) {
052: MonitorInfoCompositeData micd = new MonitorInfoCompositeData(mi);
053: return micd.getCompositeData();
054: }
055:
056: protected CompositeData getCompositeData() {
057: // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
058: // monitorInfoItemNames!
059:
060: int len = monitorInfoItemNames.length;
061: Object[] values = new Object[len];
062: CompositeData li = LockDataConverter
063: .toLockInfoCompositeData(lock);
064:
065: for (int i = 0; i < len; i++) {
066: String item = monitorInfoItemNames[i];
067: if (item.equals(LOCKED_STACK_FRAME)) {
068: StackTraceElement ste = lock.getLockedStackFrame();
069: values[i] = (ste != null ? StackTraceElementCompositeData
070: .toCompositeData(ste)
071: : null);
072: } else if (item.equals(LOCKED_STACK_DEPTH)) {
073: values[i] = new Integer(lock.getLockedStackDepth());
074: } else {
075: values[i] = li.get(item);
076: }
077: }
078:
079: try {
080: return new CompositeDataSupport(monitorInfoCompositeType,
081: monitorInfoItemNames, values);
082: } catch (OpenDataException e) {
083: // Should never reach here
084: throw Util.newInternalError(e);
085: }
086: }
087:
088: private static final CompositeType monitorInfoCompositeType;
089: private static final String[] monitorInfoItemNames;
090: static {
091: try {
092: monitorInfoCompositeType = (CompositeType) MappedMXBeanType
093: .toOpenType(MonitorInfo.class);
094: Set<String> s = monitorInfoCompositeType.keySet();
095: monitorInfoItemNames = (String[]) s.toArray(new String[0]);
096: } catch (OpenDataException e) {
097: // Should never reach here
098: throw Util.newInternalError(e);
099: }
100: }
101:
102: static CompositeType getMonitorInfoCompositeType() {
103: return monitorInfoCompositeType;
104: }
105:
106: private static final String CLASS_NAME = "className";
107: private static final String IDENTITY_HASH_CODE = "identityHashCode";
108: private static final String LOCKED_STACK_FRAME = "lockedStackFrame";
109: private static final String LOCKED_STACK_DEPTH = "lockedStackDepth";
110:
111: public static String getClassName(CompositeData cd) {
112: return getString(cd, CLASS_NAME);
113: }
114:
115: public static int getIdentityHashCode(CompositeData cd) {
116: return getInt(cd, IDENTITY_HASH_CODE);
117: }
118:
119: public static StackTraceElement getLockedStackFrame(CompositeData cd) {
120: CompositeData ste = (CompositeData) cd.get(LOCKED_STACK_FRAME);
121: if (ste != null) {
122: return StackTraceElementCompositeData.from(ste);
123: } else {
124: return null;
125: }
126: }
127:
128: public static int getLockedStackDepth(CompositeData cd) {
129: return getInt(cd, LOCKED_STACK_DEPTH);
130: }
131:
132: /** Validate if the input CompositeData has the expected
133: * CompositeType (i.e. contain all attributes with expected
134: * names and types).
135: */
136: public static void validateCompositeData(CompositeData cd) {
137: if (cd == null) {
138: throw new NullPointerException("Null CompositeData");
139: }
140:
141: if (!isTypeMatched(monitorInfoCompositeType, cd
142: .getCompositeType())) {
143: throw new IllegalArgumentException(
144: "Unexpected composite type for MonitorInfo");
145: }
146: }
147: }
|