View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.message;
18  
19  import java.io.InvalidObjectException;
20  import java.io.ObjectInputStream;
21  import java.io.Serializable;
22  import java.lang.management.ManagementFactory;
23  import java.lang.management.ThreadInfo;
24  import java.lang.management.ThreadMXBean;
25  import java.lang.reflect.Method;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  /**
30   * Captures information about all running Threads.
31   */
32  public class ThreadDumpMessage implements Message {
33  
34      private static final long serialVersionUID = -1103400781608841088L;
35  
36      private static final ThreadInfoFactory FACTORY;
37  
38      private volatile Map<ThreadInformation, StackTraceElement[]> threads;
39  
40      private final String title;
41  
42      private String formattedMessage;
43  
44      static {
45          final Method[] methods = ThreadInfo.class.getMethods();
46          boolean basic = true;
47          for (final Method method : methods) {
48              if (method.getName().equals("getLockInfo")) {
49                  basic = false;
50                  break;
51              }
52          }
53          FACTORY = basic ? new BasicThreadInfoFactory() : new ExtendedThreadInfoFactory();
54      }
55  
56      /**
57       * Generate a ThreadDumpMessage with a title.
58       * @param title The title.
59       */
60      public ThreadDumpMessage(final String title) {
61          this.title = title == null ? "" : title;
62          threads = FACTORY.createThreadInfo();
63      }
64  
65      private ThreadDumpMessage(final String formattedMsg, final String title) {
66          this.formattedMessage = formattedMsg;
67          this.title = title == null ? "" : title;
68      }
69  
70      @Override
71      public String toString() {
72          final StringBuilder sb = new StringBuilder("ThreadDumpMessage[");
73          if (this.title.length() > 0) {
74              sb.append("Title=\"").append(this.title).append("\"");
75          }
76          sb.append("]");
77          return sb.toString();
78      }
79  
80      /**
81       * Returns the ThreadDump in printable format.
82       * @return the ThreadDump suitable for logging.
83       */
84      public String getFormattedMessage() {
85          if (formattedMessage != null) {
86              return formattedMessage;
87          }
88          final StringBuilder sb = new StringBuilder(title);
89          if (title.length() > 0) {
90              sb.append("\n");
91          }
92          for (final Map.Entry<ThreadInformation, StackTraceElement[]> entry : threads.entrySet()) {
93              final ThreadInformation info = entry.getKey();
94              info.printThreadInfo(sb);
95              info.printStack(sb, entry.getValue());
96              sb.append("\n");
97          }
98          return sb.toString();
99      }
100 
101     /**
102      * Returns the title.
103      * @return the title.
104      */
105     public String getFormat() {
106         return title == null ? "" : title;
107     }
108 
109     /**
110      * Returns an array with a single element, a Map containing the ThreadInformation as the key.
111      * and the StackTraceElement array as the value;
112      * @return the "parameters" to this Message.
113      */
114     public Object[] getParameters() {
115         return null;
116     }
117 
118         /**
119      * Creates a ThreadDumpMessageProxy that can be serialized.
120      * @return a ThreadDumpMessageProxy.
121      */
122     protected Object writeReplace() {
123         return new ThreadDumpMessageProxy(this);
124     }
125 
126     private void readObject(final ObjectInputStream stream)
127         throws InvalidObjectException {
128         throw new InvalidObjectException("Proxy required");
129     }
130 
131     /**
132      * Proxy pattern used to serialize the ThreadDumpMessage.
133      */
134     private static class ThreadDumpMessageProxy implements Serializable {
135 
136         private static final long serialVersionUID = -3476620450287648269L;
137         private final String formattedMsg;
138         private final String title;
139 
140         public ThreadDumpMessageProxy(final ThreadDumpMessage msg) {
141             this.formattedMsg = msg.getFormattedMessage();
142             this.title = msg.title;
143         }
144 
145         /**
146          * Returns a ThreadDumpMessage using the data in the proxy.
147          * @return a ThreadDumpMessage.
148          */
149         protected Object readResolve() {
150             return new ThreadDumpMessage(formattedMsg, title);
151         }
152     }
153 
154     /**
155      * Factory to create Thread information.
156      */
157     private interface ThreadInfoFactory {
158         Map<ThreadInformation, StackTraceElement[]> createThreadInfo();
159     }
160 
161     /**
162      * Factory to create basic thread information.
163      */
164     private static class BasicThreadInfoFactory implements ThreadInfoFactory {
165         public Map<ThreadInformation, StackTraceElement[]> createThreadInfo() {
166             final Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
167             final Map<ThreadInformation, StackTraceElement[]> threads =
168                 new HashMap<ThreadInformation, StackTraceElement[]>(map.size());
169             for (final Map.Entry<Thread, StackTraceElement[]> entry : map.entrySet()) {
170                 threads.put(new BasicThreadInformation(entry.getKey()), entry.getValue());
171             }
172             return threads;
173         }
174     }
175 
176     /**
177      * Factory to create extended thread information.
178      */
179     private static class ExtendedThreadInfoFactory implements ThreadInfoFactory {
180         public Map<ThreadInformation, StackTraceElement[]> createThreadInfo() {
181             final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
182             final ThreadInfo[] array = bean.dumpAllThreads(true, true);
183 
184             final Map<ThreadInformation, StackTraceElement[]>  threads =
185                 new HashMap<ThreadInformation, StackTraceElement[]>(array.length);
186             for (final ThreadInfo info : array) {
187                 threads.put(new ExtendedThreadInformation(info), info.getStackTrace());
188             }
189             return threads;
190         }
191     }
192 
193     /**
194      * Always returns null.
195      *
196      * @return null
197      */
198     public Throwable getThrowable() {
199         return null;
200     }
201 }