|
1 /* |
|
2 * Copyright (c) 2004, 2008, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.management; |
|
27 |
|
28 import java.lang.management.MemoryUsage; |
|
29 import javax.management.openmbean.CompositeType; |
|
30 import javax.management.openmbean.CompositeData; |
|
31 import javax.management.openmbean.CompositeDataSupport; |
|
32 import javax.management.openmbean.OpenDataException; |
|
33 |
|
34 /** |
|
35 * A CompositeData for MemoryUsage for the local management support. |
|
36 * This class avoids the performance penalty paid to the |
|
37 * construction of a CompositeData use in the local case. |
|
38 */ |
|
39 public class MemoryUsageCompositeData extends LazyCompositeData { |
|
40 private final MemoryUsage usage; |
|
41 |
|
42 private MemoryUsageCompositeData(MemoryUsage u) { |
|
43 this.usage = u; |
|
44 } |
|
45 |
|
46 public MemoryUsage getMemoryUsage() { |
|
47 return usage; |
|
48 } |
|
49 |
|
50 public static CompositeData toCompositeData(MemoryUsage u) { |
|
51 MemoryUsageCompositeData mucd = new MemoryUsageCompositeData(u); |
|
52 return mucd.getCompositeData(); |
|
53 } |
|
54 |
|
55 protected CompositeData getCompositeData() { |
|
56 // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH |
|
57 // memoryUsageItemNames! |
|
58 final Object[] memoryUsageItemValues = { |
|
59 usage.getInit(), |
|
60 usage.getUsed(), |
|
61 usage.getCommitted(), |
|
62 usage.getMax(), |
|
63 }; |
|
64 |
|
65 try { |
|
66 return new CompositeDataSupport(memoryUsageCompositeType, |
|
67 memoryUsageItemNames, |
|
68 memoryUsageItemValues); |
|
69 } catch (OpenDataException e) { |
|
70 // Should never reach here |
|
71 throw new AssertionError(e); |
|
72 } |
|
73 } |
|
74 |
|
75 private static final CompositeType memoryUsageCompositeType; |
|
76 static { |
|
77 try { |
|
78 memoryUsageCompositeType = (CompositeType) |
|
79 MappedMXBeanType.toOpenType(MemoryUsage.class); |
|
80 } catch (OpenDataException e) { |
|
81 // Should never reach here |
|
82 throw new AssertionError(e); |
|
83 } |
|
84 } |
|
85 |
|
86 static CompositeType getMemoryUsageCompositeType() { |
|
87 return memoryUsageCompositeType; |
|
88 } |
|
89 |
|
90 private static final String INIT = "init"; |
|
91 private static final String USED = "used"; |
|
92 private static final String COMMITTED = "committed"; |
|
93 private static final String MAX = "max"; |
|
94 |
|
95 private static final String[] memoryUsageItemNames = { |
|
96 INIT, |
|
97 USED, |
|
98 COMMITTED, |
|
99 MAX, |
|
100 }; |
|
101 |
|
102 public static long getInit(CompositeData cd) { |
|
103 return getLong(cd, INIT); |
|
104 } |
|
105 public static long getUsed(CompositeData cd) { |
|
106 return getLong(cd, USED); |
|
107 } |
|
108 public static long getCommitted(CompositeData cd) { |
|
109 return getLong(cd, COMMITTED); |
|
110 } |
|
111 public static long getMax(CompositeData cd) { |
|
112 return getLong(cd, MAX); |
|
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(memoryUsageCompositeType, cd.getCompositeType())) { |
|
125 throw new IllegalArgumentException( |
|
126 "Unexpected composite type for MemoryUsage"); |
|
127 } |
|
128 } |
|
129 |
|
130 private static final long serialVersionUID = -8504291541083874143L; |
|
131 } |