1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }