blob: cc4e41a2d99f35e0612fbfc6095d17b5846f3fa8 [file] [log] [blame]
Felix Meschberger9808ed82013-06-20 12:47:35 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.threaddump.internal;
20
21import java.io.PrintWriter;
22import java.text.MessageFormat;
23import java.util.Date;
24
25/**
26 *
27 */
28public final class ThreadWriter
29{
30
31 public static final short NEW = 0;
32
33 public static final short RUNNABLE = 1;
34
35 public static final short BLOCKED = 2;
36
37 public static final short WAITING = 3;
38
39 public static final short TIMED_WAITING = 4;
40
41 public static final short TERMINATED = 5;
42
43 private static final String DATE = "{0,date,yyyy-MM-dd HH:mm:ss}";
44
45 private static final String HEADER = "Full thread dump {0} ({1} {2}):";
46
47 // nid is unknown
48 private static final String THREAD = "\"{0}\" {1}prio={2} tid=0x{3} nid=0x{4} {5,choice,0#new|1#runnable|2#waiting for monitor entry|3#in Object.wait()|4#timed_waiting|5#terminated}";
49
50 private static final String THREAD_STATUS = " java.lang.Thread.State: {0,choice,0#NEW|1#RUNNABLE|2#BLOCKED|3#WAITING (on object monitor)|4#TIMED_WAITING|5#TERMINATED}";
51
52 private static final String STACKTRACE_ELEMENT = "\tat {0}";
53
54 private final PrintWriter writer;
55
56 public ThreadWriter(PrintWriter writer)
57 {
58 this.writer = writer;
59 }
60
61 /**
62 * Full thread dump identifier
63 */
64 public void printHeader()
65 {
66 println(DATE, new Object[]
67 { new Date() });
68 println(HEADER, getSystemProperties(new String[]
69 { "java.vm.name", "java.runtime.version", "java.vm.info" }));
70 printEmptyLine();
71 }
72
73 public void printThread(String name, boolean isDaemon, long priority, long id, short status)
74 {
75 String daemon = isDaemon ? "daemon " : "";
76 println(THREAD, new Object[]
77 { name, daemon, String.valueOf(priority), Long.toHexString(id), Integer.toHexString(-1), // nid
78 new Short(status) });
79 println(THREAD_STATUS, new Object[]
80 { new Short(status) });
81 }
82
83 public void printStackTrace(StackTraceElement[] stackTrace)
84 {
85 if (stackTrace != null)
86 {
87 for (int i = 0; i < stackTrace.length; i++)
88 {
89 printStackTraceElement(stackTrace[i]);
90 }
91 }
92 }
93
94 public void printStackTraceElement(StackTraceElement element)
95 {
96 println(STACKTRACE_ELEMENT, new Object[]
97 { element });
98 }
99
100 public void printEmptyLine()
101 {
102 writer.println();
103 }
104
105 public void println(String message)
106 {
107 writer.println(message);
108 }
109
110 public void println(String pattern, Object[] arguments)
111 {
112 String result = MessageFormat.format(pattern, arguments);
113 writer.println(result);
114 }
115
116 private static Object[] getSystemProperties(String[] keys)
117 {
118 Object[] values = new Object[keys.length];
119
120 for (int i = 0; i < keys.length; i++)
121 {
122 values[i] = System.getProperty(keys[i]);
123 }
124
125 return values;
126 }
127
128}