FileDocCategorySizeDatePackage
SQLExceptionConverterFactory.javaAPI DocHibernate 3.2.54048Sat Nov 20 17:11:28 GMT 2004org.hibernate.exception

SQLExceptionConverterFactory

public class SQLExceptionConverterFactory extends Object
A factory for building SQLExceptionConverter instances.
author
Steve Ebersole

Fields Summary
private static final Log
log
Constructors Summary
private SQLExceptionConverterFactory()


	  
		// Private constructor - stops checkstyle from complaining.
	
Methods Summary
public static SQLExceptionConverterbuildMinimalSQLExceptionConverter()
Builds a minimal converter. The instance returned here just always converts to {@link GenericJDBCException}.

return
The minimal converter.

		return new SQLExceptionConverter() {
			public JDBCException convert(SQLException sqlException, String message, String sql) {
				return new GenericJDBCException( message, sqlException, sql );
			}
		};
	
public static SQLExceptionConverterbuildSQLExceptionConverter(org.hibernate.dialect.Dialect dialect, java.util.Properties properties)
Build a SQLExceptionConverter instance.

First, looks for a {@link Environment.SQL_EXCEPTION_CONVERTER} property to see if the configuration specified the class of a specific converter to use. If this property is set, attempt to construct an instance of that class. If not set, or if construction fails, the converter specific to the dialect will be used.

param
dialect The defined dialect.
param
properties The configuration properties.
return
An appropriate SQLExceptionConverter instance.
throws
HibernateException There was an error building the SQLExceptionConverter.

		SQLExceptionConverter converter = null;

		String converterClassName = ( String ) properties.get( Environment.SQL_EXCEPTION_CONVERTER );
		if ( StringHelper.isNotEmpty( converterClassName ) ) {
			converter = constructConverter( converterClassName, dialect.getViolatedConstraintNameExtracter() );
		}

		if ( converter == null ) {
			log.trace( "Using dialect defined converter" );
			converter = dialect.buildSQLExceptionConverter();
		}

		if ( converter instanceof Configurable ) {
			try {
				( ( Configurable ) converter ).configure( properties );
			}
			catch ( HibernateException e ) {
				log.warn( "Unable to configure SQLExceptionConverter", e );
				throw e;
			}
		}

		return converter;
	
private static SQLExceptionConverterconstructConverter(java.lang.String converterClassName, ViolatedConstraintNameExtracter violatedConstraintNameExtracter)

		try {
			log.trace( "Attempting to construct instance of specified SQLExceptionConverter [" + converterClassName + "]" );
			Class converterClass = ReflectHelper.classForName( converterClassName );

			// First, try to find a matching constructor accepting a ViolatedConstraintNameExtracter param...
			Constructor[] ctors = converterClass.getDeclaredConstructors();
			for ( int i = 0; i < ctors.length; i++ ) {
				if ( ctors[i].getParameterTypes() != null && ctors[i].getParameterTypes().length == 1 ) {
					if ( ViolatedConstraintNameExtracter.class.isAssignableFrom( ctors[i].getParameterTypes()[0] ) ) {
						try {
							return ( SQLExceptionConverter )
									ctors[i].newInstance( new Object[]{violatedConstraintNameExtracter} );
						}
						catch ( Throwable t ) {
							// eat it and try next
						}
					}
				}
			}

			// Otherwise, try to use the no-arg constructor
			return ( SQLExceptionConverter ) converterClass.newInstance();

		}
		catch ( Throwable t ) {
			log.warn( "Unable to construct instance of specified SQLExceptionConverter", t );
		}

		return null;