prepare Transport - Java java.lang

Java examples for java.lang:Throwable

Description

prepare Transport

Demo Code


import java.io.PrintWriter;
import java.io.StringWriter;

public class Main{
    public static Throwable prepareTransport(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        Throwable deepestThrowable = getDeepestThrowable(t);
        deepestThrowable.printStackTrace(pw);

        pw.flush();/*from   w ww. j  a v  a  2s . c o  m*/
        sw.flush();

        String messageIncludingOriginalStackTrace = "Exception occured on Hibernate Server Process: "
                + deepestThrowable.getMessage() + "\n";

        String stackTraceString = sw.toString();
        String deepestToString = deepestThrowable.toString();
        if (("" + deepestThrowable.getMessage()).equals(deepestToString)
                || stackTraceString.startsWith(deepestToString)) {
            messageIncludingOriginalStackTrace += stackTraceString;
        } else {
            messageIncludingOriginalStackTrace += (deepestToString + "\n" + stackTraceString);
        }

        return new SquirrelHibernateServerException(
                messageIncludingOriginalStackTrace,
                deepestThrowable.getMessage(), deepestToString,
                deepestThrowable.getClass().getName());
    }
    public static Throwable getDeepestThrowable(Throwable t) {
        Throwable parent = t;
        Throwable child = t.getCause();
        while (null != child) {
            parent = child;
            child = parent.getCause();
        }

        return parent;

    }
}

Related Tutorials