KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > exception > NestableDelegate


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowledgement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgement may appear in the software itself,
24  * if and wherever such third-party acknowledgements normally appear.
25  *
26  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
27  * Foundation" must not be used to endorse or promote products derived
28  * from this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */

54 package org.hibernate.exception;
55
56 import java.io.PrintStream JavaDoc;
57 import java.io.PrintWriter JavaDoc;
58 import java.io.Serializable JavaDoc;
59 import java.io.StringWriter JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.util.Arrays JavaDoc;
62 import java.util.Collections JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.List JavaDoc;
65
66 /**
67  * <p>A shared implementation of the nestable exception functionality.</p>
68  * <p/>
69  * The code is shared between
70  * {@link org.apache.commons.lang.exception.NestableError NestableError},
71  * {@link org.apache.commons.lang.exception.NestableException NestableException} and
72  * {@link org.apache.commons.lang.exception.NestableRuntimeException NestableRuntimeException}.
73  * </p>
74  *
75  * @author <a HREF="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
76  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
77  * @author <a HREF="mailto:knielsen@apache.org">Kasper Nielsen</a>
78  * @author <a HREF="mailto:steven@caswell.name">Steven Caswell</a>
79  * @author Sean C. Sullivan
80  * @author Stephen Colebourne
81  * @version $Id: NestableDelegate.java,v 1.2 2004/11/21 00:11:27 pgmjsd Exp $
82  * @since 1.0
83  */

84 public class NestableDelegate implements Serializable JavaDoc {
85
86     /**
87      * Constructor error message.
88      */

89     private static final String JavaDoc MUST_BE_THROWABLE =
90             "The Nestable implementation passed to the NestableDelegate(Nestable) "
91             + "constructor must extend java.lang.Throwable";
92
93     /**
94      * Holds the reference to the exception or error that we're
95      * wrapping (which must be a {@link
96      * org.apache.commons.lang.exception.Nestable} implementation).
97      */

98     private Throwable JavaDoc nestable = null;
99
100     /**
101      * Whether to print the stack trace top-down.
102      * This public flag may be set by calling code, typically in initialisation.
103      *
104      * @since 2.0
105      */

106     private static boolean topDown = true;
107
108     /**
109      * Whether to trim the repeated stack trace.
110      * This public flag may be set by calling code, typically in initialisation.
111      *
112      * @since 2.0
113      */

114     private static boolean trimStackFrames = true;
115
116     /**
117      * Constructs a new <code>NestableDelegate</code> instance to manage the
118      * specified <code>Nestable</code>.
119      *
120      * @param nestable the Nestable implementation (<i>must</i> extend
121      * {@link java.lang.Throwable})
122      * @since 2.0
123      */

124     public NestableDelegate(Nestable nestable) {
125         if ( nestable instanceof Throwable JavaDoc ) {
126             this.nestable = ( Throwable JavaDoc ) nestable;
127         }
128         else {
129             throw new IllegalArgumentException JavaDoc( MUST_BE_THROWABLE );
130         }
131     }
132
133     /**
134      * Returns the error message of the <code>Throwable</code> in the chain
135      * of <code>Throwable</code>s at the specified index, numbererd from 0.
136      *
137      * @param index the index of the <code>Throwable</code> in the chain of
138      * <code>Throwable</code>s
139      * @return the error message, or null if the <code>Throwable</code> at the
140      * specified index in the chain does not contain a message
141      * @throws IndexOutOfBoundsException if the <code>index</code> argument is
142      * negative or not less than the count of <code>Throwable</code>s in the
143      * chain
144      * @since 2.0
145      */

146     public String JavaDoc getMessage(int index) {
147         Throwable JavaDoc t = this.getThrowable( index );
148         if ( Nestable.class.isInstance( t ) ) {
149             return ( ( Nestable ) t ).getMessage( 0 );
150         }
151         else {
152             return t.getMessage();
153         }
154     }
155
156     /**
157      * Returns the full message contained by the <code>Nestable</code>
158      * and any nested <code>Throwable</code>s.
159      *
160      * @param baseMsg the base message to use when creating the full
161      * message. Should be generally be called via
162      * <code>nestableHelper.getMessage( super.getMessage() )</code>,
163      * where <code>super</code> is an instance of {@link
164      * java.lang.Throwable}.
165      * @return The concatenated message for this and all nested
166      * <code>Throwable</code>s
167      * @since 2.0
168      */

169     public String JavaDoc getMessage(String JavaDoc baseMsg) {
170         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
171         if ( baseMsg != null ) {
172             msg.append( baseMsg );
173         }
174
175         Throwable JavaDoc nestedCause = ExceptionUtils.getCause( this.nestable );
176         if ( nestedCause != null ) {
177             String JavaDoc causeMsg = nestedCause.getMessage();
178             if ( causeMsg != null ) {
179                 if ( baseMsg != null ) {
180                     msg.append( ": " );
181                 }
182                 msg.append( causeMsg );
183             }
184
185         }
186         return ( msg.length() > 0 ? msg.toString() : null );
187     }
188
189     /**
190      * Returns the error message of this and any nested <code>Throwable</code>s
191      * in an array of Strings, one element for each message. Any
192      * <code>Throwable</code> not containing a message is represented in the
193      * array by a null. This has the effect of cause the length of the returned
194      * array to be equal to the result of the {@link #getThrowableCount()}
195      * operation.
196      *
197      * @return the error messages
198      * @since 2.0
199      */

200     public String JavaDoc[] getMessages() {
201         Throwable JavaDoc[] throwables = this.getThrowables();
202         String JavaDoc[] msgs = new String JavaDoc[throwables.length];
203         for ( int i = 0; i < throwables.length; i++ ) {
204             msgs[i] = Nestable.class.isInstance( throwables[i] ) ?
205                     ( ( Nestable ) throwables[i] ).getMessage( 0 ) :
206                     throwables[i].getMessage();
207         }
208         return msgs;
209     }
210
211     /**
212      * Returns the <code>Throwable</code> in the chain of
213      * <code>Throwable</code>s at the specified index, numbererd from 0.
214      *
215      * @param index the index, numbered from 0, of the <code>Throwable</code> in
216      * the chain of <code>Throwable</code>s
217      * @return the <code>Throwable</code>
218      * @throws IndexOutOfBoundsException if the <code>index</code> argument is
219      * negative or not less than the count of <code>Throwable</code>s in the
220      * chain
221      * @since 2.0
222      */

223     public Throwable JavaDoc getThrowable(int index) {
224         if ( index == 0 ) {
225             return this.nestable;
226         }
227         Throwable JavaDoc[] throwables = this.getThrowables();
228         return throwables[index];
229     }
230
231     /**
232      * Returns the number of <code>Throwable</code>s contained in the
233      * <code>Nestable</code> contained by this delegate.
234      *
235      * @return the throwable count
236      * @since 2.0
237      */

238     public int getThrowableCount() {
239         return ExceptionUtils.getThrowableCount( this.nestable );
240     }
241
242     /**
243      * Returns this delegate's <code>Nestable</code> and any nested
244      * <code>Throwable</code>s in an array of <code>Throwable</code>s, one
245      * element for each <code>Throwable</code>.
246      *
247      * @return the <code>Throwable</code>s
248      * @since 2.0
249      */

250     public Throwable JavaDoc[] getThrowables() {
251         return ExceptionUtils.getThrowables( this.nestable );
252     }
253
254     /**
255      * Returns the index, numbered from 0, of the first <code>Throwable</code>
256      * that matches the specified type in the chain of <code>Throwable</code>s
257      * held in this delegate's <code>Nestable</code> with an index greater than
258      * or equal to the specified index, or -1 if the type is not found.
259      *
260      * @param type <code>Class</code> to be found
261      * @param fromIndex the index, numbered from 0, of the starting position in
262      * the chain to be searched
263      * @return index of the first occurrence of the type in the chain, or -1 if
264      * the type is not found
265      * @throws IndexOutOfBoundsException if the <code>fromIndex</code> argument
266      * is negative or not less than the count of <code>Throwable</code>s in the
267      * chain
268      * @since 2.0
269      */

270     public int indexOfThrowable(Class JavaDoc type, int fromIndex) {
271         if ( fromIndex < 0 ) {
272             throw new IndexOutOfBoundsException JavaDoc( "The start index was out of bounds: " + fromIndex );
273         }
274         Throwable JavaDoc[] throwables = ExceptionUtils.getThrowables( this.nestable );
275         if ( fromIndex >= throwables.length ) {
276             throw new IndexOutOfBoundsException JavaDoc( "The start index was out of bounds: "
277                     + fromIndex + " >= " + throwables.length );
278         }
279         for ( int i = fromIndex; i < throwables.length; i++ ) {
280             if ( throwables[i].getClass().equals( type ) ) {
281                 return i;
282             }
283         }
284         return -1;
285     }
286
287     /**
288      * Prints the stack trace of this exception the the standar error
289      * stream.
290      */

291     public void printStackTrace() {
292         printStackTrace( System.err );
293     }
294
295     /**
296      * Prints the stack trace of this exception to the specified
297      * stream.
298      *
299      * @param out <code>PrintStream</code> to use for output.
300      * @see #printStackTrace(PrintWriter)
301      */

302     public void printStackTrace(PrintStream JavaDoc out) {
303         synchronized ( out ) {
304             PrintWriter JavaDoc pw = new PrintWriter JavaDoc( out, false );
305             printStackTrace( pw );
306             // Flush the PrintWriter before it's GC'ed.
307
pw.flush();
308         }
309     }
310
311     /**
312      * Prints the stack trace of this exception to the specified
313      * writer. If the Throwable class has a <code>getCause</code>
314      * method (i.e. running on jre1.4 or higher), this method just
315      * uses Throwable's printStackTrace() method. Otherwise, generates
316      * the stack-trace, by taking into account the 'topDown' and
317      * 'trimStackFrames' parameters. The topDown and trimStackFrames
318      * are set to 'true' by default (produces jre1.4-like stack trace).
319      *
320      * @param out <code>PrintWriter</code> to use for output.
321      */

322     public void printStackTrace(PrintWriter JavaDoc out) {
323         Throwable JavaDoc throwable = this.nestable;
324         // if running on jre1.4 or higher, use default printStackTrace
325
if ( ExceptionUtils.isThrowableNested() ) {
326             if ( throwable instanceof Nestable ) {
327                 ( ( Nestable ) throwable ).printPartialStackTrace( out );
328             }
329             else {
330                 throwable.printStackTrace( out );
331             }
332             return;
333         }
334
335         // generating the nested stack trace
336
List JavaDoc stacks = new ArrayList JavaDoc();
337         while ( throwable != null ) {
338             String JavaDoc[] st = getStackFrames( throwable );
339             stacks.add( st );
340             throwable = ExceptionUtils.getCause( throwable );
341         }
342
343         // If NOT topDown, reverse the stack
344
String JavaDoc separatorLine = "Caused by: ";
345         if ( !topDown ) {
346             separatorLine = "Rethrown as: ";
347             Collections.reverse( stacks );
348         }
349
350         // Remove the repeated lines in the stack
351
if ( trimStackFrames ) trimStackFrames( stacks );
352
353         synchronized ( out ) {
354             for ( Iterator JavaDoc iter = stacks.iterator(); iter.hasNext(); ) {
355                 String JavaDoc[] st = ( String JavaDoc[] ) iter.next();
356                 for ( int i = 0, len = st.length; i < len; i++ ) {
357                     out.println( st[i] );
358                 }
359                 if ( iter.hasNext() ) out.print( separatorLine );
360             }
361         }
362     }
363
364     /**
365      * Captures the stack trace associated with the specified
366      * <code>Throwable</code> object, decomposing it into a list of
367      * stack frames.
368      *
369      * @param t The <code>Throwable</code>.
370      * @return An array of strings describing each stack frame.
371      * @since 2.0
372      */

373     protected String JavaDoc[] getStackFrames(Throwable JavaDoc t) {
374         StringWriter JavaDoc sw = new StringWriter JavaDoc();
375         PrintWriter JavaDoc pw = new PrintWriter JavaDoc( sw, true );
376
377         // Avoid infinite loop between decompose() and printStackTrace().
378
if ( t instanceof Nestable ) {
379             ( ( Nestable ) t ).printPartialStackTrace( pw );
380         }
381         else {
382             t.printStackTrace( pw );
383         }
384         return ExceptionUtils.getStackFrames( sw.getBuffer().toString() );
385     }
386
387     /**
388      * Trims the stack frames. The first set is left untouched. The rest
389      * of the frames are truncated from the bottom by comparing with
390      * one just on top.
391      *
392      * @param stacks The list containing String[] elements
393      * @since 2.0
394      */

395     protected void trimStackFrames(List JavaDoc stacks) {
396         for ( int size = stacks.size(), i = size - 1; i > 0; i-- ) {
397             String JavaDoc[] curr = ( String JavaDoc[] ) stacks.get( i );
398             String JavaDoc[] next = ( String JavaDoc[] ) stacks.get( i - 1 );
399
400             List JavaDoc currList = new ArrayList JavaDoc( Arrays.asList( curr ) );
401             List JavaDoc nextList = new ArrayList JavaDoc( Arrays.asList( next ) );
402             ExceptionUtils.removeCommonFrames( currList, nextList );
403
404             int trimmed = curr.length - currList.size();
405             if ( trimmed > 0 ) {
406                 currList.add( "\t... " + trimmed + " more" );
407                 stacks.set( i,
408                         currList.toArray( new String JavaDoc[currList.size()] ) );
409             }
410         }
411     }
412 }
413
Popular Tags