001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    
020    import java.util.Map;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class ShutdownHook implements Runnable {
026    
027            public void run() {
028                    if (GetterUtil.getBoolean(
029                                    System.getProperty("shutdown.hook.print.full.thread.dump"))) {
030    
031                            printFullThreadDump();
032                    }
033            }
034    
035            protected void printFullThreadDump() {
036                    StringBundler sb = new StringBundler();
037    
038                    sb.append("Full thread dump ");
039                    sb.append(System.getProperty("java.vm.name"));
040                    sb.append(" ");
041                    sb.append(System.getProperty("java.vm.version"));
042                    sb.append("\n\n");
043    
044                    Map<Thread, StackTraceElement[]> stackTraces =
045                            Thread.getAllStackTraces();
046    
047                    for (Map.Entry<Thread, StackTraceElement[]> entry :
048                                    stackTraces.entrySet()) {
049    
050                            Thread thread = entry.getKey();
051                            StackTraceElement[] elements = entry.getValue();
052    
053                            sb.append("\"");
054                            sb.append(thread.getName());
055                            sb.append("\"");
056    
057                            if (thread.getThreadGroup() != null) {
058                                    sb.append(" (");
059                                    sb.append(thread.getThreadGroup().getName());
060                                    sb.append(")");
061                            }
062    
063                            sb.append(", priority=");
064                            sb.append(thread.getPriority());
065                            sb.append(", id=");
066                            sb.append(thread.getId());
067                            sb.append(", state=");
068                            sb.append(thread.getState());
069                            sb.append("\n");
070    
071                            for (int i = 0; i < elements.length; i++) {
072                                    sb.append("\t");
073                                    sb.append(elements[i]);
074                                    sb.append("\n");
075                            }
076    
077                            sb.append("\n");
078                    }
079    
080                    System.out.println(sb);
081            }
082    
083    }