1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.events;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  
24  import java.util.Map;
25  
26  /**
27   * <a href="ShutdownHook.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   *
31   */
32  public class ShutdownHook implements Runnable {
33  
34      public void run() {
35          if (GetterUtil.getBoolean(
36                  System.getProperty("shutdown.hook.print.full.thread.dump"))) {
37  
38              printFullThreadDump();
39          }
40      }
41  
42      protected void printFullThreadDump() {
43          StringBuilder sb = new StringBuilder();
44  
45          sb.append("Full thread dump ");
46          sb.append(System.getProperty("java.vm.name"));
47          sb.append(" ");
48          sb.append(System.getProperty("java.vm.version"));
49          sb.append("\n\n");
50  
51          Map<Thread, StackTraceElement[]> stackTraces =
52              Thread.getAllStackTraces();
53  
54          for (Thread thread : stackTraces.keySet()) {
55              StackTraceElement[] elements = stackTraces.get(thread);
56  
57              sb.append("\"");
58              sb.append(thread.getName());
59              sb.append("\"");
60  
61              if (thread.getThreadGroup() != null) {
62                  sb.append(" (");
63                  sb.append(thread.getThreadGroup().getName());
64                  sb.append(")");
65              }
66  
67              sb.append(", priority=");
68              sb.append(thread.getPriority());
69              sb.append(", id=");
70              sb.append(thread.getId());
71              sb.append(", state=");
72              sb.append(thread.getState());
73              sb.append("\n");
74  
75              for (int i = 0; i < elements.length; i++) {
76                  sb.append("\t");
77                  sb.append(elements[i]);
78                  sb.append("\n");
79              }
80  
81              sb.append("\n");
82          }
83  
84          System.out.println(sb);
85      }
86  
87  }