KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > exceptions > driver > protocol > BackendDriverException


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Emic Networks
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Marc Herbert
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.common.exceptions.driver.protocol;
23
24 import java.io.IOException JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 import org.continuent.sequoia.common.stream.DriverBufferedInputStream;
28
29 /**
30  * This class is an SQLException (typically from backend) made serializable.
31  *
32  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
33  * @version 1.0
34  */

35 public class BackendDriverException extends SerializableException
36 {
37   private static final long serialVersionUID = -4044262679874226846L;
38
39   /**
40    * @see SerializableException#SerializableException(DriverBufferedInputStream)
41    */

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

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

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

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

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