Author: epbernard
Date: 2006-09-25 17:55:12 -0400 (Mon, 25 Sep 2006)
New Revision: 10530
Added:
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/Environment.java
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/PrefixMessageInterpolator.java
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/event/ValidateEventListener.java
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/HibernateAnnotationIntegrationTest.java
Log:
ANN-443 hibernate.validator.interpolator_class
Added: trunk/HibernateExt/metadata/src/java/org/hibernate/validator/Environment.java
===================================================================
---
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/Environment.java 2006-09-25
16:14:29 UTC (rev 10529)
+++
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/Environment.java 2006-09-25
21:55:12 UTC (rev 10530)
@@ -0,0 +1,16 @@
+//$Id: $
+package org.hibernate.validator;
+
+/**
+ * Hibernate Validator Event properties
+ * The properties are retrieved from Hibernate
+ * (hibernate.properties, hibernate.cfg.xml, persistence.xml or Configuration API)
+ *
+ * @author Emmanuel Bernard
+ */
+public class Environment {
+ /**
+ * Message interpolator class used. The same instance is shared across all
ClassValidators
+ */
+ public static String MESSAGE_INTERPOLATOR_CLASS =
"hibernate.validator.message_interpolator_class";
+}
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/event/ValidateEventListener.java
===================================================================
---
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/event/ValidateEventListener.java 2006-09-25
16:14:29 UTC (rev 10529)
+++
trunk/HibernateExt/metadata/src/java/org/hibernate/validator/event/ValidateEventListener.java 2006-09-25
21:55:12 UTC (rev 10530)
@@ -11,6 +11,9 @@
import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode;
+import org.hibernate.HibernateException;
+import org.hibernate.util.StringHelper;
+import org.hibernate.util.ReflectHelper;
import org.hibernate.cfg.Configuration;
import org.hibernate.event.Initializable;
import org.hibernate.event.PreInsertEvent;
@@ -26,6 +29,8 @@
import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidStateException;
import org.hibernate.validator.InvalidValue;
+import org.hibernate.validator.Environment;
+import org.hibernate.validator.MessageInterpolator;
/**
* Before insert and update, executes the validator framework
@@ -42,11 +47,32 @@
@SuppressWarnings("unchecked")
public synchronized void initialize(final Configuration cfg) {
if ( !isInitialized ) {
+ String interpolatorString = cfg.getProperty( Environment.MESSAGE_INTERPOLATOR_CLASS
);
+ MessageInterpolator interpolator = null;
+ if ( StringHelper.isNotEmpty( interpolatorString ) ) {
+ try {
+ Class interpolatorClass = ReflectHelper.classForName( interpolatorString );
+ interpolator = (MessageInterpolator) interpolatorClass.newInstance();
+ }
+ catch (ClassNotFoundException e) {
+ throw new HibernateException( "Unable to find message interpolator: " +
interpolatorString, e );
+ }
+ catch (IllegalAccessException e) {
+ throw new HibernateException( "Unable to instanciate message interpolator:
" + interpolatorString, e );
+ }
+ catch (InstantiationException e) {
+ throw new HibernateException( "Unable to instanciate message interpolator:
" + interpolatorString, e );
+ }
+ catch (ClassCastException e) {
+ throw new HibernateException( "Class does not implement "
+ + MessageInterpolator.class.getName() + ": " + interpolatorString, e );
+ }
+ }
Iterator<PersistentClass> classes = (Iterator<PersistentClass>)
cfg.getClassMappings();
while ( classes.hasNext() ) {
PersistentClass clazz = classes.next();
final Class mappedClass = clazz.getMappedClass();
- ClassValidator validator = new ClassValidator( mappedClass );
+ ClassValidator validator = new ClassValidator( mappedClass, interpolator );
ValidatableElement element = new ValidatableElement( mappedClass, validator );
addSubElement( clazz.getIdentifierProperty(), element );
Iterator properties = clazz.getPropertyIterator();
Modified:
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/HibernateAnnotationIntegrationTest.java
===================================================================
---
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/HibernateAnnotationIntegrationTest.java 2006-09-25
16:14:29 UTC (rev 10529)
+++
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/HibernateAnnotationIntegrationTest.java 2006-09-25
21:55:12 UTC (rev 10530)
@@ -10,6 +10,7 @@
import org.hibernate.mapping.PersistentClass;
import org.hibernate.test.annotations.TestCase;
import org.hibernate.validator.InvalidStateException;
+import org.hibernate.validator.Environment;
import org.hibernate.validator.event.ValidatePreInsertEventListener;
import org.hibernate.validator.event.ValidatePreUpdateEventListener;
@@ -68,6 +69,9 @@
catch (InvalidStateException e) {
//success
assertEquals( 2, e.getInvalidValues().length );
+ assertTrue( "Environment.MESSAGE_INTERPOLATOR_CLASS does not work",
+ e.getInvalidValues()[0].getMessage().startsWith( "prefix_")
+ );
}
finally {
if ( tx != null ) tx.rollback();
@@ -145,6 +149,7 @@
}
protected void configure(Configuration cfg) {
+ cfg.setProperty( Environment.MESSAGE_INTERPOLATOR_CLASS,
PrefixMessageInterpolator.class.getName() );
cfg.getEventListeners()
.setPreInsertEventListeners( new PreInsertEventListener[]{new
ValidatePreInsertEventListener()} );
cfg.getEventListeners()
Added:
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/PrefixMessageInterpolator.java
===================================================================
---
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/PrefixMessageInterpolator.java 2006-09-25
16:14:29 UTC (rev 10529)
+++
trunk/HibernateExt/metadata/src/test/org/hibernate/validator/test/PrefixMessageInterpolator.java 2006-09-25
21:55:12 UTC (rev 10530)
@@ -0,0 +1,14 @@
+//$Id: $
+package org.hibernate.validator.test;
+
+import org.hibernate.validator.MessageInterpolator;
+import org.hibernate.validator.Validator;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class PrefixMessageInterpolator implements MessageInterpolator {
+ public String interpolate(String message, Validator validator, MessageInterpolator
defaultInterpolator) {
+ return "prefix_" + defaultInterpolator.interpolate( message, validator,
defaultInterpolator );
+ }
+}
Show replies by date