KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > management > MonitorInfo


1 /*
2  * @(#)MonitorInfo.java 1.5 06/05/05
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang.management;
9
10 import javax.management.openmbean.CompositeData JavaDoc;
11 import sun.management.MonitorInfoCompositeData;
12
13 /**
14  * Information about an object monitor lock. An object monitor is locked
15  * when entering a synchronization block or method on that object.
16  *
17  * <h4>MXBean Mapping</h4>
18  * <tt>MonitorInfo</tt> is mapped to a {@link CompositeData CompositeData}
19  * with attributes as specified in
20  * the {@link #from from} method.
21  *
22  * @author Mandy Chung
23  * @version 1.5, 05/05/06
24  * @since 1.6
25  */

26 public class MonitorInfo extends LockInfo JavaDoc {
27
28     private int stackDepth;
29     private StackTraceElement JavaDoc stackFrame;
30     
31     /**
32      * Construct a <tt>MonitorInfo</tt> object.
33      *
34      * @param className the fully qualified name of the class of the lock object.
35      * @param identityHashCode the {@link System#identityHashCode
36      * identity hash code} of the lock object.
37      * @param stackDepth the depth in the stack trace where the object monitor
38      * was locked.
39      * @param stackFrame the stack frame that locked the object monitor.
40      * @throws IllegalArgumentException if
41      * <tt>stackDepth</tt> &ge; 0 but <tt>stackFrame</tt> is <tt>null</tt>,
42      * or <tt>stackDepth</tt> &lt; 0 but <tt>stackFrame</tt> is not
43      * <tt>null</tt>.
44      */

45     public MonitorInfo(String JavaDoc className,
46                        int identityHashCode,
47                        int stackDepth,
48                        StackTraceElement JavaDoc stackFrame) {
49         super(className, identityHashCode);
50         if (stackDepth >= 0 && stackFrame == null) {
51             throw new IllegalArgumentException JavaDoc("Parameter stackDepth is " +
52                 stackDepth + " but stackFrame is null");
53         }
54         if (stackDepth < 0 && stackFrame != null) {
55             throw new IllegalArgumentException JavaDoc("Parameter stackDepth is " +
56                 stackDepth + " but stackFrame is not null");
57         }
58         this.stackDepth = stackDepth;
59         this.stackFrame = stackFrame;
60     }
61
62     /**
63      * Returns the depth in the stack trace where the object monitor
64      * was locked. The depth is the index to the <tt>StackTraceElement</tt>
65      * array returned in the {@link ThreadInfo#getStackTrace} method.
66      *
67      * @return the depth in the stack trace where the object monitor
68      * was locked, or a negative number if not available.
69      */

70     public int getLockedStackDepth() {
71         return stackDepth;
72     }
73
74     /**
75      * Returns the stack frame that locked the object monitor.
76      *
77      * @return <tt>StackTraceElement</tt> that locked the object monitor,
78      * or <tt>null</tt> if not available.
79      */

80     public StackTraceElement JavaDoc getLockedStackFrame() {
81         return stackFrame;
82     }
83
84     /**
85      * Returns a <tt>MonitorInfo</tt> object represented by the
86      * given <tt>CompositeData</tt>.
87      * The given <tt>CompositeData</tt> must contain the following attributes
88      * as well as the attributes specified in the
89      * <a HREF="LockInfo.html#MappedType">
90      * mapped type</a> for the {@link LockInfo} class:
91      * <blockquote>
92      * <table border>
93      * <tr>
94      * <th align=left>Attribute Name</th>
95      * <th align=left>Type</th>
96      * </tr>
97      * <tr>
98      * <td>lockedStackFrame</td>
99      * <td><tt>CompositeData as specified in the
100      * <a HREF="ThreadInfo.html#StackTrace">stackTrace</a>
101      * attribute defined in the {@link ThreadInfo#from
102      * ThreadInfo.from} method.
103      * </tt></td>
104      * </tr>
105      * <tr>
106      * <td>lockedStackDepth</td>
107      * <td><tt>java.lang.Integer</tt></td>
108      * </tr>
109      * </table>
110      * </blockquote>
111      *
112      * @param cd <tt>CompositeData</tt> representing a <tt>MonitorInfo</tt>
113      *
114      * @throws IllegalArgumentException if <tt>cd</tt> does not
115      * represent a <tt>MonitorInfo</tt> with the attributes described
116      * above.
117
118      * @return a <tt>MonitorInfo</tt> object represented
119      * by <tt>cd</tt> if <tt>cd</tt> is not <tt>null</tt>;
120      * <tt>null</tt> otherwise.
121      */

122     public static MonitorInfo JavaDoc from(CompositeData JavaDoc cd) {
123         if (cd == null) {
124             return null;
125         }
126
127         if (cd instanceof MonitorInfoCompositeData) {
128             return ((MonitorInfoCompositeData) cd).getMonitorInfo();
129         } else {
130             MonitorInfoCompositeData.validateCompositeData(cd);
131             String JavaDoc className = MonitorInfoCompositeData.getClassName(cd);
132             int identityHashCode = MonitorInfoCompositeData.getIdentityHashCode(cd);
133             int stackDepth = MonitorInfoCompositeData.getLockedStackDepth(cd);
134             StackTraceElement JavaDoc stackFrame = MonitorInfoCompositeData.getLockedStackFrame(cd);
135             return new MonitorInfo JavaDoc(className,
136                                    identityHashCode,
137                                    stackDepth,
138                                    stackFrame);
139         }
140     }
141
142 }
143
Popular Tags