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.MemoryNotificationInfo;
029: import java.lang.management.MemoryUsage;
030: import javax.management.openmbean.CompositeData;
031: import javax.management.openmbean.CompositeType;
032: import javax.management.openmbean.CompositeDataSupport;
033: import javax.management.openmbean.OpenDataException;
034:
035: /**
036: * A CompositeData for MemoryNotificationInfo 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 MemoryNotifInfoCompositeData extends LazyCompositeData {
041: private final MemoryNotificationInfo memoryNotifInfo;
042:
043: private MemoryNotifInfoCompositeData(MemoryNotificationInfo info) {
044: this .memoryNotifInfo = info;
045: }
046:
047: public MemoryNotificationInfo getMemoryNotifInfo() {
048: return memoryNotifInfo;
049: }
050:
051: public static CompositeData toCompositeData(
052: MemoryNotificationInfo info) {
053: MemoryNotifInfoCompositeData mnicd = new MemoryNotifInfoCompositeData(
054: info);
055: return mnicd.getCompositeData();
056: }
057:
058: protected CompositeData getCompositeData() {
059: // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
060: // memoryNotifInfoItemNames!
061: final Object[] memoryNotifInfoItemValues = {
062: memoryNotifInfo.getPoolName(),
063: MemoryUsageCompositeData
064: .toCompositeData(memoryNotifInfo.getUsage()),
065: new Long(memoryNotifInfo.getCount()), };
066:
067: try {
068: return new CompositeDataSupport(
069: memoryNotifInfoCompositeType,
070: memoryNotifInfoItemNames, memoryNotifInfoItemValues);
071: } catch (OpenDataException e) {
072: // Should never reach here
073: throw Util.newInternalError(e);
074: }
075: }
076:
077: private static final CompositeType memoryNotifInfoCompositeType;
078: static {
079: try {
080: memoryNotifInfoCompositeType = (CompositeType) MappedMXBeanType
081: .toOpenType(MemoryNotificationInfo.class);
082: } catch (OpenDataException e) {
083: // Should never reach here
084: throw Util.newInternalError(e);
085: }
086: }
087:
088: private static final String POOL_NAME = "poolName";
089: private static final String USAGE = "usage";
090: private static final String COUNT = "count";
091: private static final String[] memoryNotifInfoItemNames = {
092: POOL_NAME, USAGE, COUNT, };
093:
094: public static String getPoolName(CompositeData cd) {
095: String poolname = getString(cd, POOL_NAME);
096: if (poolname == null) {
097: throw new IllegalArgumentException(
098: "Invalid composite data: " + "Attribute "
099: + POOL_NAME + " has null value");
100: }
101: return poolname;
102: }
103:
104: public static MemoryUsage getUsage(CompositeData cd) {
105: CompositeData usageData = (CompositeData) cd.get(USAGE);
106: return MemoryUsage.from(usageData);
107: }
108:
109: public static long getCount(CompositeData cd) {
110: return getLong(cd, COUNT);
111: }
112:
113: /** Validate if the input CompositeData has the expected
114: * CompositeType (i.e. contain all attributes with expected
115: * names and types).
116: */
117: public static void validateCompositeData(CompositeData cd) {
118: if (cd == null) {
119: throw new NullPointerException("Null CompositeData");
120: }
121:
122: if (!isTypeMatched(memoryNotifInfoCompositeType, cd
123: .getCompositeType())) {
124: throw new IllegalArgumentException(
125: "Unexpected composite type for MemoryNotificationInfo");
126: }
127: }
128:
129: }
|