1 17 18 package org.apache.avalon.util.exception; 19 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.lang.reflect.Method ; 23 import java.util.StringTokenizer ; 24 25 29 public class ExceptionHelper 30 { 31 private static final String LINE_SEPARATOR = 32 System.getProperty( "line.separator" ); 33 34 private static final String HEADER = "----"; 35 private static final String EXCEPTION = HEADER + " exception report "; 36 private static final String COMPOSITE = HEADER + " composite report "; 37 private static final String RUNTIME = HEADER + " runtime exception report "; 38 private static final String ERROR = HEADER + " error report "; 39 private static final String CAUSE = HEADER + " cause "; 40 private static final String TRACE = HEADER + " stack trace "; 41 private static final String END = ""; 42 43 private static final int WIDTH = 80; 44 45 50 public static String packException( final Throwable e ) 51 { 52 return packException( null, e ); 53 } 54 55 61 public static String packException( final Throwable e, boolean stack ) 62 { 63 return packException( null, e, stack ); 64 } 65 66 67 73 public static String packException( final String message, final Throwable e ) 74 { 75 return packException( message, e, false ); 76 } 77 78 85 public static String packException( 86 final String message, final Throwable e, boolean stack ) 87 { 88 StringBuffer buffer = new StringBuffer (); 89 packException( buffer, 0, message, e, stack ); 90 buffer.append( getLine( END ) ); 91 return buffer.toString(); 92 } 93 94 95 102 public static String packException( 103 final String message, final Throwable [] e, boolean stack ) 104 { 105 final String lead = COMPOSITE + "(" + e.length + " entries) "; 106 StringBuffer buffer = new StringBuffer ( getLine( lead ) ); 107 if( null != message ) 108 { 109 buffer.append( message ); 110 buffer.append( "\n" ); 111 } 112 for( int i=0; i<e.length; i++ ) 113 { 114 packException( buffer, i+1, null, e[i], stack ); 115 } 116 buffer.append( getLine( END ) ); 117 return buffer.toString(); 118 } 119 120 127 private static void packException( 128 StringBuffer buffer, int j, final String message, final Throwable e, boolean stack ) 129 { 130 if( e instanceof Error ) 131 { 132 buffer.append( getLine( ERROR, j ) ); 133 } 134 else if( e instanceof RuntimeException ) 135 { 136 buffer.append( getLine( RUNTIME, j ) ); 137 } 138 else 139 { 140 buffer.append( getLine( EXCEPTION, j ) ); 141 } 142 143 if( null != message ) 144 { 145 buffer.append( message ); 146 buffer.append( "\n" ); 147 } 148 if( e == null ) return; 149 150 buffer.append( "Exception: " + e.getClass().getName() + "\n" ); 151 buffer.append( "Message: " + e.getMessage() + "\n" ); 152 packCause( buffer, getCause( e ) ).toString(); 153 Throwable root = getLastThrowable( e ); 154 if( (root != null) && stack ) 155 { 156 buffer.append( getLine( TRACE ) ); 157 String [] trace = captureStackTrace( root ); 158 for( int i = 0; i < trace.length; i++ ) 159 { 160 buffer.append( trace[i] + "\n" ); 161 } 162 } 163 } 164 165 private static StringBuffer packCause( StringBuffer buffer, Throwable cause ) 166 { 167 if( cause == null ) 168 { 169 return buffer; 170 } 171 buffer.append( getLine( CAUSE ) ); 172 buffer.append( "Exception: " + cause.getClass().getName() + "\n" ); 173 buffer.append( "Message: " + cause.getMessage() + "\n" ); 174 return packCause( buffer, getCause( cause ) ); 175 } 176 177 private static Throwable getLastThrowable( Throwable exception ) 178 { 179 Throwable cause = getCause( exception ); 180 if( cause != null ) 181 return getLastThrowable( cause ); 182 return exception; 183 } 184 185 private static Throwable getCause( Throwable exception ) 186 { 187 if( exception == null ) 188 throw new NullPointerException ( "exception" ); 189 190 try 191 { 192 Method method = 193 exception.getClass().getMethod( "getCause", new Class [0] ); 194 return (Throwable ) method.invoke( exception, new Object [0] ); 195 } 196 catch( Throwable e ) 197 { 198 return null; 199 } 200 } 201 202 208 private static String [] captureStackTrace( final Throwable throwable ) 209 { 210 final StringWriter sw = new StringWriter (); 211 throwable.printStackTrace( new PrintWriter ( sw, true ) ); 212 return splitString( sw.toString(), LINE_SEPARATOR ); 213 } 214 215 222 private static String [] splitString( final String string, final String onToken ) 223 { 224 final StringTokenizer tokenizer = new StringTokenizer ( string, onToken ); 225 final String [] result = new String [tokenizer.countTokens()]; 226 227 for( int i = 0; i < result.length; i++ ) 228 { 229 String token = tokenizer.nextToken(); 230 if( token.startsWith( "\tat " ) ) 231 { 232 result[i] = token.substring( 4 ); 233 } 234 else 235 { 236 result[i] = token; 237 } 238 } 239 240 return result; 241 } 242 243 private static String getLine( String lead ) 244 { 245 return getLine( lead, 0 ); 246 } 247 248 private static String getLine( String lead, int count ) 249 { 250 StringBuffer buffer = new StringBuffer ( lead ); 251 int q = 0; 252 if( count > 0 ) 253 { 254 String v = "" + count + " "; 255 buffer.append( "" + count ); 256 buffer.append( " " ); 257 q = v.length() + 1; 258 } 259 int j = WIDTH - ( lead.length() + q ); 260 for( int i=0; i<j; i++ ) 261 { 262 buffer.append( "-" ); 263 } 264 buffer.append( "\n" ); 265 return buffer.toString(); 266 } 267 } 268 | Popular Tags |