001: /*
002: * $Id: IVException.java,v 1.4 2002/03/13 01:46:16 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.util;
052:
053: import java.util.*;
054: import java.io.*;
055:
056: /**
057: * This exception is thrown if there any errors during generation process.
058: * <P>
059: * The exception can be created with message from resource bundles
060: * All messages in this file are defined by their keys which are to be used when throwing the exception.
061: * <p>
062: * Usage:<BR>
063: * <UL>
064: * <LI>wrap some other exception into IVException and rethrow it with a message from resorce and parameters:<br>
065: * <pre><CODE>
066: * ...
067: * } catch( IOException e ) {
068: * throw new IVException( e, Resource.ERRCMDFILEREAD, url.getName(), getCommandName() );
069: * }
070: * </CODE></pre>
071: * <LI><CODE>throw new IVException( Resource.INFINITELOOP );</CODE>
072: * </UL>
073: *
074: * @author Dmitry Skavish
075: */
076: public class IVException extends Exception {
077:
078: private String message;
079: private Throwable cause;
080: private String key;
081: private Object[] parms;
082: private ResourceBundle bundle;
083:
084: public IVException() {
085: }
086:
087: public IVException(String key) {
088: this .key = key;
089: }
090:
091: public IVException(ResourceBundle bundle, String key) {
092: this .key = key;
093: this .bundle = bundle;
094: }
095:
096: public IVException(String key, Object[] parms) {
097: this .key = key;
098: this .parms = parms;
099: }
100:
101: public IVException(ResourceBundle bundle, String key, Object[] parms) {
102: this .key = key;
103: this .parms = parms;
104: this .bundle = bundle;
105: }
106:
107: public IVException(String key, Throwable cause) {
108: this .key = key;
109: this .cause = cause;
110: }
111:
112: public IVException(ResourceBundle bundle, String key,
113: Throwable cause) {
114: this .key = key;
115: this .cause = cause;
116: this .bundle = bundle;
117: }
118:
119: public IVException(String key, Object[] parms, Throwable cause) {
120: this .key = key;
121: this .parms = parms;
122: this .cause = cause;
123: }
124:
125: public IVException(ResourceBundle bundle, String key,
126: Object[] parms, Throwable cause) {
127: this .key = key;
128: this .parms = parms;
129: this .cause = cause;
130: this .bundle = bundle;
131: }
132:
133: public IVException(Throwable cause) {
134: this .cause = cause;
135: }
136:
137: public Throwable getCause() {
138: return cause;
139: }
140:
141: public String getMessageKey() {
142: return key;
143: }
144:
145: public String getMessage() {
146: if (message == null) {
147: String ourMsg = bundle == null ? Log.getMessage(key, parms)
148: : Log.getMessage(bundle, key, parms);
149: StringBuffer msg = new StringBuffer();
150: if (ourMsg != null) {
151: msg.append(ourMsg);
152: }
153: if (getCause() != null) {
154: String causeMsg = getCause().getMessage();
155: if (causeMsg != null) {
156: if (ourMsg != null) {
157: msg.append("\n\t-> ");
158: }
159: msg.append(causeMsg);
160: }
161: }
162: message = msg.toString();
163: }
164: return message;
165: }
166:
167: /**
168: * Prints this throwable and its backtrace to the specified print stream.
169: *
170: * @param s <code>PrintStream</code> to use for output
171: */
172: public void printStackTrace(PrintStream s) {
173: if (Util.javaVersion >= 1.4)
174: super .printStackTrace(s);
175: else {
176: synchronized (s) {
177: s.println(this );
178: IVVector trace = getStackTrace(this );
179: for (int i = 0; i < trace.size(); i++)
180: s.println(trace.elementAt(i));
181:
182: Throwable ourCause = getCause();
183: if (ourCause != null)
184: printStackTraceAsCause(ourCause, s, trace);
185: }
186: }
187: }
188:
189: /**
190: * Print our stack trace as a cause for the specified stack trace.
191: */
192: private static void printStackTraceAsCause(Throwable t,
193: PrintStream s, IVVector causedTrace) {
194:
195: // Compute number of frames in common between this and caused
196: IVVector trace = getStackTrace(t);
197: int m = trace.size() - 1, n = causedTrace.size() - 1;
198: while (m >= 0 && n >= 0
199: && trace.elementAt(m).equals(causedTrace.elementAt(n))) {
200: m--;
201: n--;
202: }
203: int framesInCommon = trace.size() - 1 - m;
204:
205: s.println("caused by: " + t);
206: for (int i = 0; i <= m; i++)
207: s.println(trace.elementAt(i));
208: if (framesInCommon != 0)
209: s.println("\t... " + framesInCommon + " more");
210:
211: // Recurse if we have a cause
212: if (t instanceof IVException) {
213: Throwable ourCause = ((IVException) t).getCause();
214: if (ourCause != null)
215: printStackTraceAsCause(ourCause, s, trace);
216: }
217: }
218:
219: /**
220: * Prints this throwable and its backtrace to the specified
221: * print writer.
222: *
223: * @param s <code>PrintWriter</code> to use for output
224: * @since JDK1.1
225: */
226: public void printStackTrace(PrintWriter s) {
227: if (Util.javaVersion >= 1.4)
228: super .printStackTrace(s);
229: else {
230: synchronized (s) {
231: s.println(this );
232: IVVector trace = getStackTrace(this );
233: for (int i = 0; i < trace.size(); i++)
234: s.println(trace.elementAt(i));
235:
236: Throwable ourCause = getCause();
237: if (ourCause != null)
238: printStackTraceAsCause(ourCause, s, trace);
239: }
240: }
241: }
242:
243: /**
244: * Print our stack trace as a cause for the specified stack trace.
245: */
246: private static void printStackTraceAsCause(Throwable t,
247: PrintWriter s, IVVector causedTrace) {
248:
249: // Compute number of frames in common between this and caused
250: IVVector trace = getStackTrace(t);
251: int m = trace.size() - 1, n = causedTrace.size() - 1;
252: while (m >= 0 && n >= 0
253: && trace.elementAt(m).equals(causedTrace.elementAt(n))) {
254: m--;
255: n--;
256: }
257: int framesInCommon = trace.size() - 1 - m;
258:
259: s.println("caused by: " + t);
260: for (int i = 0; i <= m; i++)
261: s.println(trace.elementAt(i));
262: if (framesInCommon != 0)
263: s.println("\t... " + framesInCommon + " more");
264:
265: // Recurse if we have a cause
266: if (t instanceof IVException) {
267: Throwable ourCause = ((IVException) t).getCause();
268: if (ourCause != null)
269: printStackTraceAsCause(ourCause, s, trace);
270: }
271: }
272:
273: private void super _printStackTrace(PrintWriter pw) {
274: super .printStackTrace(pw);
275: }
276:
277: private static IVVector getStackTrace(Throwable t) {
278: StringWriter sw = new StringWriter();
279: PrintWriter pw = new PrintWriter(sw, true);
280:
281: if (t instanceof IVException) {
282: ((IVException) t).super _printStackTrace(pw);
283: } else {
284: t.printStackTrace(pw);
285: }
286:
287: StringTokenizer st = new StringTokenizer(sw.getBuffer()
288: .toString(), Util.lineSeparator);
289: IVVector v = new IVVector(20);
290: while (st.hasMoreTokens()) {
291: String s = st.nextToken();
292: if (s.startsWith("\tat"))
293: v.addElement(s);
294: }
295: return v;
296: }
297:
298: }
|