001    /**
002     * Copyright (c) 2000-2010 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 (Thread thread : stackTraces.keySet()) {
048                            StackTraceElement[] elements = stackTraces.get(thread);
049    
050                            sb.append("\"");
051                            sb.append(thread.getName());
052                            sb.append("\"");
053    
054                            if (thread.getThreadGroup() != null) {
055                                    sb.append(" (");
056                                    sb.append(thread.getThreadGroup().getName());
057                                    sb.append(")");
058                            }
059    
060                            sb.append(", priority=");
061                            sb.append(thread.getPriority());
062                            sb.append(", id=");
063                            sb.append(thread.getId());
064                            sb.append(", state=");
065                            sb.append(thread.getState());
066                            sb.append("\n");
067    
068                            for (int i = 0; i < elements.length; i++) {
069                                    sb.append("\t");
070                                    sb.append(elements[i]);
071                                    sb.append("\n");
072                            }
073    
074                            sb.append("\n");
075                    }
076    
077                    System.out.println(sb);
078            }
079    
080    }