001    /**
002     * Copyright (c) 2000-2013 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            @Override
028            public void run() {
029                    if (GetterUtil.getBoolean(
030                                    System.getProperty("shutdown.hook.print.full.thread.dump"))) {
031    
032                            printFullThreadDump();
033                    }
034            }
035    
036            protected void printFullThreadDump() {
037                    StringBundler sb = new StringBundler();
038    
039                    sb.append("Full thread dump ");
040                    sb.append(System.getProperty("java.vm.name"));
041                    sb.append(" ");
042                    sb.append(System.getProperty("java.vm.version"));
043                    sb.append("\n\n");
044    
045                    Map<Thread, StackTraceElement[]> stackTraces =
046                            Thread.getAllStackTraces();
047    
048                    for (Map.Entry<Thread, StackTraceElement[]> entry :
049                                    stackTraces.entrySet()) {
050    
051                            Thread thread = entry.getKey();
052                            StackTraceElement[] elements = entry.getValue();
053    
054                            sb.append("\"");
055                            sb.append(thread.getName());
056                            sb.append("\"");
057    
058                            if (thread.getThreadGroup() != null) {
059                                    sb.append(" (");
060                                    sb.append(thread.getThreadGroup().getName());
061                                    sb.append(")");
062                            }
063    
064                            sb.append(", priority=");
065                            sb.append(thread.getPriority());
066                            sb.append(", id=");
067                            sb.append(thread.getId());
068                            sb.append(", state=");
069                            sb.append(thread.getState());
070                            sb.append("\n");
071    
072                            for (int i = 0; i < elements.length; i++) {
073                                    sb.append("\t");
074                                    sb.append(elements[i]);
075                                    sb.append("\n");
076                            }
077    
078                            sb.append("\n");
079                    }
080    
081                    System.out.println(sb);
082            }
083    
084    }