1 23 24 package org.infoglue.cms.applications.common.actions; 25 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.util.StringTokenizer ; 29 30 31 36 public final class Error 37 { 38 private Throwable throwable; 39 private Throwable cause; 40 41 44 public Error(Throwable throwable, Throwable cause) 45 { 46 this.throwable = throwable; 47 this.cause = cause; 48 } 49 50 51 52 55 public String getName() { 56 final String fullyQualifiedName = this.throwable.getClass().getName(); 57 final int index = (fullyQualifiedName.lastIndexOf(".") == -1) ? 0 : fullyQualifiedName.lastIndexOf(".") + 1; 58 return fullyQualifiedName.substring(index); 59 } 60 61 64 public String getMessage() { 65 return this.throwable.getMessage(); 66 } 67 68 71 public String getStackTrace() { 72 return getStackTrace(this.throwable); 73 } 74 75 78 public boolean hasCause() { 79 return this.cause != null; 80 } 81 82 85 public String getCauseMessage() { 86 return hasCause() ? this.cause.getMessage() : ""; 87 } 88 89 92 public String getCauseStackTrace() { 93 return hasCause() ? getStackTrace(this.cause) : ""; 94 } 95 96 97 98 103 106 private String getStackTrace(Throwable throwable) { 107 final StringBuffer sb = new StringBuffer (); 108 final StringTokenizer st = new StringTokenizer (stackTraceToString(throwable), "\n"); 109 110 while(st.hasMoreTokens()) { 111 sb.append(st.nextToken() + "<br/>"); 112 } 113 return sb.toString(); 114 } 115 116 119 private String stackTraceToString(Throwable throwable) { 120 final StringWriter sw = new StringWriter (); 121 final PrintWriter pw = new PrintWriter (sw); 122 123 throwable.printStackTrace(pw); 124 return sw.getBuffer().toString(); 125 } 126 127 128 129 } | Popular Tags |