1 55 package org.apache.log.format; 56 57 import java.io.PrintWriter ; 58 import java.io.StringWriter ; 59 import java.lang.reflect.Method ; 60 import java.util.StringTokenizer ; 61 62 72 final class ExceptionUtil 73 { 74 private static final String LINE_SEPARATOR = System.getProperty( "line.separator" ); 75 private static final String GET_CAUSE_NAME = "getCause"; 76 private static final Class [] GET_CAUSE_PARAMTYPES = new Class [ 0 ]; 77 private static final Class CASCADING_INTERFACE; 78 79 static 80 { 81 Class klass = null; 82 try 83 { 84 klass = Class.forName("org.apache.avalon.framework.CascadingThrowable"); 85 } 86 catch (Exception e) 87 { 88 klass = null; 90 } 91 92 CASCADING_INTERFACE = klass; 93 } 94 95 98 private ExceptionUtil() 99 { 100 } 101 102 108 public static String printStackTrace( final Throwable throwable ) 109 { 110 return printStackTrace( throwable, 0, true ); 111 } 112 113 120 public static String printStackTrace( final Throwable throwable, 121 final boolean printCascading ) 122 { 123 return printStackTrace( throwable, 0, printCascading ); 124 } 125 126 135 public static String printStackTrace( final Throwable throwable, final int depth ) 136 { 137 int dp = depth; 138 final String [] lines = captureStackTrace( throwable ); 139 140 if( 0 == dp || dp > lines.length ) 141 { 142 dp = lines.length; 143 } 144 145 final StringBuffer sb = new StringBuffer (); 146 147 for( int i = 0; i < dp; i++ ) 148 { 149 sb.append( lines[ i ] ); 150 sb.append( LINE_SEPARATOR ); 151 } 152 153 return sb.toString(); 154 } 155 156 164 public static String printStackTrace( final Throwable throwable, 165 final int depth, 166 final boolean printCascading ) 167 { 168 return printStackTrace( throwable, depth, printCascading, true ); 169 } 170 171 184 public static String printStackTrace( final Throwable throwable, 185 final int depth, 186 final boolean printCascading, 187 final boolean useReflection ) 188 { 189 final String result = printStackTrace( throwable, depth ); 190 191 if( !printCascading ) 192 { 193 return result; 194 } 195 else 196 { 197 final StringBuffer sb = new StringBuffer (); 198 sb.append( result ); 199 200 Throwable cause = getCause( throwable, useReflection ); 201 202 while( null != cause ) 203 { 204 sb.append( "rethrown from" ); 205 sb.append( LINE_SEPARATOR ); 206 sb.append( printStackTrace( cause, depth ) ); 207 208 cause = getCause( cause, useReflection ); 209 } 210 211 return sb.toString(); 212 } 213 } 214 215 222 public static Throwable getCause( final Throwable throwable, 223 final boolean useReflection ) 224 { 225 if( useReflection || 226 ( null != CASCADING_INTERFACE && 227 CASCADING_INTERFACE.isAssignableFrom(throwable.getClass()) ) ) 228 { 229 try 230 { 231 final Class clazz = throwable.getClass(); 232 final Method method = 233 clazz.getMethod( GET_CAUSE_NAME, GET_CAUSE_PARAMTYPES ); 234 return (Throwable )method.invoke( throwable, null ); 235 } 236 catch( final Throwable t ) 237 { 238 return null; 239 } 240 } 241 else 242 { 243 return null; 244 } 245 } 246 247 253 public static String [] captureStackTrace( final Throwable throwable ) 254 { 255 final StringWriter sw = new StringWriter (); 256 throwable.printStackTrace( new PrintWriter ( sw, true ) ); 257 return splitStringInternal( sw.toString(), LINE_SEPARATOR ); 258 } 259 260 268 public static String [] splitString( final String string, final String onToken ) 269 { 270 return splitStringInternal( string, onToken ); 271 } 272 273 280 private static String [] splitStringInternal( final String string, final String onToken ) 281 { 282 final StringTokenizer tokenizer = new StringTokenizer ( string, onToken ); 283 final String [] result = new String [ tokenizer.countTokens() ]; 284 285 for( int i = 0; i < result.length; i++ ) 286 { 287 result[ i ] = tokenizer.nextToken(); 288 } 289 290 return result; 291 } 292 } 293 | Popular Tags |