KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > management > data > jdk > ThreadInfoFormat


1 /**
2 * Copyright (c) 2004-2005 jManage.org
3 *
4 * This is a free software; you can redistribute it and/or
5 * modify it under the terms of the license at
6 * http://www.jmanage.org.
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */

14 package org.jmanage.core.management.data.jdk;
15
16 import org.jmanage.core.management.data.DataFormat;
17
18 import javax.management.openmbean.CompositeData JavaDoc;
19
20 /**
21  *
22  * Name: main (id=1)
23  * State: TIMED_WAITING
24  * Total blocked: 0 Total waited: 0
25  *
26  * Stack trace:
27  * java.lang.Thread.sleep(Native Method)
28  * JConsoleTest.main(JConsoleTest.java:13)
29  *
30  * <p>
31  * Date: Dec 9, 2005
32  * @author Rakesh Kalra
33  */

34 public class ThreadInfoFormat implements DataFormat {
35
36     private static final DataFormat stackTraceFormatter =
37             new StackTraceElementFormat();
38
39     public String JavaDoc format(Object JavaDoc data) {
40
41         CompositeData JavaDoc compositeData = (CompositeData JavaDoc)data;
42         if(!compositeData.getCompositeType().getTypeName().
43                 equals("java.lang.management.ThreadInfo")){
44             throw new RuntimeException JavaDoc("Invalid typeName:" +
45                     compositeData.getCompositeType().getTypeName());
46         }
47
48         StringBuffer JavaDoc threadInfo = new StringBuffer JavaDoc();
49         threadInfo.append("Name: ");
50         threadInfo.append(compositeData.get("threadName"));
51         threadInfo.append(" (id=");
52         threadInfo.append(compositeData.get("threadId"));
53         threadInfo.append(")\n");
54         threadInfo.append("State: ");
55         String JavaDoc threadState = (String JavaDoc)compositeData.get("threadState");
56         threadInfo.append(threadState);
57         if(threadState.equals("WAITING")){
58             threadInfo.append(" on ");
59             threadInfo.append(compositeData.get("lockName"));
60         }
61         threadInfo.append("\n");
62         threadInfo.append("Total blocked: ");
63         threadInfo.append(compositeData.get("blockedCount"));
64         threadInfo.append(" Total waited: ");
65         threadInfo.append(compositeData.get("waitedCount"));
66         threadInfo.append("\n");
67         CompositeData JavaDoc[] stackTrace =
68                 (CompositeData JavaDoc[])compositeData.get("stackTrace");
69
70         threadInfo.append("Strack trace:\n");
71         threadInfo.append(stackTraceFormatter.format(stackTrace));
72         threadInfo.append("\n\n");
73         return threadInfo.toString();
74     }
75 }
76
Popular Tags