001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent, Inc.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Emmanuel Cecchet
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.common.exceptions.driver;
021:
022: import java.io.PrintStream;
023: import java.io.PrintWriter;
024: import java.sql.SQLException;
025:
026: import org.continuent.sequoia.common.exceptions.driver.protocol.SerializableException;
027:
028: /**
029: * This class customizes SQLException and is used for reporting internal IO
030: * exception catched by the driver.
031: */
032: public class DriverIOException extends SQLException {
033: private static final long serialVersionUID = 5066115114622251828L;
034:
035: /**
036: * @see SQLException#SQLException()
037: */
038: public DriverIOException() {
039: super ();
040: }
041:
042: /**
043: * @see SQLException#SQLException(java.lang.String)
044: */
045: public DriverIOException(String reason) {
046: super (reason);
047: }
048:
049: /**
050: * @see SQLException#SQLException(java.lang.String, java.lang.String)
051: */
052: public DriverIOException(String reason, String sQLState) {
053: super (reason, sQLState);
054: }
055:
056: /**
057: * @see SQLException#SQLException(java.lang.String, java.lang.String, int)
058: */
059: public DriverIOException(String reason, String sQLState,
060: int vendorCode) {
061: super (reason, sQLState, vendorCode);
062: }
063:
064: /**
065: * Creates a new <code>DriverIOException</code> around a
066: * SerializableException received from controller, itself converted from an
067: * SQLException in most cases. So we set SQLState and vendorCode.
068: *
069: * @param message message
070: * @param cause exception from controller to wrap
071: */
072: public DriverIOException(String message, SerializableException cause) {
073: super (message, cause.getSQLState(), cause.getErrorCode());
074: initCause(cause);
075: }
076:
077: /**
078: * Missing message constructor: let's borrow message from cause.
079: *
080: * @param cause exception to wrap
081: */
082: public DriverIOException(SerializableException cause) {
083: this ("Message of cause: " + cause.getLocalizedMessage(), cause);
084: }
085:
086: /**
087: * Missing message constructor: let's borrow message from cause.
088: *
089: * @param cause exception to wrap
090: */
091: public DriverIOException(Exception cause) {
092: /**
093: * @see #DriverIOException(String, SerializableException)
094: * @see #DriverIOException(String, Exception)
095: */
096: this ("Message of cause: " + cause.getLocalizedMessage(), cause);
097: }
098:
099: /**
100: * Creates a new <code>DriverIOException</code> around an exception of a
101: * type not specifically handled elsewhere. Typically used for exceptions
102: * internal to the driver.
103: *
104: * @param message message
105: * @param cause generic exception to wrap
106: */
107: public DriverIOException(String message, Exception cause) {
108: super (message);
109: initCause(cause);
110: }
111:
112: /**
113: * @see #DriverIOException(String, SQLException)
114: * @deprecated
115: */
116: public DriverIOException(SQLException cause) {
117: this ("", cause);
118: }
119:
120: /**
121: * An SQLException should not be wrapped inside a DriverSQLException: this is
122: * a symptom of mixing different layers.
123: *
124: * @param message message
125: * @param cause cause
126: * @deprecated
127: * @throws IllegalArgumentException always
128: */
129: public DriverIOException(String message, SQLException cause)
130: throws IllegalArgumentException {
131: // ok let's be tolerant for the moment
132: super (message);
133: initCause(cause);
134:
135: // TODO: ... but this is the future:
136: // A (Driver-)SQLException should be created here and nowhere below
137:
138: // IllegalArgumentException iae = new IllegalArgumentException(
139: // "Bug: cause of a DriverSQLException should not itself be an SQLException
140: // "
141: // + message);
142: // iae.initCause(cause);
143: // throw iae;
144: }
145:
146: /**
147: * Overrides super method so we print the serializable stack trace of next
148: * exceptions in the chain (if they use our serializable stack trace)
149: *
150: * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
151: */
152: public void printStackTrace(PrintStream s) {
153: /*
154: * super does unfortunately not call printStackTrace() recursively on the
155: * chain: instead it breaks object encapsulation by calling instead printing
156: * methods and private fields on nexts. And since our chain uses its own
157: * private stack trace implementation (because of JDK 1.4 woes) this does
158: * print nothing in the end.
159: */
160: super .printStackTrace(s);
161:
162: // So we have to call printStackStrace() ourselves.
163: Throwable cause = getCause();
164: if (null != cause && cause instanceof SerializableException) {
165: s.println("SerializableStackTrace of each cause:");
166: ((SerializableException) cause).printStackTrace(s);
167: }
168: }
169:
170: /**
171: * Overrides super method so we print the serializable stack trace of next
172: * exceptions in the chain (if they use our serializable stack trace)
173: *
174: * @see java.lang.Throwable#printStackTrace()
175: */
176: public void printStackTrace() {
177: /**
178: * This comes back to
179: *
180: * @see DriverSQLException#printStackTrace(PrintStream)
181: */
182: super .printStackTrace();
183:
184: }
185:
186: /**
187: * Overrides super method so we print the serializable stack trace of next
188: * exceptions in the chain (if they use our serializable stack trace)
189: *
190: * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
191: */
192: public void printStackTrace(PrintWriter s) {
193: /** @see #printStackTrace(PrintStream) */
194: super .printStackTrace(s);
195:
196: Throwable cause = getCause();
197: if (null != cause && cause instanceof SerializableException) {
198: s.println("SerializableStackTrace of each cause:");
199: ((SerializableException) cause).printStackTrace(s);
200: }
201: }
202:
203: }
|