001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }