KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > exceptions > driver > DriverSQLException


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2005 Emic Networks
4  * Contact: c-jdbc@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  * Initial developer(s): Marc Herbert
21  * Contributor(s): ______________________.
22  */

23
24 package org.objectweb.cjdbc.common.exceptions.driver;
25
26 import java.io.PrintStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import org.objectweb.cjdbc.common.exceptions.driver.protocol.SerializableException;
31
32 /**
33  * This class customizes SQLException. Since JDBC allows only SQLExceptions, it
34  * is used to systematically wrap underlying exceptions (typically coming from
35  * the controller). The main feature added to SQLException is to override
36  * printStackTrace() methods so they also print the non-standard, serializable
37  * stack traces of SerializableException coming from the controller. Another
38  * feature is to provide constructors with a "cause" (chaining), avoiding the
39  * use of initCause()
40  */

41 /**
42  * FIXME: this class relies on "multiple dispatch", which does not exist in Java
43  * (doh!). The current workaround it to cast properly at each call site. The
44  * definitive fix is to use instanceof.
45  */

46 public class DriverSQLException extends SQLException JavaDoc
47 {
48   /**
49    * @see SQLException#SQLException()
50    */

51   public DriverSQLException()
52   {
53     super();
54   }
55
56   /**
57    * @see SQLException#SQLException(java.lang.String)
58    */

59   public DriverSQLException(String JavaDoc reason)
60   {
61     super(reason);
62   }
63
64   /**
65    * @see SQLException#SQLException(java.lang.String, java.lang.String)
66    */

67   public DriverSQLException(String JavaDoc reason, String JavaDoc sQLState)
68   {
69     super(reason, sQLState);
70   }
71
72   /**
73    * @see SQLException#SQLException(java.lang.String, java.lang.String, int)
74    */

75   public DriverSQLException(String JavaDoc reason, String JavaDoc sQLState, int vendorCode)
76   {
77     super(reason, sQLState, vendorCode);
78   }
79
80   /**
81    * Creates a new <code>DriverSQLException</code> around a
82    * SerializableException received from controller, itself converted from an
83    * SQLException in most cases. So we set SQLState and vendorCode.
84    *
85    * @param message message
86    * @param cause exception from controller to wrap
87    */

88   public DriverSQLException(String JavaDoc message, SerializableException cause)
89   {
90     super(message, cause.getSQLState(), cause.getErrorCode());
91     initCause(cause);
92   }
93
94   /**
95    * Missing message constructor: let's borrow message from cause.
96    *
97    * @param cause exception to wrap
98    */

99   public DriverSQLException(SerializableException cause)
100   {
101     this("Message of cause: " + cause.getLocalizedMessage(), cause);
102   }
103
104   /**
105    * Missing message constructor: let's borrow message from cause.
106    *
107    * @param cause exception to wrap
108    */

109   public DriverSQLException(Exception JavaDoc cause)
110   {
111     /**
112      * @see #DriverSQLException(String, SerializableException)
113      * @see #DriverSQLException(String, Exception)
114      */

115     this("Message of cause: " + cause.getLocalizedMessage(), cause);
116   }
117
118   /**
119    * Creates a new <code>DriverSQLException</code> around an exception of a
120    * type not specifically handled elsewhere. Typically used for exceptions
121    * internal to the driver.
122    *
123    * @param message message
124    * @param cause generic exception to wrap
125    */

126   public DriverSQLException(String JavaDoc message, Exception JavaDoc cause)
127   {
128     super(message);
129     initCause(cause);
130   }
131
132   /**
133    * @see #DriverSQLException(String, SQLException)
134    * @deprecated
135    */

136   public DriverSQLException(SQLException JavaDoc cause)
137   {
138     this("", cause);
139   }
140
141   /**
142    * An SQLException should not be wrapped inside a DriverSQLException: this is
143    * a symptom of mixing different layers.
144    *
145    * @param message message
146    * @param cause cause
147    * @deprecated
148    * @throws IllegalArgumentException always
149    */

150   public DriverSQLException(String JavaDoc message, SQLException JavaDoc cause)
151       throws IllegalArgumentException JavaDoc
152   {
153     // ok let's be tolerant for the moment
154
super(message);
155     initCause(cause);
156
157     // TODO: ... but this is the future:
158
// A (Driver-)SQLException should be created here and nowhere below
159

160     // IllegalArgumentException iae = new IllegalArgumentException(
161
// "Bug: cause of a DriverSQLException should not itself be an SQLException
162
// "
163
// + message);
164
// iae.initCause(cause);
165
// throw iae;
166
}
167
168   /**
169    * Overrides super method so we print the serializable stack trace of next
170    * exceptions in the chain (if they use our serializable stack trace)
171    *
172    * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
173    */

174   public void printStackTrace(PrintStream JavaDoc s)
175   {
176     /*
177      * super does unfortunately not call printStackTrace() recursively on the
178      * chain: instead it breaks object encapsulation by calling instead printing
179      * methods and private fields on nexts. And since our chain uses its own
180      * private stack trace implementation (because of JDK 1.4 woes) this does
181      * print nothing in the end.
182      */

183     super.printStackTrace(s);
184
185     // So we have to call printStackStrace() ourselves.
186
Throwable JavaDoc cause = getCause();
187     if (null != cause && cause instanceof SerializableException)
188     {
189       s.println("SerializableStackTrace of each cause:");
190       ((SerializableException) cause).printStackTrace(s);
191     }
192   }
193
194   /**
195    * Overrides super method so we print the serializable stack trace of next
196    * exceptions in the chain (if they use our serializable stack trace)
197    *
198    * @see java.lang.Throwable#printStackTrace()
199    */

200   public void printStackTrace()
201   {
202     /**
203      * This comes back to
204      *
205      * @see DriverSQLException#printStackTrace(PrintStream)
206      */

207     super.printStackTrace();
208
209   }
210
211   /**
212    * Overrides super method so we print the serializable stack trace of next
213    * exceptions in the chain (if they use our serializable stack trace)
214    *
215    * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
216    */

217   public void printStackTrace(PrintWriter JavaDoc s)
218   {
219     /** @see #printStackTrace(PrintStream) */
220     super.printStackTrace(s);
221
222     Throwable JavaDoc cause = getCause();
223     if (null != cause && cause instanceof SerializableException)
224     {
225       s.println("SerializableStackTrace of each cause:");
226       ((SerializableException) cause).printStackTrace(s);
227     }
228   }
229
230 }
231
Popular Tags