1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.events;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  
27  import java.util.Map;
28  
29  /**
30   * <a href="ShutdownHook.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class ShutdownHook implements Runnable {
35  
36      public void run() {
37          if (GetterUtil.getBoolean(
38                  System.getProperty("shutdown.hook.print.full.thread.dump"))) {
39  
40              printFullThreadDump();
41          }
42      }
43  
44      protected void printFullThreadDump() {
45          StringBuilder sb = new StringBuilder();
46  
47          sb.append("Full thread dump ");
48          sb.append(System.getProperty("java.vm.name"));
49          sb.append(" ");
50          sb.append(System.getProperty("java.vm.version"));
51          sb.append("\n\n");
52  
53          Map<Thread, StackTraceElement[]> stackTraces =
54              Thread.getAllStackTraces();
55  
56          for (Thread thread : stackTraces.keySet()) {
57              StackTraceElement[] elements = stackTraces.get(thread);
58  
59              sb.append("\"");
60              sb.append(thread.getName());
61              sb.append("\"");
62  
63              if (thread.getThreadGroup() != null) {
64                  sb.append(" (");
65                  sb.append(thread.getThreadGroup().getName());
66                  sb.append(")");
67              }
68  
69              sb.append(", priority=");
70              sb.append(thread.getPriority());
71              sb.append(", id=");
72              sb.append(thread.getId());
73              sb.append(", state=");
74              sb.append(thread.getState());
75              sb.append("\n");
76  
77              for (int i = 0; i < elements.length; i++) {
78                  sb.append("\t");
79                  sb.append(elements[i]);
80                  sb.append("\n");
81              }
82  
83              sb.append("\n");
84          }
85  
86          System.out.println(sb);
87      }
88  
89  }