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