0N/A/*
2362N/A * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.management;
0N/A
0N/Aimport java.lang.management.ThreadInfo;
0N/Aimport java.lang.management.MonitorInfo;
0N/Aimport java.lang.management.LockInfo;
0N/Aimport javax.management.openmbean.CompositeType;
0N/Aimport javax.management.openmbean.CompositeData;
0N/Aimport javax.management.openmbean.CompositeDataSupport;
0N/Aimport javax.management.openmbean.OpenDataException;
0N/Aimport javax.management.openmbean.OpenType;
0N/A
0N/A/**
0N/A * A CompositeData for ThreadInfo for the local management support.
0N/A * This class avoids the performance penalty paid to the
0N/A * construction of a CompositeData use in the local case.
0N/A */
0N/Apublic class ThreadInfoCompositeData extends LazyCompositeData {
0N/A private final ThreadInfo threadInfo;
0N/A private final CompositeData cdata;
0N/A private final boolean currentVersion;
0N/A
0N/A private ThreadInfoCompositeData(ThreadInfo ti) {
0N/A this.threadInfo = ti;
0N/A this.currentVersion = true;
0N/A this.cdata = null;
0N/A }
0N/A
0N/A private ThreadInfoCompositeData(CompositeData cd) {
0N/A this.threadInfo = null;
0N/A this.currentVersion = ThreadInfoCompositeData.isCurrentVersion(cd);
0N/A this.cdata = cd;
0N/A }
0N/A
0N/A public ThreadInfo getThreadInfo() {
0N/A return threadInfo;
0N/A }
0N/A
0N/A public boolean isCurrentVersion() {
0N/A return currentVersion;
0N/A }
0N/A
0N/A public static ThreadInfoCompositeData getInstance(CompositeData cd) {
0N/A validateCompositeData(cd);
0N/A return new ThreadInfoCompositeData(cd);
0N/A }
0N/A
0N/A public static CompositeData toCompositeData(ThreadInfo ti) {
0N/A ThreadInfoCompositeData ticd = new ThreadInfoCompositeData(ti);
0N/A return ticd.getCompositeData();
0N/A }
0N/A
0N/A protected CompositeData getCompositeData() {
0N/A // Convert StackTraceElement[] to CompositeData[]
0N/A StackTraceElement[] stackTrace = threadInfo.getStackTrace();
0N/A CompositeData[] stackTraceData =
0N/A new CompositeData[stackTrace.length];
0N/A for (int i = 0; i < stackTrace.length; i++) {
0N/A StackTraceElement ste = stackTrace[i];
0N/A stackTraceData[i] = StackTraceElementCompositeData.toCompositeData(ste);
0N/A }
0N/A
0N/A // Convert MonitorInfo[] and LockInfo[] to CompositeData[]
5789N/A LockDataConverter converter = LockDataConverter.newLockDataConverter(threadInfo);
0N/A CompositeData lockInfoData = converter.toLockInfoCompositeData();
0N/A CompositeData[] lockedSyncsData = converter.toLockedSynchronizersCompositeData();
0N/A
0N/A // Convert MonitorInfo[] to CompositeData[]
0N/A MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
0N/A CompositeData[] lockedMonitorsData =
0N/A new CompositeData[lockedMonitors.length];
0N/A for (int i = 0; i < lockedMonitors.length; i++) {
0N/A MonitorInfo mi = lockedMonitors[i];
0N/A lockedMonitorsData[i] = MonitorInfoCompositeData.toCompositeData(mi);
0N/A }
0N/A
0N/A
0N/A // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
0N/A // threadInfoItemNames!
0N/A final Object[] threadInfoItemValues = {
0N/A new Long(threadInfo.getThreadId()),
0N/A threadInfo.getThreadName(),
0N/A threadInfo.getThreadState().name(),
0N/A new Long(threadInfo.getBlockedTime()),
0N/A new Long(threadInfo.getBlockedCount()),
0N/A new Long(threadInfo.getWaitedTime()),
0N/A new Long(threadInfo.getWaitedCount()),
0N/A lockInfoData,
0N/A threadInfo.getLockName(),
0N/A new Long(threadInfo.getLockOwnerId()),
0N/A threadInfo.getLockOwnerName(),
0N/A stackTraceData,
0N/A new Boolean(threadInfo.isSuspended()),
0N/A new Boolean(threadInfo.isInNative()),
0N/A lockedMonitorsData,
0N/A lockedSyncsData,
0N/A };
0N/A
0N/A try {
0N/A return new CompositeDataSupport(threadInfoCompositeType,
0N/A threadInfoItemNames,
0N/A threadInfoItemValues);
0N/A } catch (OpenDataException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A }
0N/A }
0N/A
0N/A // Attribute names
0N/A private static final String THREAD_ID = "threadId";
0N/A private static final String THREAD_NAME = "threadName";
0N/A private static final String THREAD_STATE = "threadState";
0N/A private static final String BLOCKED_TIME = "blockedTime";
0N/A private static final String BLOCKED_COUNT = "blockedCount";
0N/A private static final String WAITED_TIME = "waitedTime";
0N/A private static final String WAITED_COUNT = "waitedCount";
0N/A private static final String LOCK_INFO = "lockInfo";
0N/A private static final String LOCK_NAME = "lockName";
0N/A private static final String LOCK_OWNER_ID = "lockOwnerId";
0N/A private static final String LOCK_OWNER_NAME = "lockOwnerName";
0N/A private static final String STACK_TRACE = "stackTrace";
0N/A private static final String SUSPENDED = "suspended";
0N/A private static final String IN_NATIVE = "inNative";
0N/A private static final String LOCKED_MONITORS = "lockedMonitors";
0N/A private static final String LOCKED_SYNCS = "lockedSynchronizers";
0N/A
0N/A private static final String[] threadInfoItemNames = {
0N/A THREAD_ID,
0N/A THREAD_NAME,
0N/A THREAD_STATE,
0N/A BLOCKED_TIME,
0N/A BLOCKED_COUNT,
0N/A WAITED_TIME,
0N/A WAITED_COUNT,
0N/A LOCK_INFO,
0N/A LOCK_NAME,
0N/A LOCK_OWNER_ID,
0N/A LOCK_OWNER_NAME,
0N/A STACK_TRACE,
0N/A SUSPENDED,
0N/A IN_NATIVE,
0N/A LOCKED_MONITORS,
0N/A LOCKED_SYNCS,
0N/A };
0N/A
0N/A // New attributes added in 6.0 ThreadInfo
0N/A private static final String[] threadInfoV6Attributes = {
0N/A LOCK_INFO,
0N/A LOCKED_MONITORS,
0N/A LOCKED_SYNCS,
0N/A };
0N/A
0N/A // Current version of ThreadInfo
0N/A private static final CompositeType threadInfoCompositeType;
0N/A // Previous version of ThreadInfo
0N/A private static final CompositeType threadInfoV5CompositeType;
0N/A private static final CompositeType lockInfoCompositeType;
0N/A static {
0N/A try {
0N/A threadInfoCompositeType = (CompositeType)
0N/A MappedMXBeanType.toOpenType(ThreadInfo.class);
0N/A // Form a CompositeType for JDK 5.0 ThreadInfo version
0N/A String[] itemNames =
0N/A threadInfoCompositeType.keySet().toArray(new String[0]);
0N/A int numV5Attributes = threadInfoItemNames.length -
0N/A threadInfoV6Attributes.length;
0N/A String[] v5ItemNames = new String[numV5Attributes];
0N/A String[] v5ItemDescs = new String[numV5Attributes];
0N/A OpenType[] v5ItemTypes = new OpenType[numV5Attributes];
0N/A int i = 0;
0N/A for (String n : itemNames) {
0N/A if (isV5Attribute(n)) {
0N/A v5ItemNames[i] = n;
0N/A v5ItemDescs[i] = threadInfoCompositeType.getDescription(n);
0N/A v5ItemTypes[i] = threadInfoCompositeType.getType(n);
0N/A i++;
0N/A }
0N/A }
0N/A
0N/A threadInfoV5CompositeType =
0N/A new CompositeType("java.lang.management.ThreadInfo",
0N/A "J2SE 5.0 java.lang.management.ThreadInfo",
0N/A v5ItemNames,
0N/A v5ItemDescs,
0N/A v5ItemTypes);
0N/A } catch (OpenDataException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A }
0N/A
0N/A // Each CompositeData object has its CompositeType associated
0N/A // with it. So we can get the CompositeType representing LockInfo
0N/A // from a mapped CompositeData for any LockInfo object.
0N/A // Thus we construct a random LockInfo object and pass it
0N/A // to LockDataConverter to do the conversion.
0N/A Object o = new Object();
0N/A LockInfo li = new LockInfo(o.getClass().getName(),
0N/A System.identityHashCode(o));
0N/A CompositeData cd = LockDataConverter.toLockInfoCompositeData(li);
0N/A lockInfoCompositeType = cd.getCompositeType();
0N/A }
0N/A
0N/A private static boolean isV5Attribute(String itemName) {
0N/A for (String n : threadInfoV6Attributes) {
0N/A if (itemName.equals(n)) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A public static boolean isCurrentVersion(CompositeData cd) {
0N/A if (cd == null) {
0N/A throw new NullPointerException("Null CompositeData");
0N/A }
0N/A
0N/A return isTypeMatched(threadInfoCompositeType, cd.getCompositeType());
0N/A }
0N/A
0N/A public long threadId() {
0N/A return getLong(cdata, THREAD_ID);
0N/A }
0N/A
0N/A public String threadName() {
0N/A // The ThreadName item cannot be null so we check that
0N/A // it is present with a non-null value.
0N/A String name = getString(cdata, THREAD_NAME);
0N/A if (name == null) {
0N/A throw new IllegalArgumentException("Invalid composite data: " +
0N/A "Attribute " + THREAD_NAME + " has null value");
0N/A }
0N/A return name;
0N/A }
0N/A
0N/A public Thread.State threadState() {
0N/A return Thread.State.valueOf(getString(cdata, THREAD_STATE));
0N/A }
0N/A
0N/A public long blockedTime() {
0N/A return getLong(cdata, BLOCKED_TIME);
0N/A }
0N/A
0N/A public long blockedCount() {
0N/A return getLong(cdata, BLOCKED_COUNT);
0N/A }
0N/A
0N/A public long waitedTime() {
0N/A return getLong(cdata, WAITED_TIME);
0N/A }
0N/A
0N/A public long waitedCount() {
0N/A return getLong(cdata, WAITED_COUNT);
0N/A }
0N/A
0N/A public String lockName() {
0N/A // The LockName and LockOwnerName can legitimately be null,
0N/A // we don't bother to check the value
0N/A return getString(cdata, LOCK_NAME);
0N/A }
0N/A
0N/A public long lockOwnerId() {
0N/A return getLong(cdata, LOCK_OWNER_ID);
0N/A }
0N/A
0N/A public String lockOwnerName() {
0N/A return getString(cdata, LOCK_OWNER_NAME);
0N/A }
0N/A
0N/A public boolean suspended() {
0N/A return getBoolean(cdata, SUSPENDED);
0N/A }
0N/A
0N/A public boolean inNative() {
0N/A return getBoolean(cdata, IN_NATIVE);
0N/A }
0N/A
0N/A public StackTraceElement[] stackTrace() {
0N/A CompositeData[] stackTraceData =
0N/A (CompositeData[]) cdata.get(STACK_TRACE);
0N/A
0N/A // The StackTrace item cannot be null, but if it is we will get
0N/A // a NullPointerException when we ask for its length.
0N/A StackTraceElement[] stackTrace =
0N/A new StackTraceElement[stackTraceData.length];
0N/A for (int i = 0; i < stackTraceData.length; i++) {
0N/A CompositeData cdi = stackTraceData[i];
0N/A stackTrace[i] = StackTraceElementCompositeData.from(cdi);
0N/A }
0N/A return stackTrace;
0N/A }
0N/A
0N/A // 6.0 new attributes
0N/A public LockInfo lockInfo() {
5789N/A LockDataConverter converter = LockDataConverter.newLockDataConverter();
0N/A CompositeData lockInfoData = (CompositeData) cdata.get(LOCK_INFO);
0N/A return converter.toLockInfo(lockInfoData);
0N/A }
0N/A
0N/A public MonitorInfo[] lockedMonitors() {
0N/A CompositeData[] lockedMonitorsData =
0N/A (CompositeData[]) cdata.get(LOCKED_MONITORS);
0N/A
0N/A // The LockedMonitors item cannot be null, but if it is we will get
0N/A // a NullPointerException when we ask for its length.
0N/A MonitorInfo[] monitors =
0N/A new MonitorInfo[lockedMonitorsData.length];
0N/A for (int i = 0; i < lockedMonitorsData.length; i++) {
0N/A CompositeData cdi = lockedMonitorsData[i];
0N/A monitors[i] = MonitorInfo.from(cdi);
0N/A }
0N/A return monitors;
0N/A }
0N/A
0N/A public LockInfo[] lockedSynchronizers() {
5789N/A LockDataConverter converter = LockDataConverter.newLockDataConverter();
0N/A CompositeData[] lockedSyncsData =
0N/A (CompositeData[]) cdata.get(LOCKED_SYNCS);
0N/A
0N/A // The LockedSynchronizers item cannot be null, but if it is we will
0N/A // get a NullPointerException when we ask for its length.
0N/A return converter.toLockedSynchronizers(lockedSyncsData);
0N/A }
0N/A
0N/A /** Validate if the input CompositeData has the expected
0N/A * CompositeType (i.e. contain all attributes with expected
0N/A * names and types).
0N/A */
0N/A public static void validateCompositeData(CompositeData cd) {
0N/A if (cd == null) {
0N/A throw new NullPointerException("Null CompositeData");
0N/A }
0N/A
0N/A CompositeType type = cd.getCompositeType();
0N/A boolean currentVersion = true;
0N/A if (!isTypeMatched(threadInfoCompositeType, type)) {
0N/A currentVersion = false;
0N/A // check if cd is an older version
0N/A if (!isTypeMatched(threadInfoV5CompositeType, type)) {
0N/A throw new IllegalArgumentException(
0N/A "Unexpected composite type for ThreadInfo");
0N/A }
0N/A }
0N/A
0N/A CompositeData[] stackTraceData =
0N/A (CompositeData[]) cd.get(STACK_TRACE);
0N/A if (stackTraceData == null) {
0N/A throw new IllegalArgumentException(
0N/A "StackTraceElement[] is missing");
0N/A }
0N/A if (stackTraceData.length > 0) {
0N/A StackTraceElementCompositeData.validateCompositeData(stackTraceData[0]);
0N/A }
0N/A
0N/A // validate v6 attributes
0N/A if (currentVersion) {
0N/A CompositeData li = (CompositeData) cd.get(LOCK_INFO);
0N/A if (li != null) {
0N/A if (!isTypeMatched(lockInfoCompositeType,
0N/A li.getCompositeType())) {
0N/A throw new IllegalArgumentException(
0N/A "Unexpected composite type for \"" +
0N/A LOCK_INFO + "\" attribute.");
0N/A }
0N/A }
0N/A
0N/A CompositeData[] lms = (CompositeData[]) cd.get(LOCKED_MONITORS);
0N/A if (lms == null) {
0N/A throw new IllegalArgumentException("MonitorInfo[] is null");
0N/A }
0N/A if (lms.length > 0) {
0N/A MonitorInfoCompositeData.validateCompositeData(lms[0]);
0N/A }
0N/A
0N/A CompositeData[] lsyncs = (CompositeData[]) cd.get(LOCKED_SYNCS);
0N/A if (lsyncs == null) {
0N/A throw new IllegalArgumentException("LockInfo[] is null");
0N/A }
0N/A if (lsyncs.length > 0) {
0N/A if (!isTypeMatched(lockInfoCompositeType,
0N/A lsyncs[0].getCompositeType())) {
0N/A throw new IllegalArgumentException(
0N/A "Unexpected composite type for \"" +
0N/A LOCKED_SYNCS + "\" attribute.");
0N/A }
0N/A }
0N/A
0N/A }
0N/A }
1807N/A
1807N/A private static final long serialVersionUID = 2464378539119753175L;
0N/A}