HHH-6061 - ValidatoryFactory type checking

This commit is contained in:
Steve Ebersole 2011-03-30 13:28:01 -05:00
parent 5b5ec673b4
commit 3e22ef111c
4 changed files with 51 additions and 2 deletions

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.cfg.beanvalidation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
@ -55,6 +56,43 @@ public class BeanValidationIntegrator implements Integrator {
private static final String ACTIVATOR_CLASS = "org.hibernate.cfg.beanvalidation.TypeSafeActivator";
private static final String DDL_METHOD = "applyDDL";
private static final String ACTIVATE_METHOD = "activateBeanValidation";
private static final String VALIDATE_METHOD = "validateFactory";
public static void validateFactory(Object object) {
try {
final Class activatorClass = BeanValidationIntegrator.class.getClassLoader().loadClass( ACTIVATOR_CLASS );
try {
final Method validateMethod = activatorClass.getMethod( VALIDATE_METHOD, Object.class );
if ( ! validateMethod.isAccessible() ) {
validateMethod.setAccessible( true );
}
try {
validateMethod.invoke( null, object );
}
catch (InvocationTargetException e) {
if ( e.getTargetException() instanceof HibernateException ) {
throw (HibernateException) e.getTargetException();
}
throw new HibernateException( "Unable to check validity of passed ValidatorFactory", e );
}
catch (IllegalAccessException e) {
throw new HibernateException( "Unable to check validity of passed ValidatorFactory", e );
}
}
catch (HibernateException e) {
throw e;
}
catch (Exception e) {
throw new HibernateException( "Could not locate method needed for ValidatorFactory validation", e );
}
}
catch (HibernateException e) {
throw e;
}
catch (Exception e) {
throw new HibernateException( "Could not locate TypeSafeActivator class", e );
}
}
@Override
public void integrate(

View File

@ -69,6 +69,16 @@ class TypeSafeActivator {
private static final String FACTORY_PROPERTY = "javax.persistence.validation.factory";
@SuppressWarnings( {"UnusedDeclaration"})
public static void validateFactory(Object object) {
if ( ! ValidatorFactory.class.isInstance( object ) ) {
throw new HibernateException(
"Given object was not an instance of " + ValidatorFactory.class.getName()
+ "[" + object.getClass().getName() + "]"
);
}
}
@SuppressWarnings( {"UnusedDeclaration"})
public static void activateBeanValidation(EventListenerRegistry listenerRegistry, Properties properties) {
ValidatorFactory factory = getValidatorFactory( properties );

View File

@ -73,6 +73,7 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.annotations.reflection.XMLContext;
import org.hibernate.cfg.beanvalidation.BeanValidationIntegrator;
import org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider;
import org.hibernate.ejb.event.JpaEventListenerRegistration;
import org.hibernate.ejb.instrument.InterceptFieldClassFileTransformer;
@ -237,6 +238,7 @@ public class Ejb3Configuration implements Serializable, Referenceable {
// validation factory
final Object validationFactory = overrides.get( AvailableSettings.VALIDATION_FACTORY );
if ( validationFactory != null ) {
BeanValidationIntegrator.validateFactory( validationFactory );
props.put( AvailableSettings.VALIDATION_FACTORY, validationFactory );
}
overrides.remove( AvailableSettings.VALIDATION_FACTORY );
@ -602,6 +604,7 @@ public class Ejb3Configuration implements Serializable, Referenceable {
// validation factory
final Object validationFactory = integration.get( AvailableSettings.VALIDATION_FACTORY );
if ( validationFactory != null ) {
BeanValidationIntegrator.validateFactory( validationFactory );
properties.put( AvailableSettings.VALIDATION_FACTORY, validationFactory );
}

View File

@ -141,7 +141,6 @@ public class ConfigurationObjectSettingTest extends BaseUnitTestCase {
}
@Test
@FailureExpected( jiraKey = "HHH-6061")
public void testContainerBootstrapValidationFactory() {
final Object token = new Object();
PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter();
@ -244,7 +243,6 @@ public class ConfigurationObjectSettingTest extends BaseUnitTestCase {
}
@Test
@FailureExpected( jiraKey = "HHH-6061")
public void testStandaloneBootstrapValidationFactory() {
final Object token = new Object();
PersistenceMetadata metadata = new PersistenceMetadata();