001    /**
002     * Copyright (c) 2000-present 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.kernel.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018    
019    import java.io.PrintWriter;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class StackTraceUtil {
025    
026            public static String getCallerKey() {
027                    Exception e = new Exception();
028    
029                    StackTraceElement[] stackTraceElements = e.getStackTrace();
030    
031                    StackTraceElement stackTraceElement = stackTraceElements[1];
032    
033                    return stackTraceElement.getClassName() + "#" +
034                            stackTraceElement.getMethodName() + "#" +
035                                    stackTraceElement.getLineNumber();
036            }
037    
038            public static String getStackTrace(Throwable t) {
039                    String stackTrace = null;
040    
041                    PrintWriter printWriter = null;
042    
043                    try {
044                            UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
045    
046                            printWriter = UnsyncPrintWriterPool.borrow(unsyncStringWriter);
047    
048                            t.printStackTrace(printWriter);
049    
050                            printWriter.flush();
051    
052                            stackTrace = unsyncStringWriter.toString();
053                    }
054                    finally {
055                            if (printWriter != null) {
056                                    printWriter.flush();
057                                    printWriter.close();
058                            }
059                    }
060    
061                    return stackTrace;
062            }
063    
064    }