Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/GarbageCollectionNotifInfoCompositeData.java
38827 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import com.sun.management.GarbageCollectionNotificationInfo;28import com.sun.management.GcInfo;29import java.lang.reflect.Method;30import javax.management.openmbean.CompositeData;31import javax.management.openmbean.CompositeType;32import javax.management.openmbean.CompositeDataSupport;33import javax.management.openmbean.OpenDataException;34import javax.management.openmbean.OpenType;35import javax.management.openmbean.SimpleType;36import java.security.AccessController;37import java.security.PrivilegedAction;38import java.lang.reflect.Field;39import java.util.HashMap;4041/**42* A CompositeData for GarbageCollectionNotificationInfo for the local management support.43* This class avoids the performance penalty paid to the44* construction of a CompositeData use in the local case.45*/46public class GarbageCollectionNotifInfoCompositeData extends LazyCompositeData {47private final GarbageCollectionNotificationInfo gcNotifInfo;4849public GarbageCollectionNotifInfoCompositeData(GarbageCollectionNotificationInfo info) {50this.gcNotifInfo = info;51}5253public GarbageCollectionNotificationInfo getGarbageCollectionNotifInfo() {54return gcNotifInfo;55}5657public static CompositeData toCompositeData(GarbageCollectionNotificationInfo info) {58GarbageCollectionNotifInfoCompositeData gcnicd =59new GarbageCollectionNotifInfoCompositeData(info);60return gcnicd.getCompositeData();61}6263private CompositeType getCompositeTypeByBuilder() {64final GcInfoBuilder builder = AccessController.doPrivileged (new PrivilegedAction<GcInfoBuilder>() {65public GcInfoBuilder run() {66try {67Class cl = Class.forName("com.sun.management.GcInfo");68Field f = cl.getDeclaredField("builder");69f.setAccessible(true);70return (GcInfoBuilder)f.get(gcNotifInfo.getGcInfo());71} catch(ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {72return null;73}74}75});76CompositeType gict = null;77synchronized(compositeTypeByBuilder) {78gict = compositeTypeByBuilder.get(builder);79if(gict == null) {80OpenType<?>[] gcNotifInfoItemTypes = new OpenType<?>[] {81SimpleType.STRING,82SimpleType.STRING,83SimpleType.STRING,84builder.getGcInfoCompositeType(),85};86try {87final String typeName =88"sun.management.GarbageCollectionNotifInfoCompositeType";89gict = new CompositeType(typeName,90"CompositeType for GC notification info",91gcNotifInfoItemNames,92gcNotifInfoItemNames,93gcNotifInfoItemTypes);94compositeTypeByBuilder.put(builder,gict);95} catch (OpenDataException e) {96// shouldn't reach here97throw Util.newException(e);98}99}100}101return gict;102}103104protected CompositeData getCompositeData() {105// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH106// gcNotifInfoItemNames!107final Object[] gcNotifInfoItemValues;108gcNotifInfoItemValues = new Object[] {109gcNotifInfo.getGcName(),110gcNotifInfo.getGcAction(),111gcNotifInfo.getGcCause(),112GcInfoCompositeData.toCompositeData(gcNotifInfo.getGcInfo())113};114115CompositeType gict = getCompositeTypeByBuilder();116117try {118return new CompositeDataSupport(gict,119gcNotifInfoItemNames,120gcNotifInfoItemValues);121} catch (OpenDataException e) {122// Should never reach here123throw new AssertionError(e);124}125}126127// private static MappedMXBeanType gcInfoMapType;128private static final String GC_NAME = "gcName";129private static final String GC_ACTION = "gcAction";130private static final String GC_CAUSE = "gcCause";131private static final String GC_INFO = "gcInfo";132private static final String[] gcNotifInfoItemNames = {133GC_NAME,134GC_ACTION,135GC_CAUSE,136GC_INFO137};138private static HashMap<GcInfoBuilder,CompositeType> compositeTypeByBuilder =139new HashMap<>();140141public static String getGcName(CompositeData cd) {142String gcname = getString(cd, GC_NAME);143if (gcname == null) {144throw new IllegalArgumentException("Invalid composite data: " +145"Attribute " + GC_NAME + " has null value");146}147return gcname;148}149150public static String getGcAction(CompositeData cd) {151String gcaction = getString(cd, GC_ACTION);152if (gcaction == null) {153throw new IllegalArgumentException("Invalid composite data: " +154"Attribute " + GC_ACTION + " has null value");155}156return gcaction;157}158159public static String getGcCause(CompositeData cd) {160String gccause = getString(cd, GC_CAUSE);161if (gccause == null) {162throw new IllegalArgumentException("Invalid composite data: " +163"Attribute " + GC_CAUSE + " has null value");164}165return gccause;166}167168public static GcInfo getGcInfo(CompositeData cd) {169CompositeData gcInfoData = (CompositeData) cd.get(GC_INFO);170return GcInfo.from(gcInfoData);171}172173/** Validate if the input CompositeData has the expected174* CompositeType (i.e. contain all attributes with expected175* names and types).176*/177public static void validateCompositeData(CompositeData cd) {178if (cd == null) {179throw new NullPointerException("Null CompositeData");180}181182if (!isTypeMatched( getBaseGcNotifInfoCompositeType(), cd.getCompositeType())) {183throw new IllegalArgumentException(184"Unexpected composite type for GarbageCollectionNotificationInfo");185}186}187188// This is only used for validation.189private static CompositeType baseGcNotifInfoCompositeType = null;190private static synchronized CompositeType getBaseGcNotifInfoCompositeType() {191if (baseGcNotifInfoCompositeType == null) {192try {193OpenType<?>[] baseGcNotifInfoItemTypes = new OpenType<?>[] {194SimpleType.STRING,195SimpleType.STRING,196SimpleType.STRING,197GcInfoCompositeData.getBaseGcInfoCompositeType()198};199baseGcNotifInfoCompositeType =200new CompositeType("sun.management.BaseGarbageCollectionNotifInfoCompositeType",201"CompositeType for Base GarbageCollectionNotificationInfo",202gcNotifInfoItemNames,203gcNotifInfoItemNames,204baseGcNotifInfoItemTypes);205} catch (OpenDataException e) {206// shouldn't reach here207throw Util.newException(e);208}209}210return baseGcNotifInfoCompositeType;211}212213private static final long serialVersionUID = -1805123446483771292L;214}215216217