001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.util;
020:
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.PrintStream;
025: import java.io.PrintWriter;
026: import java.io.Serializable;
027:
028: import org.apache.openjpa.lib.util.Localizer.Message;
029:
030: /**
031: * Exception type for all OpenJPA exceptions. Meant to be easily
032: * transformed into an appropriate exception at the API layer, since most APIs
033: * define their own exception types.
034: *
035: * @author Abe White
036: * @since 0.4.0
037: */
038: public abstract class OpenJPAException extends RuntimeException
039: implements Serializable, ExceptionInfo {
040:
041: private transient boolean _fatal = false;
042: private transient Object _failed = null;
043: private transient Throwable[] _nested = null;
044:
045: /**
046: * Default constructor.
047: */
048: public OpenJPAException() {
049: }
050:
051: /**
052: * Constructor; supply message.
053: */
054: public OpenJPAException(String msg) {
055: super (msg);
056: }
057:
058: /**
059: * Constructor; supply message.
060: */
061: public OpenJPAException(Message msg) {
062: super (msg.getMessage());
063: }
064:
065: /**
066: * Construct with cause.
067: */
068: public OpenJPAException(Throwable cause) {
069: this (cause.getMessage(), cause);
070: }
071:
072: /**
073: * Construct with message and cause.
074: */
075: public OpenJPAException(String msg, Throwable cause) {
076: super (msg);
077: setCause(cause);
078: }
079:
080: /**
081: * Construct with message and cause.
082: */
083: public OpenJPAException(Message msg, Throwable cause) {
084: super (msg.getMessage());
085: setCause(cause);
086: }
087:
088: /**
089: * Exception type.
090: */
091: public abstract int getType();
092:
093: /**
094: * Exception subtype.
095: */
096: public int getSubtype() {
097: return 0;
098: }
099:
100: /**
101: * Whether this error is fatal.
102: */
103: public boolean isFatal() {
104: return _fatal;
105: }
106:
107: /**
108: * Whether this error is fatal.
109: */
110: public OpenJPAException setFatal(boolean fatal) {
111: _fatal = fatal;
112: return this ;
113: }
114:
115: /**
116: * Returns the first {@link Throwable} from {@link #getNestedThrowables}
117: * in order to conform to {@link Throwable#getCause} in Java 1.4+.
118: *
119: * @see Throwable#getCause
120: */
121: public Throwable getCause() {
122: if (_nested == null || _nested.length == 0)
123: return null;
124: else
125: return _nested[0];
126: }
127:
128: /**
129: * The first nested throwable.
130: */
131: public OpenJPAException setCause(Throwable nested) {
132: if (_nested != null)
133: throw new IllegalStateException();
134: if (nested != null)
135: _nested = new Throwable[] { nested };
136: return this ;
137: }
138:
139: /**
140: * The nested throwables.
141: */
142: public Throwable[] getNestedThrowables() {
143: return (_nested == null) ? Exceptions.EMPTY_THROWABLES
144: : _nested;
145: }
146:
147: /**
148: * The nested throwables.
149: */
150: public OpenJPAException setNestedThrowables(Throwable[] nested) {
151: _nested = nested;
152: return this ;
153: }
154:
155: /**
156: * The failed object.
157: */
158: public Object getFailedObject() {
159: return _failed;
160: }
161:
162: /**
163: * The failed object.
164: */
165: public OpenJPAException setFailedObject(Object failed) {
166: _failed = failed;
167: return this ;
168: }
169:
170: public String toString() {
171: return Exceptions.toString(this );
172: }
173:
174: public void printStackTrace() {
175: printStackTrace(System.err);
176: }
177:
178: public void printStackTrace(PrintStream out) {
179: super .printStackTrace(out);
180: Exceptions.printNestedThrowables(this , out);
181: }
182:
183: public void printStackTrace(PrintWriter out) {
184: super .printStackTrace(out);
185: Exceptions.printNestedThrowables(this , out);
186: }
187:
188: private void writeObject(ObjectOutputStream out) throws IOException {
189: out.writeBoolean(_fatal);
190: out.writeObject(Exceptions.replaceFailedObject(_failed));
191: out.writeObject(Exceptions.replaceNestedThrowables(_nested));
192: }
193:
194: private void readObject(ObjectInputStream in) throws IOException,
195: ClassNotFoundException {
196: _fatal = in.readBoolean();
197: _failed = in.readObject();
198: _nested = (Throwable[]) in.readObject();
199: }
200: }
|