001: /*
002: * $RCSfile: ImagingException.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:57 $
010: * $State: Exp $
011: */
012: package javax.media.jai.util;
013:
014: import java.lang.reflect.InvocationTargetException;
015: import java.lang.reflect.Method;
016: import java.io.PrintStream;
017: import java.io.PrintWriter;
018: import java.security.PrivilegedActionException;
019:
020: /**
021: * This class is designed to contain information of the (abnormal)
022: * situations that happen in JAI and the operations plugged into JAI.
023: * The behavior of this class imitates the <code>Exception</code> class
024: * in Java<sup><font size="-2">TM</font></sup> 2 Platform version 1.4
025: * to define a chained exception and is call-compatible
026: * with Java<sup><font size="-2">TM</font></sup> 2 Platform version 1.4:
027: * The cause can be stored and retrieved from the
028: * instance of this class. Also, the root cause for an instance
029: * can be retrieved.
030: *
031: * @since JAI 1.1.2
032: */
033: public class ImagingException extends RuntimeException {
034: /** The cached cause. */
035: private Throwable cause = null;
036:
037: /** The default constructor. */
038: public ImagingException() {
039: super ();
040: }
041:
042: /**
043: * The constructor to accept the message that describes the situation.
044: *
045: * @param message The message to describe the situation.
046: */
047: public ImagingException(String message) {
048: super (message);
049: }
050:
051: /**
052: * The constructor to accept the cause of this <code>ImagingException</code>.
053: *
054: * @param cause The cause of this <code>ImagingException</code>.
055: */
056: public ImagingException(Throwable cause) {
057: super ();
058: this .cause = cause;
059: }
060:
061: /**
062: * The constructor to accept the cause of this <code>ImagingException</code>
063: * and the message that describes the situation.
064: *
065: * @param message The message that describes the situation.
066: * @param cause The cause of this <code>ImagingException</code>
067: */
068: public ImagingException(String message, Throwable cause) {
069: super (message);
070: this .cause = cause;
071: }
072:
073: /**
074: * Returns the cause of this <code>ImagingException</code>.
075: */
076: public Throwable getCause() {
077: return cause;
078: }
079:
080: /**
081: * Recursively retrieves the root cause of this
082: * <code>ImagingException</code>.
083: */
084: public Throwable getRootCause() {
085: Throwable rootCause = cause;
086: Throwable atop = this ;
087:
088: while (rootCause != atop && rootCause != null) {
089: try {
090: atop = rootCause;
091: Method getCause = rootCause.getClass().getMethod(
092: "getCause", null);
093: rootCause = (Throwable) getCause
094: .invoke(rootCause, null);
095: } catch (Exception e) {
096: // do nothing. This happens (1) getCause method is not defined.
097: // (2) Reflection error. So rootCause will be the same as
098: // atop. Then stop the loop.
099: if (rootCause instanceof InvocationTargetException)
100: rootCause = ((InvocationTargetException) rootCause)
101: .getTargetException();
102: else if (rootCause instanceof PrivilegedActionException)
103: rootCause = ((PrivilegedActionException) rootCause)
104: .getException();
105: else
106: rootCause = atop;
107: } finally {
108: if (rootCause == null)
109: rootCause = atop;
110: }
111: }
112:
113: return rootCause;
114: }
115:
116: /**
117: * Prints the stack trace. For Java<sup><font size="-2">TM</font></sup> 2
118: * Platform version 1.4 and up, calls the same
119: * method in the super class. For Java<sup><font size="-2">TM</font></sup>
120: * 2 Platform version 1.3, prints the stack trace of
121: * this <code>ImagingException</code> and the trace of its cause.
122: */
123: public void printStackTrace() {
124: printStackTrace(System.err);
125: }
126:
127: /**
128: * Prints the stack trace. For Java<sup><font size="-2">TM</font></sup>
129: * 2 Platform version 1.4 and up, calls the same
130: * method in the super class. For Java<sup><font size="-2">TM</font></sup>
131: * 2 Platform version 1.4, prints the stack trace of
132: * this <code>ImagingException</code> and the trace of its cause.
133: */
134: public void printStackTrace(PrintStream s) {
135:
136: synchronized (s) {
137: super .printStackTrace(s);
138: boolean is14 = false;
139: try {
140: String version = System.getProperty("java.version");
141: is14 = version.indexOf("1.4") >= 0;
142: } catch (Exception e) {
143: }
144:
145: if (!is14 && cause != null) {
146: s.println("Caused by:");
147: cause.printStackTrace(s);
148: }
149:
150: }
151: }
152:
153: /**
154: * Prints the stack trace. For Java<sup><font size="-2">TM</font></sup> 2
155: * Platform version 1.4 and up, calls the same
156: * method in the super class. For Java<sup><font size="-2">TM</font></sup>
157: * 2 Platform version 1.43, prints the stack trace of
158: * this <code>ImagingException</code> and the trace of its cause.
159: */
160:
161: public void printStackTrace(PrintWriter s) {
162: synchronized (s) {
163: super .printStackTrace(s);
164: boolean is14 = false;
165: try {
166: String version = System.getProperty("java.version");
167: is14 = version.indexOf("1.4") >= 0;
168: } catch (Exception e) {
169: }
170:
171: if (!is14 && cause != null) {
172: s.println("Caused by:");
173: cause.printStackTrace(s);
174: }
175: }
176: }
177: /*
178: public static void main(String[] args) {
179: Exception ce = new ImagingException(new RuntimeException("test"));
180: Throwable cause = ce.getCause();
181: System.out.println("Cause: " + cause);
182: System.out.println("Root cause: " + ((ImagingException)ce).getRootCause());
183: ce.printStackTrace();
184: }
185: */
186: }
|