1 package org.apache.torque;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.PrintStream;
58 import java.io.PrintWriter;
59 import java.io.StringWriter;
60 import java.util.LinkedList;
61 import java.util.StringTokenizer;
62
63 /***
64 * This is a base class of runtime exeptions thrown by Torque. <p>
65 *
66 * This class represents a non-checked type exception (see
67 * {@link java.lang.RuntimeException}).
68 * It is intended to ease the debugging by carrying on the information about the
69 * exception which was caught and provoked throwing the current exception.
70 * Catching and rethrowing may occur multiple times, and provided that all
71 * exceptions except the first one are descendands of
72 * <code>TorqueRuntimeException</code>, when the exception is finally printed
73 * out using any of the <code>printStackTrace()</code> methods, the stacktrace
74 * will contain the information about all exceptions thrown and caught on the
75 * way.
76 *
77 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
78 * @version $Id: TorqueRuntimeException.java,v 1.7 2002/09/13 06:09:09 mpoeschl Exp $
79 */
80 public class TorqueRuntimeException
81 extends RuntimeException
82 {
83 /***
84 * Holds the reference to the exception or error that caused
85 * this exception to be thrown.
86 */
87 private Throwable nested = null;
88
89 /***
90 * Constructs a new <code>TorqueRuntimeException</code> without specified
91 * detail message.
92 */
93 public TorqueRuntimeException()
94 {
95 super();
96 }
97
98 /***
99 * Constructs a new <code>TorqueRuntimeException</code> with specified
100 * detail message.
101 *
102 * @param msg the error message.
103 */
104 public TorqueRuntimeException(String msg)
105 {
106 super(msg);
107 }
108
109 /***
110 * Constructs a new <code>TorqueRuntimeException</code> with specified
111 * nested <code>Throwable</code>.
112 *
113 * @param nested the exception or error that caused this exception
114 * to be thrown.
115 */
116 public TorqueRuntimeException(Throwable nested)
117 {
118 super();
119 this.nested = nested;
120 }
121
122 /***
123 * Constructs a new <code>TorqueRuntimeException</code> with specified
124 * detail message and nested <code>Throwable</code>.
125 *
126 * @param msg the error message.
127 * @param nested the exception or error that caused this exception
128 * to be thrown.
129 */
130 public TorqueRuntimeException(String msg, Throwable nested)
131 {
132 super(msg);
133 this.nested = nested;
134 }
135
136 /***
137 * Prints the stack trace of this exception the the standar error stream.
138 */
139 public void printStackTrace()
140 {
141 synchronized (System.err)
142 {
143 printStackTrace(System.err);
144 }
145 }
146
147 /***
148 * Prints the stack trace of this exception to the specified print stream.
149 *
150 * @param out <code>PrintStream</code> to use for output
151 */
152 public void printStackTrace(PrintStream out)
153 {
154 synchronized (out)
155 {
156 PrintWriter pw = new PrintWriter(out, false);
157 printStackTrace(pw);
158 // flush the PrintWriter before it's GCed
159 pw.flush();
160 }
161 }
162
163 /***
164 * Prints the stack trace of this exception to the specified print writer.
165 *
166 * @param out <code>PrintWriter</code> to use for output.
167 */
168 public void printStackTrace(PrintWriter out)
169 {
170 synchronized (out)
171 {
172 printStackTrace(out, 0);
173 }
174 }
175
176 /***
177 * Prints the stack trace of this exception skiping a specified number
178 * of stack frames.
179 *
180 * @param out <code>PrintWriter</code> to use for output.
181 * @param skip the numbere of stack frames to skip.
182 */
183 public void printStackTrace(PrintWriter out, int skip)
184 {
185 String[] st = captureStackTrace();
186 if (nested != null)
187 {
188 if (nested instanceof TorqueRuntimeException)
189 {
190 ((TorqueRuntimeException) nested)
191 .printStackTrace(out, st.length - 2);
192 }
193 else if (nested instanceof TorqueException)
194 {
195 ((TorqueException) nested).printStackTrace(out);
196 }
197 else
198 {
199 String[] nst = captureStackTrace(nested);
200 for (int i = 0; i < nst.length - st.length + 2; i++)
201 {
202 out.println(nst[i]);
203 }
204 }
205 out.print("rethrown as ");
206 }
207 for (int i = 0; i < st.length - skip; i++)
208 {
209 out.println(st[i]);
210 }
211 }
212
213 /***
214 * Captures the stack trace associated with this exception.
215 *
216 * @return an array of Strings describing stack frames.
217 */
218 private String[] captureStackTrace()
219 {
220 StringWriter sw = new StringWriter();
221 super.printStackTrace(new PrintWriter(sw, true));
222 return splitStackTrace(sw.getBuffer().toString());
223 }
224
225 /***
226 * Captures the stack trace associated with a <code>Throwable</code>
227 * object.
228 *
229 * @param t the <code>Throwable</code>.
230 * @return an array of Strings describing stack frames.
231 */
232 private String[] captureStackTrace(Throwable t)
233 {
234 StringWriter sw = new StringWriter();
235 t.printStackTrace(new PrintWriter(sw, true));
236 return splitStackTrace(sw.getBuffer().toString());
237 }
238
239 /***
240 * Splits the stack trace given as a newline separated string
241 * into an array of stack frames.
242 *
243 * @param stackTrace the stack trace.
244 * @return an array of Strings describing stack frames.
245 */
246 private String[] splitStackTrace(String stackTrace)
247 {
248 String linebreak = System.getProperty("line.separator");
249 StringTokenizer st = new StringTokenizer(stackTrace, linebreak);
250 LinkedList list = new LinkedList();
251 while (st.hasMoreTokens())
252 {
253 list.add(st.nextToken());
254 }
255 return (String[]) list.toArray(new String[] {});
256 }
257 }
This page was automatically generated by Maven