001: /*
002: * Copyright 2004 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.MemoryUsage;
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:
034: /**
035: * A CompositeData for MemoryUsage for the local management support.
036: * This class avoids the performance penalty paid to the
037: * construction of a CompositeData use in the local case.
038: */
039: public class MemoryUsageCompositeData extends LazyCompositeData {
040: private final MemoryUsage usage;
041:
042: private MemoryUsageCompositeData(MemoryUsage u) {
043: this .usage = u;
044: }
045:
046: public MemoryUsage getMemoryUsage() {
047: return usage;
048: }
049:
050: public static CompositeData toCompositeData(MemoryUsage u) {
051: MemoryUsageCompositeData mucd = new MemoryUsageCompositeData(u);
052: return mucd.getCompositeData();
053: }
054:
055: protected CompositeData getCompositeData() {
056: // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
057: // memoryUsageItemNames!
058: final Object[] memoryUsageItemValues = {
059: new Long(usage.getInit()), new Long(usage.getUsed()),
060: new Long(usage.getCommitted()),
061: new Long(usage.getMax()), };
062:
063: try {
064: return new CompositeDataSupport(memoryUsageCompositeType,
065: memoryUsageItemNames, memoryUsageItemValues);
066: } catch (OpenDataException e) {
067: // Should never reach here
068: throw Util.newInternalError(e);
069: }
070: }
071:
072: private static final CompositeType memoryUsageCompositeType;
073: static {
074: try {
075: memoryUsageCompositeType = (CompositeType) MappedMXBeanType
076: .toOpenType(MemoryUsage.class);
077: } catch (OpenDataException e) {
078: // Should never reach here
079: throw Util.newInternalError(e);
080: }
081: }
082:
083: static CompositeType getMemoryUsageCompositeType() {
084: return memoryUsageCompositeType;
085: }
086:
087: private static final String INIT = "init";
088: private static final String USED = "used";
089: private static final String COMMITTED = "committed";
090: private static final String MAX = "max";
091:
092: private static final String[] memoryUsageItemNames = { INIT, USED,
093: COMMITTED, MAX, };
094:
095: public static long getInit(CompositeData cd) {
096: return getLong(cd, INIT);
097: }
098:
099: public static long getUsed(CompositeData cd) {
100: return getLong(cd, USED);
101: }
102:
103: public static long getCommitted(CompositeData cd) {
104: return getLong(cd, COMMITTED);
105: }
106:
107: public static long getMax(CompositeData cd) {
108: return getLong(cd, MAX);
109: }
110:
111: /** Validate if the input CompositeData has the expected
112: * CompositeType (i.e. contain all attributes with expected
113: * names and types).
114: */
115: public static void validateCompositeData(CompositeData cd) {
116: if (cd == null) {
117: throw new NullPointerException("Null CompositeData");
118: }
119:
120: if (!isTypeMatched(memoryUsageCompositeType, cd
121: .getCompositeType())) {
122: throw new IllegalArgumentException(
123: "Unexpected composite type for MemoryUsage");
124: }
125: }
126: }
|