KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > exceptions > SQLExceptionFactory


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

24
25 package org.objectweb.cjdbc.common.exceptions;
26
27 import java.sql.SQLException JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * This class defines a SQLExceptionFactory
33  *
34  * @author Marc Wick
35  * @version 1.0
36  */

37 public class SQLExceptionFactory
38 {
39
40   /**
41    * creates a new SQLException with the cjdbcMessage
42    *
43    * @param sqlEx the original exception
44    * @param cjdbcMessage the cjdbc message to use for the new sqlexception
45    * @return a new SQLException
46    */

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

69   public static SQLException JavaDoc getSQLException(List JavaDoc exceptions,
70       String JavaDoc cjdbcMessage)
71   {
72     String JavaDoc sqlState = null;
73     int errorCode = 0;
74     for (int i = 0; i < exceptions.size(); i++)
75     {
76       SQLException JavaDoc ex = (SQLException JavaDoc) exceptions.get(i);
77       cjdbcMessage += ex.getMessage() + "\n";
78       if (i == 0)
79       {
80         //first exception
81
sqlState = ex.getSQLState();
82         errorCode = ex.getErrorCode();
83       }
84       else
85       {
86         //make sure sqlState is the same for all backends
87
if (sqlState != null && !sqlState.equals(ex.getSQLState()))
88           sqlState = null;
89         //make sure the error code is the same for all backends
90
if (errorCode != ex.getErrorCode())
91           errorCode = 0;
92       }
93     }
94     SQLException JavaDoc newHead = new SQLException JavaDoc(cjdbcMessage, sqlState, errorCode);
95     Iterator JavaDoc exIter = exceptions.iterator();
96     
97     // TODO: shouldn't we use the new initCause() standard chaining instead ?
98
// See more comments above.
99
while (exIter.hasNext())
100       newHead.setNextException((SQLException JavaDoc)exIter.next());
101     
102     return newHead;
103   }
104
105 }
106
Popular Tags