001: /**********************************************************************
002: Copyright (c) 2006 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.exceptions;
018:
019: /**
020: * Base exception thrown by JPOX.
021: *
022: * @version $Revision: 1.2 $
023: */
024: public class JPOXException extends RuntimeException {
025: /** Array of nested Throwables (optional) */
026: Throwable[] nested;
027:
028: /** The object being processed when the error was encountered (optional). */
029: Object failed;
030:
031: /** Flag defining if this exception is fatal, or could be retried with the possibility of success. */
032: boolean fatal;
033:
034: /**
035: * Constructs a new exception without a detail message.
036: */
037: public JPOXException() {
038: }
039:
040: /**
041: * Constructs a new exception with the specified detail message.
042: * @param msg the detail message.
043: */
044: public JPOXException(String msg) {
045: super (msg);
046: }
047:
048: /**
049: * Constructs a new exception with the specified detail message and nested <code>Throwable</code>s.
050: * @param msg the detail message.
051: * @param nested the nested <code>Throwable[]</code>.
052: */
053: public JPOXException(String msg, Throwable[] nested) {
054: super (msg);
055: this .nested = nested;
056: }
057:
058: /**
059: * Constructs a new exception with the specified detail message and nested <code>Throwable</code>.
060: * @param msg the detail message.
061: * @param nested the nested <code>Throwable</code>.
062: */
063: public JPOXException(String msg, Throwable nested) {
064: super (msg);
065: this .nested = new Throwable[] { nested };
066: }
067:
068: /**
069: * Constructs a new exception with the specified detail message and failed object.
070: * @param msg the detail message.
071: * @param failed the failed object.
072: */
073: public JPOXException(String msg, Object failed) {
074: super (msg);
075: this .failed = failed;
076: }
077:
078: /**
079: * Constructs a new exception with the specified detail
080: * message, nested <code>Throwable</code>s, and failed object.
081: * @param msg the detail message.
082: * @param nested the nested <code>Throwable[]</code>.
083: * @param failed the failed object.
084: */
085: public JPOXException(String msg, Throwable[] nested, Object failed) {
086: super (msg);
087: this .nested = nested;
088: this .failed = failed;
089: }
090:
091: /**
092: * Constructs a new exception with the specified detail message, nested <code>Throwable</code>,
093: * and failed object.
094: * @param msg the detail message.
095: * @param nested the nested <code>Throwable</code>.
096: * @param failed the failed object.
097: */
098: public JPOXException(String msg, Throwable nested, Object failed) {
099: super (msg);
100: this .nested = new Throwable[] { nested };
101: this .failed = failed;
102: }
103:
104: /**
105: * Method to set the exception as being fatal.
106: * Returns the exception so that user code can call
107: * "throw new JPOXException(...).setFatal();"
108: * @return This exception (for convenience)
109: */
110: public JPOXException setFatal() {
111: fatal = true;
112: return this ;
113: }
114:
115: /**
116: * Accessor for whether the exception is fatal, or retriable.
117: * @return Whether it is fatal
118: */
119: public boolean isFatal() {
120: return fatal;
121: }
122:
123: /**
124: * The exception may include a failed object.
125: * @return the failed object.
126: */
127: public Object getFailedObject() {
128: return failed;
129: }
130:
131: /**
132: * The exception may have been caused by multiple exceptions in the runtime.
133: * If multiple objects caused the problem, each failed object will have its
134: * own <code>Exception</code>.
135: * @return the nested Throwable array.
136: */
137: public Throwable[] getNestedExceptions() {
138: return nested;
139: }
140:
141: /**
142: * Return the first nested exception (if any), otherwise null.
143: * @return the first or only nested Throwable.
144: */
145: public synchronized Throwable getCause() {
146: return ((nested == null || nested.length == 0) ? null
147: : nested[0]);
148: }
149:
150: /**
151: * Prints this <code>Exception</code> and its backtrace to the standard
152: * error output. Print nested Throwables' stack trace as well.
153: */
154: public void printStackTrace() {
155: printStackTrace(System.err);
156: }
157:
158: /**
159: * Prints this <code>Exception</code> and its backtrace to the
160: * specified print stream. Print nested Throwables' stack trace as well.
161: * @param s <code>PrintStream</code> to use for output
162: */
163: public synchronized void printStackTrace(java.io.PrintStream s) {
164: int len = nested == null ? 0 : nested.length;
165: synchronized (s) {
166: if (getMessage() != null) {
167: s.println(getMessage());
168: }
169: super .printStackTrace(s);
170: if (len > 0) {
171: s.println("Nested Throwables StackTrace:");
172: for (int i = 0; i < len; ++i) {
173: Throwable exception = nested[i];
174: if (exception != null) {
175: exception.printStackTrace(s);
176: }
177: }
178: }
179: }
180: }
181:
182: /**
183: * Prints this <code>Exception</code> and its backtrace to the
184: * specified print writer. Print nested Throwables' stack trace as well.
185: * @param s <code>PrintWriter</code> to use for output
186: */
187: public synchronized void printStackTrace(java.io.PrintWriter s) {
188: int len = nested == null ? 0 : nested.length;
189: synchronized (s) {
190: if (getMessage() != null) {
191: s.println(getMessage());
192: }
193: super .printStackTrace(s);
194: if (len > 0) {
195: s.println("Nested Throwables StackTrace:");
196: for (int i = 0; i < len; ++i) {
197: Throwable exception = nested[i];
198: if (exception != null) {
199: exception.printStackTrace(s);
200: }
201: }
202: }
203: }
204: }
205:
206: }
|