1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.events;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.StringBundler;
19  
20  import java.util.Map;
21  
22  /**
23   * <a href="ShutdownHook.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Brian Wing Shun Chan
26   */
27  public class ShutdownHook implements Runnable {
28  
29      public void run() {
30          if (GetterUtil.getBoolean(
31                  System.getProperty("shutdown.hook.print.full.thread.dump"))) {
32  
33              printFullThreadDump();
34          }
35      }
36  
37      protected void printFullThreadDump() {
38          StringBundler sb = new StringBundler();
39  
40          sb.append("Full thread dump ");
41          sb.append(System.getProperty("java.vm.name"));
42          sb.append(" ");
43          sb.append(System.getProperty("java.vm.version"));
44          sb.append("\n\n");
45  
46          Map<Thread, StackTraceElement[]> stackTraces =
47              Thread.getAllStackTraces();
48  
49          for (Thread thread : stackTraces.keySet()) {
50              StackTraceElement[] elements = stackTraces.get(thread);
51  
52              sb.append("\"");
53              sb.append(thread.getName());
54              sb.append("\"");
55  
56              if (thread.getThreadGroup() != null) {
57                  sb.append(" (");
58                  sb.append(thread.getThreadGroup().getName());
59                  sb.append(")");
60              }
61  
62              sb.append(", priority=");
63              sb.append(thread.getPriority());
64              sb.append(", id=");
65              sb.append(thread.getId());
66              sb.append(", state=");
67              sb.append(thread.getState());
68              sb.append("\n");
69  
70              for (int i = 0; i < elements.length; i++) {
71                  sb.append("\t");
72                  sb.append(elements[i]);
73                  sb.append("\n");
74              }
75  
76              sb.append("\n");
77          }
78  
79          System.out.println(sb);
80      }
81  
82  }