KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > exceptions > driver > protocol > BackendDriverException


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.protocol;
25
26 import java.io.IOException JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import org.objectweb.cjdbc.common.stream.CJDBCInputStream;
30
31 /**
32  * This class is an SQLException (typically from backend) made serializable.
33  *
34  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
35  * @version 1.0
36  */

37 public class BackendDriverException
38     extends SerializableException
39 {
40   /**
41    * @see SerializableException#SerializableException(CJDBCInputStream)
42    */

43   public BackendDriverException(CJDBCInputStream in) throws IOException JavaDoc
44   {
45     super(in);
46   }
47
48   /**
49    * Converts a chain of Throwables to a new chain of SerializableException
50    * starting with a <code>BackendDriverException</code>. The returned chain
51    * has the same length. We don't use super's method but re-implement it since
52    * we want to also convert SQLException old-style chaining into a new style
53    * chain. "SyntheticSQLException-s" from
54    * {@link org.objectweb.cjdbc.common.exceptions.SQLExceptionFactory} also
55    * currently use old style chaining (with setNextException).
56    *
57    * @param start head of chain to convert.
58    * @see SerializableException#SerializableException(Throwable)
59    */

60   public BackendDriverException(Throwable JavaDoc start)
61   {
62     super(start.getMessage(), convertNext(start)); // recursion here
63
convertStackTrace(start);
64
65     if (start instanceof SQLException JavaDoc) // hopefully, else why are we here?
66
{
67       SQLException JavaDoc sqlE = (SQLException JavaDoc) start;
68       setSQLState(sqlE.getSQLState());
69       setErrorCode(sqlE.getErrorCode());
70     }
71   }
72
73   /**
74    * Get the first cause found (new or old style), and convert it to a new
75    * BackendDriverException object (which is Serializable)
76    */

77
78   private static SerializableException convertNext(Throwable JavaDoc regularEx)
79   {
80     /*
81      * If we find that the new standard 1.4 chain is used, then we don't even
82      * look at the old SQLException chain.
83      */

84     /*
85      * We could also <em>not</em> lose this information by: adding another
86      * separated chain to this class, serialize both chains, and convert
87      * everything back to SQLExceptions on the driver side so both chains can
88      * separately be offered to the JDBC client...
89      */

90     Throwable JavaDoc newStyleCause = regularEx.getCause();
91     if (null != newStyleCause)
92       return new BackendDriverException(newStyleCause);
93
94     // check legacy style chaining
95
if (regularEx instanceof SQLException JavaDoc)
96     {
97       SQLException JavaDoc nextE = ((SQLException JavaDoc) regularEx).getNextException();
98       if (null != nextE)
99         return new BackendDriverException(nextE);
100     }
101
102     // found no more link, stop condition
103
return null;
104
105   }
106 }
Popular Tags