KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > util > exception > ExceptionHelper


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.util.exception;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 /**
26  * General utilities supporting the packaging of exception messages.
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  */

29 public class ExceptionHelper
30 {
31     private static final String JavaDoc LINE_SEPARATOR =
32       System.getProperty( "line.separator" );
33
34     private static final String JavaDoc HEADER = "----";
35     private static final String JavaDoc EXCEPTION = HEADER + " exception report ";
36     private static final String JavaDoc COMPOSITE = HEADER + " composite report ";
37     private static final String JavaDoc RUNTIME = HEADER + " runtime exception report ";
38     private static final String JavaDoc ERROR = HEADER + " error report ";
39     private static final String JavaDoc CAUSE = HEADER + " cause ";
40     private static final String JavaDoc TRACE = HEADER + " stack trace ";
41     private static final String JavaDoc END = "";
42
43     private static final int WIDTH = 80;
44
45     /**
46      * Returns the exception and causal exceptions as a formatted string.
47      * @param e the exception
48      * @return String the formatting string
49      */

50     public static String JavaDoc packException( final Throwable JavaDoc e )
51     {
52         return packException( null, e );
53     }
54
55     /**
56      * Returns the exception and causal exceptions as a formatted string.
57      * @param e the exception
58      * @param stack TRUE to generate a stack trace
59      * @return String the formatting string
60      */

61     public static String JavaDoc packException( final Throwable JavaDoc e, boolean stack )
62     {
63         return packException( null, e, stack );
64     }
65
66
67     /**
68      * Returns the exception and causal exceptions as a formatted string.
69      * @param message the header message
70      * @param e the exception
71      * @return String the formatting string
72      */

73     public static String JavaDoc packException( final String JavaDoc message, final Throwable JavaDoc e )
74     {
75         return packException( message, e, false );
76     }
77
78     /**
79      * Returns the exception and causal exceptions as a formatted string.
80      * @param message the header message
81      * @param e the exception
82      * @param stack TRUE to generate a stack trace
83      * @return String the formatting string
84      */

85     public static String JavaDoc packException(
86        final String JavaDoc message, final Throwable JavaDoc e, boolean stack )
87     {
88         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
89         packException( buffer, 0, message, e, stack );
90         buffer.append( getLine( END ) );
91         return buffer.toString();
92     }
93
94
95     /**
96      * Returns the exception and causal exceptions as a formatted string.
97      * @param message the header message
98      * @param e the exceptions
99      * @param stack TRUE to generate a stack trace
100      * @return String the formatting string
101      */

102     public static String JavaDoc packException(
103        final String JavaDoc message, final Throwable JavaDoc[] e, boolean stack )
104     {
105         final String JavaDoc lead = COMPOSITE + "(" + e.length + " entries) ";
106         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( 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     /**
121      * Returns the exception and causal exceptions as a formatted string.
122      * @param message the header message
123      * @param e the exception
124      * @param stack TRUE to generate a stack trace
125      * @return String the formatting string
126      */

127     private static void packException(
128        StringBuffer JavaDoc buffer, int j, final String JavaDoc message, final Throwable JavaDoc e, boolean stack )
129     {
130         if( e instanceof Error JavaDoc )
131         {
132             buffer.append( getLine( ERROR, j ) );
133         }
134         else if( e instanceof RuntimeException JavaDoc )
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 JavaDoc root = getLastThrowable( e );
154         if( (root != null) && stack )
155         {
156             buffer.append( getLine( TRACE ) );
157             String JavaDoc[] 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 JavaDoc packCause( StringBuffer JavaDoc buffer, Throwable JavaDoc 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 JavaDoc getLastThrowable( Throwable JavaDoc exception )
178     {
179         Throwable JavaDoc cause = getCause( exception );
180         if( cause != null )
181           return getLastThrowable( cause );
182         return exception;
183     }
184
185     private static Throwable JavaDoc getCause( Throwable JavaDoc exception )
186     {
187         if( exception == null )
188           throw new NullPointerException JavaDoc( "exception" );
189
190         try
191         {
192             Method JavaDoc method =
193               exception.getClass().getMethod( "getCause", new Class JavaDoc[0] );
194             return (Throwable JavaDoc) method.invoke( exception, new Object JavaDoc[0] );
195         }
196         catch( Throwable JavaDoc e )
197         {
198             return null;
199         }
200     }
201
202     /**
203      * Captures the stack trace associated with this exception.
204      *
205      * @param throwable a <code>Throwable</code>
206      * @return an array of Strings describing stack frames.
207      */

208     private static String JavaDoc[] captureStackTrace( final Throwable JavaDoc throwable )
209     {
210         final StringWriter JavaDoc sw = new StringWriter JavaDoc();
211         throwable.printStackTrace( new PrintWriter JavaDoc( sw, true ) );
212         return splitString( sw.toString(), LINE_SEPARATOR );
213     }
214
215     /**
216      * Splits the string on every token into an array of stack frames.
217      *
218      * @param string the string to split
219      * @param onToken the token to split on
220      * @return the resultant array
221      */

222     private static String JavaDoc[] splitString( final String JavaDoc string, final String JavaDoc onToken )
223     {
224         final StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( string, onToken );
225         final String JavaDoc[] result = new String JavaDoc[tokenizer.countTokens()];
226
227         for( int i = 0; i < result.length; i++ )
228         {
229             String JavaDoc 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 JavaDoc getLine( String JavaDoc lead )
244     {
245         return getLine( lead, 0 );
246     }
247
248     private static String JavaDoc getLine( String JavaDoc lead, int count )
249     {
250         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( lead );
251         int q = 0;
252         if( count > 0 )
253         {
254             String JavaDoc 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