~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/exception/SQLExceptionConverterFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id: SQLExceptionConverterFactory.java 4782 2004-11-21 00:11:27Z pgmjsd $
 
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;
 
14
import java.sql.SQLException;
 
15
import java.util.Properties;
 
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 properties) throws HibernateException {
 
44
                SQLExceptionConverter converter = null;
 
45
 
 
46
                String converterClassName = ( String ) 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 sqlException, String message, String sql) {
 
78
                                return new GenericJDBCException( message, sqlException, sql );
 
79
                        }
 
80
                };
 
81
        }
 
82
 
 
83
        private static SQLExceptionConverter constructConverter(String converterClassName, ViolatedConstraintNameExtracter violatedConstraintNameExtracter) {
 
84
                try {
 
85
                        log.trace( "Attempting to construct instance of specified SQLExceptionConverter [" + converterClassName + "]" );
 
86
                        Class converterClass = ReflectHelper.classForName( converterClassName );
 
87
 
 
88
                        // First, try to find a matching constructor accepting a ViolatedConstraintNameExtracter param...
 
89
                        Constructor[] 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[]{violatedConstraintNameExtracter} );
 
96
                                                }
 
97
                                                catch ( Throwable 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 t ) {
 
109
                        log.warn( "Unable to construct instance of specified SQLExceptionConverter", t );
 
110
                }
 
111
 
 
112
                return null;
 
113
        }
 
114
}