00001 /** 00002 * C-JDBC: Clustered JDBC. 00003 * Copyright (C) 2002-2004 French National Institute For Research In Computer 00004 * Science And Control (INRIA). 00005 * Contact: c-jdbc@objectweb.org 00006 * 00007 * This library is free software; you can redistribute it and/or modify it 00008 * under the terms of the GNU Lesser General Public License as published by the 00009 * Free Software Foundation; either version 2.1 of the License, or any later 00010 * version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00015 * for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public License 00018 * along with this library; if not, write to the Free Software Foundation, 00019 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00020 * 00021 * Initial developer(s): Marc Wick. 00022 * Contributor(s): ______________________. 00023 */ 00024 00025 package org.objectweb.cjdbc.common.exceptions; 00026 00027 import java.sql.SQLException; 00028 import java.util.List; 00029 00030 /** 00031 * This class defines a SQLExceptionFactory 00032 * 00033 * @author Marc Wick 00034 * @version 1.0 00035 */ 00036 public class SQLExceptionFactory 00037 { 00038 00039 /** 00040 * creates a new SQLException with the cjdbcMessage 00041 * 00042 * @param sqlEx the original exception 00043 * @param cjdbcMessage the cjdbc message to use for the new sqlexception 00044 * @return a new SQLException 00045 */ 00046 public static SQLException getSQLException(SQLException sqlEx, 00047 String cjdbcMessage) 00048 { 00049 SQLException newException = new SQLException(cjdbcMessage, sqlEx 00050 .getSQLState(), sqlEx.getErrorCode()); 00051 newException.setNextException(sqlEx); 00052 return newException; 00053 } 00054 00055 /** 00056 * creates a new SQLException with the cjdbcMessage, if all exceptions in the 00057 * list have the same errorcode and sqlstate the returned SQLExcepion will be 00058 * constructed with this values otherwise with null and 0 00059 * 00060 * @param exceptions list of exceptions 00061 * @param cjdbcMessage the cjdbc message 00062 * @return a new SQLException 00063 */ 00064 public static SQLException getSQLException(List exceptions, 00065 String cjdbcMessage) 00066 { 00067 String sqlState = null; 00068 int errorCode = 0; 00069 for (int i = 0; i < exceptions.size(); i++) 00070 { 00071 SQLException ex = (SQLException) exceptions.get(i); 00072 cjdbcMessage += ex.getMessage() + "\n"; 00073 if (i == 0) 00074 { 00075 //first exception 00076 sqlState = ex.getSQLState(); 00077 errorCode = ex.getErrorCode(); 00078 } 00079 else 00080 { 00081 //make sure sqlState is the same for all backends 00082 if (sqlState != null && !sqlState.equals(ex.getSQLState())) 00083 sqlState = null; 00084 //make sure the error code is the same for all backends 00085 if (errorCode != ex.getErrorCode()) 00086 errorCode = 0; 00087 } 00088 } 00089 return new SQLException(cjdbcMessage, sqlState, errorCode); 00090 } 00091 00092 }