History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/management/GarbageCollectionNotifInfoCompositeData.java (1 of 18352 files). Show all

Unified · Split

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