KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > throwable > v1 > ThrowableParser


1 /*
2  * @(#)ThrowableParser.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.util.throwable.v1;
30
31
32 import java.io.StringWriter JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.io.BufferedReader JavaDoc;
35 import java.io.StringReader JavaDoc;
36
37 import java.util.Vector JavaDoc;
38
39 import java.lang.reflect.Method JavaDoc;
40
41 //import org.apache.log4j.Logger;
42

43
44 /**
45  * Parses a Throwable's stack trace into its components.
46  *
47  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
48  * @since March 17, 2002
49  * @version $Date: 2003/09/23 19:54:07 $
50  */

51 public class ThrowableParser
52 {
53     //private static final Logger LOG = Logger.getLogger(
54
// ThrowableParser.class.getName() );
55

56     // JDK 1.1+
57
private BufferedReader JavaDoc traceReader;
58     
59     // JDK 1.4+
60
private Object JavaDoc[] traceList;
61     private int traceIndex;
62     
63     private static final String JavaDoc JDK14_GST_METHOD = "getStackTrace";
64     private static final Class JavaDoc[] JDK14_GST_METHOD_ARGS = new Class JavaDoc[0];
65
66     private static final String JavaDoc JDK14_GC_METHOD = "getCause";
67     private static final Class JavaDoc[] JDK14_GC_METHOD_ARGS = new Class JavaDoc[0];
68     
69     
70     
71     /**
72      * Only piecemeal iterates through the stack-trace. Supports the JDK 1.4
73      * StackTraceElement class.
74      */

75     public ThrowableParser( Throwable JavaDoc t )
76     {
77         if (t == null)
78         {
79             throw new IllegalArgumentException JavaDoc("no null arguments");
80         }
81         parseThrowable( t );
82     }
83     
84     
85     /**
86      * @return <tt>null</tt> if there are no more lines, or the next line
87      * in the list.
88      */

89     public synchronized StackTraceLineParser next()
90     {
91         if (this.traceList != null)
92         {
93             return next14();
94         }
95         
96         // else
97
return next11();
98     }
99     
100     
101     //-------------------------------------------------------------------------
102

103     
104     /**
105      *
106      */

107     protected StackTraceLineParser next11()
108     {
109         //LOG.debug( "finding next stack trace line for JDK 1.1+" );
110
while (true)
111         {
112             String JavaDoc line;
113             try
114             {
115                 line = this.traceReader.readLine();
116                 //LOG.debug("trace line = '"+line+"'");
117
}
118             catch (java.io.IOException JavaDoc ioe)
119             {
120                 throw new IllegalStateException JavaDoc("Never supposed to happen: "+
121                     ioe );
122             }
123             if (line == null)
124             {
125                 //LOG.debug("end-of-stack trace.");
126
return null;
127             }
128             try
129             {
130                 StackTraceLineParser stlp = new StackTraceLineParser( line );
131                 
132                 // found a valid line
133
return stlp;
134             }
135             catch (Exception JavaDoc e)
136             {
137                 // not a valid line. Continue.
138
//LOG.debug(" - not a valid line",e);
139
}
140         }
141     }
142     
143     
144     /**
145      *
146      */

147     protected StackTraceLineParser next14()
148     {
149         //LOG.debug( "finding next stack trace line for JDK 1.4+" );
150
if (this.traceIndex >= this.traceList.length)
151         {
152             return null;
153         }
154         
155         // this will throw an exception if the parser does not know this
156
// type. That's fine. That will show a bug in this implementation.
157
StackTraceLineParser stlp = new StackTraceLineParser(
158             this.traceList[ this.traceIndex ] );
159         ++this.traceIndex;
160         
161         return stlp;
162     }
163     
164     
165     
166     protected void parseThrowable( Throwable JavaDoc t )
167     {
168         try
169         {
170             // Construct the trace from the complete trace chain.
171
Vector JavaDoc trace = new Vector JavaDoc();
172             while (t != null)
173             {
174                 Class JavaDoc c = t.getClass();
175                 Method JavaDoc m = c.getDeclaredMethod( JDK14_GST_METHOD,
176                     JDK14_GST_METHOD_ARGS );
177                 Object JavaDoc[] o = (Object JavaDoc[])m.invoke( t, null );
178                 if (o != null)
179                 {
180                     for (int i = 0; i < o.length; ++i)
181                     {
182                         trace.addElement( o[i] );
183                     }
184                 }
185                 
186                 // find cause of t
187
m = c.getDeclaredMethod( JDK14_GC_METHOD,
188                     JDK14_GC_METHOD_ARGS );
189                 t = (Throwable JavaDoc)m.invoke( t, null );
190             }
191             
192             Object JavaDoc[] o = new Object JavaDoc[ trace.size() ];
193             trace.copyInto( o );
194             this.traceList = o;
195             this.traceIndex = 0;
196             
197             //LOG.info("Parsing: using JDK 1.4 method.");
198
}
199         catch (ThreadDeath JavaDoc td)
200         {
201             // don't ever gulp these
202
throw td;
203         }
204         catch (Throwable JavaDoc th)
205         {
206             // use JDK 1.1+ compatible method
207
parseThrowableBuffer( t );
208         }
209     }
210     
211     
212     protected void parseThrowableBuffer( Throwable JavaDoc t )
213     {
214         //LOG.info("Parsing: reverted to JDK 1.1 compatible method.");
215

216         StringWriter JavaDoc sw = new StringWriter JavaDoc();
217         t.printStackTrace( new PrintWriter JavaDoc( sw ) );
218         StringReader JavaDoc sr = new StringReader JavaDoc( sw.toString() );
219         this.traceReader = new BufferedReader JavaDoc( sr );
220     }
221 }
222
223
Popular Tags