Path: blob/master/src/java.management/share/classes/sun/management/MonitorInfoCompositeData.java
41152 views
/*1* Copyright (c) 2005, 2019, 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 java.lang.management.MonitorInfo;28import java.util.HashMap;29import java.util.Map;30import javax.management.openmbean.CompositeType;31import javax.management.openmbean.CompositeData;32import javax.management.openmbean.CompositeDataSupport;33import javax.management.openmbean.OpenDataException;34import javax.management.openmbean.OpenType;3536/**37* A CompositeData for MonitorInfo for the local management support.38* This class avoids the performance penalty paid to the39* construction of a CompositeData use in the local case.40*/41public class MonitorInfoCompositeData extends LazyCompositeData {42@SuppressWarnings("serial") // Not statically typed as Serializable43private final MonitorInfo lock;4445private MonitorInfoCompositeData(MonitorInfo mi) {46this.lock = mi;47}4849public MonitorInfo getMonitorInfo() {50return lock;51}5253public static CompositeData toCompositeData(MonitorInfo mi) {54MonitorInfoCompositeData micd = new MonitorInfoCompositeData(mi);55return micd.getCompositeData();56}5758protected CompositeData getCompositeData() {59StackTraceElement ste = lock.getLockedStackFrame();60CompositeData steCData = ste != null ? StackTraceElementCompositeData.toCompositeData(ste)61: null;62// values may be null; can't use Map.of63Map<String,Object> items = new HashMap<>();64items.put(CLASS_NAME, lock.getClassName());65items.put(IDENTITY_HASH_CODE, lock.getIdentityHashCode());66items.put(LOCKED_STACK_FRAME, steCData);67items.put(LOCKED_STACK_DEPTH, lock.getLockedStackDepth());6869try {70return new CompositeDataSupport(MONITOR_INFO_COMPOSITE_TYPE, items);71} catch (OpenDataException e) {72// Should never reach here73throw new AssertionError(e);74}75}7677private static final String CLASS_NAME = "className";78private static final String IDENTITY_HASH_CODE = "identityHashCode";79private static final String LOCKED_STACK_FRAME = "lockedStackFrame";80private static final String LOCKED_STACK_DEPTH = "lockedStackDepth";8182private static final String[] MONITOR_INFO_ATTRIBUTES = {83CLASS_NAME,84IDENTITY_HASH_CODE,85LOCKED_STACK_FRAME,86LOCKED_STACK_DEPTH87};8889private static final CompositeType MONITOR_INFO_COMPOSITE_TYPE;90private static final CompositeType V6_COMPOSITE_TYPE;91static {92try {93MONITOR_INFO_COMPOSITE_TYPE = (CompositeType)94MappedMXBeanType.toOpenType(MonitorInfo.class);9596OpenType<?>[] types = new OpenType<?>[MONITOR_INFO_ATTRIBUTES.length];97for (int i = 0; i < MONITOR_INFO_ATTRIBUTES.length; i++) {98String name = MONITOR_INFO_ATTRIBUTES[i];99types[i] = name.equals(LOCKED_STACK_FRAME)100? StackTraceElementCompositeData.v5CompositeType()101: MONITOR_INFO_COMPOSITE_TYPE.getType(name);102}103V6_COMPOSITE_TYPE = new CompositeType("MonitorInfo",104"JDK 6 MonitorInfo",105MONITOR_INFO_ATTRIBUTES,106MONITOR_INFO_ATTRIBUTES,107types);108} catch (OpenDataException e) {109// Should never reach here110throw new AssertionError(e);111}112}113114static CompositeType v6CompositeType() {115return V6_COMPOSITE_TYPE;116}117118public static String getClassName(CompositeData cd) {119return getString(cd, CLASS_NAME);120}121122public static int getIdentityHashCode(CompositeData cd) {123return getInt(cd, IDENTITY_HASH_CODE);124}125126public static StackTraceElement getLockedStackFrame(CompositeData cd) {127CompositeData ste = (CompositeData) cd.get(LOCKED_STACK_FRAME);128if (ste != null) {129return StackTraceElementCompositeData.from(ste);130} else {131return null;132}133}134135public static int getLockedStackDepth(CompositeData cd) {136return getInt(cd, LOCKED_STACK_DEPTH);137}138139/** Validate if the input CompositeData has the expected140* CompositeType (i.e. contain all attributes with expected141* names and types).142*/143public static void validateCompositeData(CompositeData cd) {144if (cd == null) {145throw new NullPointerException("Null CompositeData");146}147148if (!isTypeMatched(MONITOR_INFO_COMPOSITE_TYPE, cd.getCompositeType()) &&149!isTypeMatched(V6_COMPOSITE_TYPE, cd.getCompositeType())) {150throw new IllegalArgumentException(151"Unexpected composite type for MonitorInfo");152}153}154155private static final long serialVersionUID = -5825215591822908529L;156}157158159