001 /*
002 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.lang.management;
027
028 import javax.management.openmbean.CompositeData;
029 import sun.management.MonitorInfoCompositeData;
030
031 /**
032 * Information about an object monitor lock. An object monitor is locked
033 * when entering a synchronization block or method on that object.
034 *
035 * <h4>MXBean Mapping</h4>
036 * <tt>MonitorInfo</tt> is mapped to a {@link CompositeData CompositeData}
037 * with attributes as specified in
038 * the {@link #from from} method.
039 *
040 * @author Mandy Chung
041 * @version 1.11, 05/05/07
042 * @since 1.6
043 */
044 public class MonitorInfo extends LockInfo {
045
046 private int stackDepth;
047 private StackTraceElement stackFrame;
048
049 /**
050 * Construct a <tt>MonitorInfo</tt> object.
051 *
052 * @param className the fully qualified name of the class of the lock object.
053 * @param identityHashCode the {@link System#identityHashCode
054 * identity hash code} of the lock object.
055 * @param stackDepth the depth in the stack trace where the object monitor
056 * was locked.
057 * @param stackFrame the stack frame that locked the object monitor.
058 * @throws IllegalArgumentException if
059 * <tt>stackDepth</tt> ≥ 0 but <tt>stackFrame</tt> is <tt>null</tt>,
060 * or <tt>stackDepth</tt> < 0 but <tt>stackFrame</tt> is not
061 * <tt>null</tt>.
062 */
063 public MonitorInfo(String className, int identityHashCode,
064 int stackDepth, StackTraceElement stackFrame) {
065 super (className, identityHashCode);
066 if (stackDepth >= 0 && stackFrame == null) {
067 throw new IllegalArgumentException(
068 "Parameter stackDepth is " + stackDepth
069 + " but stackFrame is null");
070 }
071 if (stackDepth < 0 && stackFrame != null) {
072 throw new IllegalArgumentException(
073 "Parameter stackDepth is " + stackDepth
074 + " but stackFrame is not null");
075 }
076 this .stackDepth = stackDepth;
077 this .stackFrame = stackFrame;
078 }
079
080 /**
081 * Returns the depth in the stack trace where the object monitor
082 * was locked. The depth is the index to the <tt>StackTraceElement</tt>
083 * array returned in the {@link ThreadInfo#getStackTrace} method.
084 *
085 * @return the depth in the stack trace where the object monitor
086 * was locked, or a negative number if not available.
087 */
088 public int getLockedStackDepth() {
089 return stackDepth;
090 }
091
092 /**
093 * Returns the stack frame that locked the object monitor.
094 *
095 * @return <tt>StackTraceElement</tt> that locked the object monitor,
096 * or <tt>null</tt> if not available.
097 */
098 public StackTraceElement getLockedStackFrame() {
099 return stackFrame;
100 }
101
102 /**
103 * Returns a <tt>MonitorInfo</tt> object represented by the
104 * given <tt>CompositeData</tt>.
105 * The given <tt>CompositeData</tt> must contain the following attributes
106 * as well as the attributes specified in the
107 * <a href="LockInfo.html#MappedType">
108 * mapped type</a> for the {@link LockInfo} class:
109 * <blockquote>
110 * <table border>
111 * <tr>
112 * <th align=left>Attribute Name</th>
113 * <th align=left>Type</th>
114 * </tr>
115 * <tr>
116 * <td>lockedStackFrame</td>
117 * <td><tt>CompositeData as specified in the
118 * <a href="ThreadInfo.html#StackTrace">stackTrace</a>
119 * attribute defined in the {@link ThreadInfo#from
120 * ThreadInfo.from} method.
121 * </tt></td>
122 * </tr>
123 * <tr>
124 * <td>lockedStackDepth</td>
125 * <td><tt>java.lang.Integer</tt></td>
126 * </tr>
127 * </table>
128 * </blockquote>
129 *
130 * @param cd <tt>CompositeData</tt> representing a <tt>MonitorInfo</tt>
131 *
132 * @throws IllegalArgumentException if <tt>cd</tt> does not
133 * represent a <tt>MonitorInfo</tt> with the attributes described
134 * above.
135
136 * @return a <tt>MonitorInfo</tt> object represented
137 * by <tt>cd</tt> if <tt>cd</tt> is not <tt>null</tt>;
138 * <tt>null</tt> otherwise.
139 */
140 public static MonitorInfo from(CompositeData cd) {
141 if (cd == null) {
142 return null;
143 }
144
145 if (cd instanceof MonitorInfoCompositeData) {
146 return ((MonitorInfoCompositeData) cd).getMonitorInfo();
147 } else {
148 MonitorInfoCompositeData.validateCompositeData(cd);
149 String className = MonitorInfoCompositeData
150 .getClassName(cd);
151 int identityHashCode = MonitorInfoCompositeData
152 .getIdentityHashCode(cd);
153 int stackDepth = MonitorInfoCompositeData
154 .getLockedStackDepth(cd);
155 StackTraceElement stackFrame = MonitorInfoCompositeData
156 .getLockedStackFrame(cd);
157 return new MonitorInfo(className, identityHashCode,
158 stackDepth, stackFrame);
159 }
160 }
161
162 }
|