001: /*
002: * Copyright 2005 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 javax.management.openmbean.CompositeType;
029: import javax.management.openmbean.CompositeData;
030: import javax.management.openmbean.CompositeDataSupport;
031: import javax.management.openmbean.OpenDataException;
032:
033: /**
034: * A CompositeData for StackTraceElement for the local management support.
035: * This class avoids the performance penalty paid to the
036: * construction of a CompositeData use in the local case.
037: */
038: public class StackTraceElementCompositeData extends LazyCompositeData {
039: private final StackTraceElement ste;
040:
041: private StackTraceElementCompositeData(StackTraceElement ste) {
042: this .ste = ste;
043: }
044:
045: public StackTraceElement getStackTraceElement() {
046: return ste;
047: }
048:
049: public static StackTraceElement from(CompositeData cd) {
050: validateCompositeData(cd);
051:
052: return new StackTraceElement(getString(cd, CLASS_NAME),
053: getString(cd, METHOD_NAME), getString(cd, FILE_NAME),
054: getInt(cd, LINE_NUMBER));
055: }
056:
057: public static CompositeData toCompositeData(StackTraceElement ste) {
058: StackTraceElementCompositeData cd = new StackTraceElementCompositeData(
059: ste);
060: return cd.getCompositeData();
061: }
062:
063: protected CompositeData getCompositeData() {
064: // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
065: // stackTraceElementItemNames!
066: final Object[] stackTraceElementItemValues = {
067: ste.getClassName(), ste.getMethodName(),
068: ste.getFileName(), new Integer(ste.getLineNumber()),
069: new Boolean(ste.isNativeMethod()), };
070: try {
071: return new CompositeDataSupport(
072: stackTraceElementCompositeType,
073: stackTraceElementItemNames,
074: stackTraceElementItemValues);
075: } catch (OpenDataException e) {
076: // Should never reach here
077: throw Util.newInternalError(e);
078: }
079: }
080:
081: private static final CompositeType stackTraceElementCompositeType;
082: static {
083: try {
084: stackTraceElementCompositeType = (CompositeType) MappedMXBeanType
085: .toOpenType(StackTraceElement.class);
086: } catch (OpenDataException e) {
087: // Should never reach here
088: throw Util.newInternalError(e);
089: }
090: }
091:
092: // Attribute names
093: private static final String CLASS_NAME = "className";
094: private static final String METHOD_NAME = "methodName";
095: private static final String FILE_NAME = "fileName";
096: private static final String LINE_NUMBER = "lineNumber";
097: private static final String NATIVE_METHOD = "nativeMethod";
098:
099: private static final String[] stackTraceElementItemNames = {
100: CLASS_NAME, METHOD_NAME, FILE_NAME, LINE_NUMBER,
101: NATIVE_METHOD, };
102:
103: /** Validate if the input CompositeData has the expected
104: * CompositeType (i.e. contain all attributes with expected
105: * names and types).
106: */
107: public static void validateCompositeData(CompositeData cd) {
108: if (cd == null) {
109: throw new NullPointerException("Null CompositeData");
110: }
111:
112: if (!isTypeMatched(stackTraceElementCompositeType, cd
113: .getCompositeType())) {
114: throw new IllegalArgumentException(
115: "Unexpected composite type for StackTraceElement");
116: }
117: }
118: }
|