KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > ExceptionUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/util/misc/ExceptionUtil.java,v 1.5 2006/03/09 20:30:33 llc Exp $
26  * $Revision: 1.5 $
27  * $Date: 2006/03/09 20:30:33 $
28  */

29  
30 package com.sun.appserv.management.util.misc;
31
32 import java.util.ArrayList JavaDoc;
33
34
35 /**
36     Useful utilities for Exceptions
37  */

38 public final class ExceptionUtil
39 {
40         private
41     ExceptionUtil()
42     {
43         // disallow instantiation
44
}
45     
46         public static String JavaDoc
47     toString( final Throwable JavaDoc t )
48     {
49         final String JavaDoc SEP = System.getProperty( "line.separator" );
50         
51         final Throwable JavaDoc rootCause = getRootCause( t );
52         
53         return rootCause.getClass().getName() + ": " +
54             StringUtil.quote( rootCause.getMessage() ) + SEP +
55             getStackTrace( rootCause );
56     }
57         
58     /**
59         Get the chain of exceptions via getCause(). The first element is the
60         Exception passed.
61         
62         @param start the Exception to traverse
63         @return a Throwable[] or an Exception[] as appropriate
64      */

65         public static Throwable JavaDoc[]
66     getCauses( final Throwable JavaDoc start )
67     {
68         final ArrayList JavaDoc<Throwable JavaDoc> list = new ArrayList JavaDoc<Throwable JavaDoc>();
69         
70         boolean haveNonException = false;
71         
72         Throwable JavaDoc t = start;
73         while ( t != null )
74         {
75             list.add( t );
76             
77             if ( ! ( t instanceof Exception JavaDoc ) )
78             {
79                 haveNonException = true;
80             }
81             
82             final Throwable JavaDoc temp = t.getCause();
83             if ( temp == null )
84                 break;
85             t = temp;
86         }
87         
88         final Throwable JavaDoc[] results = haveNonException ?
89             new Throwable JavaDoc[ list.size() ] : new Exception JavaDoc[ list.size() ];
90         
91         list.toArray( results );
92         
93         return( results );
94     }
95     
96     
97     /**
98         Get the original troublemaker.
99         
100         @param e the Exception to dig into
101         @return the original Throwable that started the problem
102      */

103         public static Throwable JavaDoc
104     getRootCause( final Throwable JavaDoc e )
105     {
106         final Throwable JavaDoc[] causes = getCauses( e );
107         
108         return( causes[ causes.length - 1 ] );
109     }
110     
111     /**
112         Get the stack trace as a String.
113         
114         @param t the Throwabe whose stack trace should be gotten
115         @return a String containing the stack trace
116      */

117         public static String JavaDoc
118     getStackTrace( Throwable JavaDoc t )
119     {
120         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
121         final StackTraceElement JavaDoc[] elems = t.getStackTrace();
122         
123         for( int i = 0; i < elems.length; ++i )
124         {
125             buf.append( elems[ i ] );
126             buf.append( "\n" );
127         }
128         
129         
130         return( buf.toString() );
131     }
132 }
133
134
Popular Tags