KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > exception > ErrorCodeConverter


1 // $Id: ErrorCodeConverter.java,v 1.3 2005/04/06 23:46:18 epbernard Exp $
2
package org.hibernate.exception;
3
4 import org.hibernate.JDBCException;
5
6 import java.sql.SQLException JavaDoc;
7
8 /**
9  * A SQLExceptionConverter implementation which performs converion based on
10  * the vendor specific ErrorCode. This is just intended really as just a
11  * base class for converters which know the interpretation of vendor-specific
12  * codes.
13  *
14  * @author Steve Ebersole
15  */

16 public class ErrorCodeConverter implements SQLExceptionConverter {
17
18     private ViolatedConstraintNameExtracter extracter;
19
20     public ErrorCodeConverter(ViolatedConstraintNameExtracter extracter) {
21         this.extracter = extracter;
22     }
23
24     /**
25      * The error codes representing SQL grammar issues.
26      *
27      * @return The SQL grammar error codes.
28      */

29     protected int[] getSQLGrammarErrorCodes() {
30         return null;
31     }
32
33     /**
34      * The error codes representing issues with a connection.
35      *
36      * @return The connection error codes.
37      */

38     protected int[] getConnectionErrorCodes() {
39         return null;
40     }
41
42     /**
43      * The error codes representing various types of database integrity issues.
44      *
45      * @return The integrity violation error codes.
46      */

47     protected int[] getIntegrityViolationErrorCodes() {
48         return null;
49     }
50
51     protected int[] getLockAcquisitionErrorCodes() {
52         return null;
53     }
54
55     /**
56      * Convert the given SQLException into Hibernate's JDBCException hierarchy.
57      *
58      * @param sqlException The SQLException to be converted.
59      * @param message An optional error message.
60      * @param sql Optionally, the sql being performed when the exception occurred.
61      * @return The resulting JDBCException.
62      */

63     public JDBCException convert(SQLException JavaDoc sqlException, String JavaDoc message, String JavaDoc sql) {
64         int errorCode = JDBCExceptionHelper.extractErrorCode( sqlException );
65
66         if ( isMatch( getConnectionErrorCodes(), errorCode ) ) {
67             return new JDBCConnectionException( message, sqlException, sql );
68         }
69         else if ( isMatch( getSQLGrammarErrorCodes(), errorCode ) ) {
70             return new SQLGrammarException( message, sqlException, sql );
71         }
72         else if ( isMatch( getIntegrityViolationErrorCodes(), errorCode ) ) {
73             String JavaDoc constraintName = extracter.extractConstraintName( sqlException );
74             return new ConstraintViolationException( message, sqlException, sql, constraintName );
75         }
76         else if ( isMatch( getLockAcquisitionErrorCodes(), errorCode ) ) {
77             return new LockAcquisitionException( message, sqlException, sql );
78         }
79
80         return handledNonSpecificException( sqlException, message, sql );
81     }
82
83     /**
84      * Handle an exception not converted to a specific type based on the built-in checks.
85      *
86      * @param sqlException The exception to be handled.
87      * @param message An optional message
88      * @param sql Optionally, the sql being performed when the exception occurred.
89      * @return The converted exception; should <b>never</b> be null.
90      */

91     protected JDBCException handledNonSpecificException(SQLException JavaDoc sqlException, String JavaDoc message, String JavaDoc sql) {
92         return new GenericJDBCException( message, sqlException, sql );
93     }
94
95     private boolean isMatch(int[] errorCodes, int errorCode) {
96         if ( errorCodes != null ) {
97             for ( int i = 0, max = errorCodes.length; i < max; i++ ) {
98                 if ( errorCodes[i] == errorCode ) {
99                     return true;
100                 }
101             }
102         }
103         return false;
104     }
105 }
106
Popular Tags