|
1 /* |
|
2 * Copyright (c) 2011, 2015, 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 com.sun.management.internal; |
|
27 |
|
28 import com.sun.management.GarbageCollectionNotificationInfo; |
|
29 import com.sun.management.GcInfo; |
|
30 import javax.management.openmbean.CompositeData; |
|
31 import javax.management.openmbean.CompositeType; |
|
32 import javax.management.openmbean.CompositeDataSupport; |
|
33 import javax.management.openmbean.OpenDataException; |
|
34 import javax.management.openmbean.OpenType; |
|
35 import javax.management.openmbean.SimpleType; |
|
36 import java.security.AccessController; |
|
37 import java.security.PrivilegedAction; |
|
38 import java.lang.reflect.Field; |
|
39 import java.util.HashMap; |
|
40 import sun.management.LazyCompositeData; |
|
41 import static sun.management.LazyCompositeData.getString; |
|
42 import sun.management.Util; |
|
43 |
|
44 /** |
|
45 * A CompositeData for GarbageCollectionNotificationInfo for the local management support. |
|
46 * This class avoids the performance penalty paid to the |
|
47 * construction of a CompositeData use in the local case. |
|
48 */ |
|
49 public class GarbageCollectionNotifInfoCompositeData extends LazyCompositeData { |
|
50 private final GarbageCollectionNotificationInfo gcNotifInfo; |
|
51 |
|
52 public GarbageCollectionNotifInfoCompositeData(GarbageCollectionNotificationInfo info) { |
|
53 this.gcNotifInfo = info; |
|
54 } |
|
55 |
|
56 public GarbageCollectionNotificationInfo getGarbageCollectionNotifInfo() { |
|
57 return gcNotifInfo; |
|
58 } |
|
59 |
|
60 public static CompositeData toCompositeData(GarbageCollectionNotificationInfo info) { |
|
61 GarbageCollectionNotifInfoCompositeData gcnicd = |
|
62 new GarbageCollectionNotifInfoCompositeData(info); |
|
63 return gcnicd.getCompositeData(); |
|
64 } |
|
65 |
|
66 private CompositeType getCompositeTypeByBuilder() { |
|
67 final GcInfoBuilder builder = AccessController.doPrivileged (new PrivilegedAction<GcInfoBuilder>() { |
|
68 public GcInfoBuilder run() { |
|
69 try { |
|
70 Class<?> cl = Class.forName("com.sun.management.GcInfo"); |
|
71 Field f = cl.getDeclaredField("builder"); |
|
72 f.setAccessible(true); |
|
73 return (GcInfoBuilder)f.get(gcNotifInfo.getGcInfo()); |
|
74 } catch(ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { |
|
75 return null; |
|
76 } |
|
77 } |
|
78 }); |
|
79 CompositeType gict = null; |
|
80 synchronized(compositeTypeByBuilder) { |
|
81 gict = compositeTypeByBuilder.get(builder); |
|
82 if(gict == null) { |
|
83 OpenType<?>[] gcNotifInfoItemTypes = new OpenType<?>[] { |
|
84 SimpleType.STRING, |
|
85 SimpleType.STRING, |
|
86 SimpleType.STRING, |
|
87 builder.getGcInfoCompositeType(), |
|
88 }; |
|
89 try { |
|
90 final String typeName = |
|
91 "sun.management.GarbageCollectionNotifInfoCompositeType"; |
|
92 gict = new CompositeType(typeName, |
|
93 "CompositeType for GC notification info", |
|
94 gcNotifInfoItemNames, |
|
95 gcNotifInfoItemNames, |
|
96 gcNotifInfoItemTypes); |
|
97 compositeTypeByBuilder.put(builder,gict); |
|
98 } catch (OpenDataException e) { |
|
99 // shouldn't reach here |
|
100 throw new RuntimeException(e); |
|
101 } |
|
102 } |
|
103 } |
|
104 return gict; |
|
105 } |
|
106 |
|
107 protected CompositeData getCompositeData() { |
|
108 // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH |
|
109 // gcNotifInfoItemNames! |
|
110 final Object[] gcNotifInfoItemValues; |
|
111 gcNotifInfoItemValues = new Object[] { |
|
112 gcNotifInfo.getGcName(), |
|
113 gcNotifInfo.getGcAction(), |
|
114 gcNotifInfo.getGcCause(), |
|
115 GcInfoCompositeData.toCompositeData(gcNotifInfo.getGcInfo()) |
|
116 }; |
|
117 |
|
118 CompositeType gict = getCompositeTypeByBuilder(); |
|
119 |
|
120 try { |
|
121 return new CompositeDataSupport(gict, |
|
122 gcNotifInfoItemNames, |
|
123 gcNotifInfoItemValues); |
|
124 } catch (OpenDataException e) { |
|
125 // Should never reach here |
|
126 throw new AssertionError(e); |
|
127 } |
|
128 } |
|
129 |
|
130 // private static MappedMXBeanType gcInfoMapType; |
|
131 private static final String GC_NAME = "gcName"; |
|
132 private static final String GC_ACTION = "gcAction"; |
|
133 private static final String GC_CAUSE = "gcCause"; |
|
134 private static final String GC_INFO = "gcInfo"; |
|
135 private static final String[] gcNotifInfoItemNames = { |
|
136 GC_NAME, |
|
137 GC_ACTION, |
|
138 GC_CAUSE, |
|
139 GC_INFO |
|
140 }; |
|
141 private static HashMap<GcInfoBuilder,CompositeType> compositeTypeByBuilder = |
|
142 new HashMap<>(); |
|
143 |
|
144 public static String getGcName(CompositeData cd) { |
|
145 String gcname = getString(cd, GC_NAME); |
|
146 if (gcname == null) { |
|
147 throw new IllegalArgumentException("Invalid composite data: " + |
|
148 "Attribute " + GC_NAME + " has null value"); |
|
149 } |
|
150 return gcname; |
|
151 } |
|
152 |
|
153 public static String getGcAction(CompositeData cd) { |
|
154 String gcaction = getString(cd, GC_ACTION); |
|
155 if (gcaction == null) { |
|
156 throw new IllegalArgumentException("Invalid composite data: " + |
|
157 "Attribute " + GC_ACTION + " has null value"); |
|
158 } |
|
159 return gcaction; |
|
160 } |
|
161 |
|
162 public static String getGcCause(CompositeData cd) { |
|
163 String gccause = getString(cd, GC_CAUSE); |
|
164 if (gccause == null) { |
|
165 throw new IllegalArgumentException("Invalid composite data: " + |
|
166 "Attribute " + GC_CAUSE + " has null value"); |
|
167 } |
|
168 return gccause; |
|
169 } |
|
170 |
|
171 public static GcInfo getGcInfo(CompositeData cd) { |
|
172 CompositeData gcInfoData = (CompositeData) cd.get(GC_INFO); |
|
173 return GcInfo.from(gcInfoData); |
|
174 } |
|
175 |
|
176 /** Validate if the input CompositeData has the expected |
|
177 * CompositeType (i.e. contain all attributes with expected |
|
178 * names and types). |
|
179 */ |
|
180 public static void validateCompositeData(CompositeData cd) { |
|
181 if (cd == null) { |
|
182 throw new NullPointerException("Null CompositeData"); |
|
183 } |
|
184 |
|
185 if (!isTypeMatched( getBaseGcNotifInfoCompositeType(), cd.getCompositeType())) { |
|
186 throw new IllegalArgumentException( |
|
187 "Unexpected composite type for GarbageCollectionNotificationInfo"); |
|
188 } |
|
189 } |
|
190 |
|
191 // This is only used for validation. |
|
192 private static CompositeType baseGcNotifInfoCompositeType = null; |
|
193 private static synchronized CompositeType getBaseGcNotifInfoCompositeType() { |
|
194 if (baseGcNotifInfoCompositeType == null) { |
|
195 try { |
|
196 OpenType<?>[] baseGcNotifInfoItemTypes = new OpenType<?>[] { |
|
197 SimpleType.STRING, |
|
198 SimpleType.STRING, |
|
199 SimpleType.STRING, |
|
200 GcInfoCompositeData.getBaseGcInfoCompositeType() |
|
201 }; |
|
202 baseGcNotifInfoCompositeType = |
|
203 new CompositeType("sun.management.BaseGarbageCollectionNotifInfoCompositeType", |
|
204 "CompositeType for Base GarbageCollectionNotificationInfo", |
|
205 gcNotifInfoItemNames, |
|
206 gcNotifInfoItemNames, |
|
207 baseGcNotifInfoItemTypes); |
|
208 } catch (OpenDataException e) { |
|
209 // shouldn't reach here |
|
210 throw new RuntimeException(e); |
|
211 } |
|
212 } |
|
213 return baseGcNotifInfoCompositeType; |
|
214 } |
|
215 |
|
216 private static final long serialVersionUID = -1805123446483771292L; |
|
217 } |