KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > exceptions > SQLExceptionFactory


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

22
23 package org.continuent.sequoia.common.exceptions;
24
25 import java.sql.SQLException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * This class defines a SQLExceptionFactory
31  *
32  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
33  * @version 1.0
34  */

35 public class SQLExceptionFactory
36 {
37
38   /**
39    * creates a new SQLException with the sequoiaMessage
40    *
41    * @param sqlEx the original exception
42    * @param sequoiaMessage the sequoia message to use for the new sqlexception
43    * @return a new SQLException
44    */

45   public static SQLException JavaDoc getSQLException(SQLException JavaDoc sqlEx,
46       String JavaDoc sequoiaMessage)
47   {
48     SQLException JavaDoc newException = new SQLException JavaDoc(sequoiaMessage, sqlEx
49         .getSQLState(), sqlEx.getErrorCode());
50     // TODO: shouldn't we use the new initCause() standard chaining instead ?
51
// if we move to a new "SyntheticSQLException" type we will have to do
52
// it anyway.
53
// See also same issue below.
54
newException.setNextException(sqlEx);
55     return newException;
56   }
57
58   /**
59    * creates a new SQLException with the sequoiaMessage, if all exceptions in
60    * the list have the same errorcode and sqlstate the returned SQLExcepion will
61    * be constructed with this values otherwise with null and 0
62    *
63    * @param exceptions list of exceptions
64    * @param sequoiaMessage the Sequoia message
65    * @return a new SQLException
66    */

67   public static SQLException JavaDoc getSQLException(List JavaDoc exceptions,
68       String JavaDoc sequoiaMessage)
69   {
70     String JavaDoc sqlState = null;
71     int errorCode = 0;
72     for (int i = 0; i < exceptions.size(); i++)
73     {
74       SQLException JavaDoc ex = (SQLException JavaDoc) exceptions.get(i);
75       if (ex == null)
76         continue;
77       sequoiaMessage += ex.getMessage() + "\n";
78       if (sqlState == null)
79       {
80         // first exception
81
sqlState = ex.getSQLState();
82         errorCode = ex.getErrorCode();
83       }
84       // We ignore if backend reports different SQL states or error codes. We
85
// report the error of the first backend that failed. Details can be
86
// retrieved for each backend in the exception chaining below.
87
}
88     SQLException JavaDoc newHead = new SQLException JavaDoc(sequoiaMessage, sqlState, errorCode);
89     Iterator JavaDoc exIter = exceptions.iterator();
90
91     // TODO: shouldn't we use the new initCause() standard chaining instead ?
92
// See more comments above.
93
while (exIter.hasNext())
94       newHead.setNextException((SQLException JavaDoc) exIter.next());
95
96     return newHead;
97   }
98
99 }
100
Popular Tags