KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: SQLExceptionConverterFactory.java,v 1.3 2004/11/21 00:11:27 pgmjsd Exp $
2
package org.hibernate.exception;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.hibernate.HibernateException;
7 import org.hibernate.JDBCException;
8 import org.hibernate.cfg.Environment;
9 import org.hibernate.dialect.Dialect;
10 import org.hibernate.util.ReflectHelper;
11 import org.hibernate.util.StringHelper;
12
13 import java.lang.reflect.Constructor JavaDoc;
14 import java.sql.SQLException JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 /**
18  * A factory for building SQLExceptionConverter instances.
19  *
20  * @author Steve Ebersole
21  */

22 public class SQLExceptionConverterFactory {
23
24     private static final Log log = LogFactory.getLog( SQLExceptionConverterFactory.class );
25
26     private SQLExceptionConverterFactory() {
27         // Private constructor - stops checkstyle from complaining.
28
}
29
30     /**
31      * Build a SQLExceptionConverter instance.
32      * <p/>
33      * First, looks for a {@link Environment.SQL_EXCEPTION_CONVERTER} property to see
34      * if the configuration specified the class of a specific converter to use. If this
35      * property is set, attempt to construct an instance of that class. If not set, or
36      * if construction fails, the converter specific to the dialect will be used.
37      *
38      * @param dialect The defined dialect.
39      * @param properties The configuration properties.
40      * @return An appropriate SQLExceptionConverter instance.
41      * @throws HibernateException There was an error building the SQLExceptionConverter.
42      */

43     public static SQLExceptionConverter buildSQLExceptionConverter(Dialect dialect, Properties JavaDoc properties) throws HibernateException {
44         SQLExceptionConverter converter = null;
45
46         String JavaDoc converterClassName = ( String JavaDoc ) properties.get( Environment.SQL_EXCEPTION_CONVERTER );
47         if ( StringHelper.isNotEmpty( converterClassName ) ) {
48             converter = constructConverter( converterClassName, dialect.getViolatedConstraintNameExtracter() );
49         }
50
51         if ( converter == null ) {
52             log.trace( "Using dialect defined converter" );
53             converter = dialect.buildSQLExceptionConverter();
54         }
55
56         if ( converter instanceof Configurable ) {
57             try {
58                 ( ( Configurable ) converter ).configure( properties );
59             }
60             catch ( HibernateException e ) {
61                 log.warn( "Unable to configure SQLExceptionConverter", e );
62                 throw e;
63             }
64         }
65
66         return converter;
67     }
68
69     /**
70      * Builds a minimal converter. The instance returned here just always converts to
71      * {@link GenericJDBCException}.
72      *
73      * @return The minimal converter.
74      */

75     public static SQLExceptionConverter buildMinimalSQLExceptionConverter() {
76         return new SQLExceptionConverter() {
77             public JDBCException convert(SQLException JavaDoc sqlException, String JavaDoc message, String JavaDoc sql) {
78                 return new GenericJDBCException( message, sqlException, sql );
79             }
80         };
81     }
82
83     private static SQLExceptionConverter constructConverter(String JavaDoc converterClassName, ViolatedConstraintNameExtracter violatedConstraintNameExtracter) {
84         try {
85             log.trace( "Attempting to construct instance of specified SQLExceptionConverter [" + converterClassName + "]" );
86             Class JavaDoc converterClass = ReflectHelper.classForName( converterClassName );
87
88             // First, try to find a matching constructor accepting a ViolatedConstraintNameExtracter param...
89
Constructor JavaDoc[] ctors = converterClass.getDeclaredConstructors();
90             for ( int i = 0; i < ctors.length; i++ ) {
91                 if ( ctors[i].getParameterTypes() != null && ctors[i].getParameterTypes().length == 1 ) {
92                     if ( ViolatedConstraintNameExtracter.class.isAssignableFrom( ctors[i].getParameterTypes()[0] ) ) {
93                         try {
94                             return ( SQLExceptionConverter )
95                                     ctors[i].newInstance( new Object JavaDoc[]{violatedConstraintNameExtracter} );
96                         }
97                         catch ( Throwable JavaDoc t ) {
98                             // eat it and try next
99
}
100                     }
101                 }
102             }
103
104             // Otherwise, try to use the no-arg constructor
105
return ( SQLExceptionConverter ) converterClass.newInstance();
106
107         }
108         catch ( Throwable JavaDoc t ) {
109             log.warn( "Unable to construct instance of specified SQLExceptionConverter", t );
110         }
111
112         return null;
113     }
114 }
115
Popular Tags