From 3e22ef111cbfdb528f27f6f7057dc51a1f825195 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 30 Mar 2011 13:28:01 -0500 Subject: [PATCH] HHH-6061 - ValidatoryFactory type checking --- .../BeanValidationIntegrator.java | 38 +++++++++++++++++++ .../cfg/beanvalidation/TypeSafeActivator.java | 10 +++++ .../org/hibernate/ejb/Ejb3Configuration.java | 3 ++ .../ConfigurationObjectSettingTest.java | 2 - 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java b/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java index bda431a93b..ad98076bbf 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationIntegrator.java @@ -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( diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java b/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java index 966daeafa8..73573ae3e0 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java @@ -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 ); diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java index 59e54c50f9..3d91ebeae2 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java @@ -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 ); } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/ConfigurationObjectSettingTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/ConfigurationObjectSettingTest.java index b12ce943b3..ec4cd8b2de 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/ConfigurationObjectSettingTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/ConfigurationObjectSettingTest.java @@ -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();