blob: 4714a14fce305b1703a6c62a9ebe94e5c115e515 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.management;
27
28import java.lang.management.MemoryNotificationInfo;
29import java.lang.management.MemoryUsage;
30import javax.management.openmbean.CompositeData;
31import javax.management.openmbean.CompositeType;
32import javax.management.openmbean.CompositeDataSupport;
33import javax.management.openmbean.OpenDataException;
34
35/**
36 * A CompositeData for MemoryNotificationInfo for the local management support.
37 * This class avoids the performance penalty paid to the
38 * construction of a CompositeData use in the local case.
39 */
40public class MemoryNotifInfoCompositeData extends LazyCompositeData {
41 private final MemoryNotificationInfo memoryNotifInfo;
42
43 private MemoryNotifInfoCompositeData(MemoryNotificationInfo info) {
44 this.memoryNotifInfo = info;
45 }
46
47 public MemoryNotificationInfo getMemoryNotifInfo() {
48 return memoryNotifInfo;
49 }
50
51 public static CompositeData toCompositeData(MemoryNotificationInfo info) {
52 MemoryNotifInfoCompositeData mnicd =
53 new MemoryNotifInfoCompositeData(info);
54 return mnicd.getCompositeData();
55 }
56
57 protected CompositeData getCompositeData() {
58 // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
59 // memoryNotifInfoItemNames!
60 final Object[] memoryNotifInfoItemValues = {
61 memoryNotifInfo.getPoolName(),
62 MemoryUsageCompositeData.toCompositeData(memoryNotifInfo.getUsage()),
63 new Long(memoryNotifInfo.getCount()),
64 };
65
66 try {
67 return new CompositeDataSupport(memoryNotifInfoCompositeType,
68 memoryNotifInfoItemNames,
69 memoryNotifInfoItemValues);
70 } catch (OpenDataException e) {
71 // Should never reach here
72 throw Util.newInternalError(e);
73 }
74 }
75
76 private static final CompositeType memoryNotifInfoCompositeType;
77 static {
78 try {
79 memoryNotifInfoCompositeType = (CompositeType)
80 MappedMXBeanType.toOpenType(MemoryNotificationInfo.class);
81 } catch (OpenDataException e) {
82 // Should never reach here
83 throw Util.newInternalError(e);
84 }
85 }
86
87 private static final String POOL_NAME = "poolName";
88 private static final String USAGE = "usage";
89 private static final String COUNT = "count";
90 private static final String[] memoryNotifInfoItemNames = {
91 POOL_NAME,
92 USAGE,
93 COUNT,
94 };
95
96
97 public static String getPoolName(CompositeData cd) {
98 String poolname = getString(cd, POOL_NAME);
99 if (poolname == null) {
100 throw new IllegalArgumentException("Invalid composite data: " +
101 "Attribute " + POOL_NAME + " has null value");
102 }
103 return poolname;
104 }
105
106 public static MemoryUsage getUsage(CompositeData cd) {
107 CompositeData usageData = (CompositeData) cd.get(USAGE);
108 return MemoryUsage.from(usageData);
109 }
110
111 public static long getCount(CompositeData cd) {
112 return getLong(cd, COUNT);
113 }
114
115 /** Validate if the input CompositeData has the expected
116 * CompositeType (i.e. contain all attributes with expected
117 * names and types).
118 */
119 public static void validateCompositeData(CompositeData cd) {
120 if (cd == null) {
121 throw new NullPointerException("Null CompositeData");
122 }
123
124 if (!isTypeMatched(memoryNotifInfoCompositeType, cd.getCompositeType())) {
125 throw new IllegalArgumentException(
126 "Unexpected composite type for MemoryNotificationInfo");
127 }
128 }
129
130}