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 com.sun.management.VMOption;
029: import com.sun.management.VMOption.Origin;
030: import javax.management.openmbean.CompositeType;
031: import javax.management.openmbean.CompositeData;
032: import javax.management.openmbean.CompositeDataSupport;
033: import javax.management.openmbean.OpenDataException;
034:
035: /**
036: * A CompositeData for VMOption 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 VMOptionCompositeData extends LazyCompositeData {
041: private final VMOption option;
042:
043: private VMOptionCompositeData(VMOption option) {
044: this .option = option;
045: }
046:
047: public VMOption getVMOption() {
048: return option;
049: }
050:
051: public static CompositeData toCompositeData(VMOption option) {
052: VMOptionCompositeData vcd = new VMOptionCompositeData(option);
053: return vcd.getCompositeData();
054: }
055:
056: protected CompositeData getCompositeData() {
057: // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
058: // vmOptionItemNames!
059: final Object[] vmOptionItemValues = { option.getName(),
060: option.getValue(), new Boolean(option.isWriteable()),
061: option.getOrigin().toString(), };
062:
063: try {
064: return new CompositeDataSupport(vmOptionCompositeType,
065: vmOptionItemNames, vmOptionItemValues);
066: } catch (OpenDataException e) {
067: // Should never reach here
068: throw Util.newInternalError(e);
069: }
070: }
071:
072: private static final CompositeType vmOptionCompositeType;
073: static {
074: try {
075: vmOptionCompositeType = (CompositeType) MappedMXBeanType
076: .toOpenType(VMOption.class);
077: } catch (OpenDataException e) {
078: // Should never reach here
079: throw Util.newInternalError(e);
080: }
081: }
082:
083: static CompositeType getVMOptionCompositeType() {
084: return vmOptionCompositeType;
085: }
086:
087: private static final String NAME = "name";
088: private static final String VALUE = "value";
089: private static final String WRITEABLE = "writeable";
090: private static final String ORIGIN = "origin";
091:
092: private static final String[] vmOptionItemNames = { NAME, VALUE,
093: WRITEABLE, ORIGIN, };
094:
095: public static String getName(CompositeData cd) {
096: return getString(cd, NAME);
097: }
098:
099: public static String getValue(CompositeData cd) {
100: return getString(cd, VALUE);
101: }
102:
103: public static Origin getOrigin(CompositeData cd) {
104: String o = getString(cd, ORIGIN);
105: return Enum.valueOf(Origin.class, o);
106: }
107:
108: public static boolean isWriteable(CompositeData cd) {
109: return getBoolean(cd, WRITEABLE);
110: }
111:
112: /** Validate if the input CompositeData has the expected
113: * CompositeType (i.e. contain all attributes with expected
114: * names and types).
115: */
116: public static void validateCompositeData(CompositeData cd) {
117: if (cd == null) {
118: throw new NullPointerException("Null CompositeData");
119: }
120:
121: if (!isTypeMatched(vmOptionCompositeType, cd.getCompositeType())) {
122: throw new IllegalArgumentException(
123: "Unexpected composite type for VMOption");
124: }
125: }
126: }
|