Author: epbernard
Date: 2007-01-27 11:24:20 -0500 (Sat, 27 Jan 2007)
New Revision: 11110
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/event/JPAValidateEventListener.java
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/event/ValidateEventListener.java
Log:
ANN-546 support for pure JPA players, tests ready but not commited due to dependencies
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/event/JPAValidateEventListener.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/event/JPAValidateEventListener.java
(rev 0)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/event/JPAValidateEventListener.java 2007-01-27
16:24:20 UTC (rev 11110)
@@ -0,0 +1,47 @@
+//$Id: $
+package org.hibernate.validator.event;
+
+import java.lang.ref.WeakReference;
+import java.util.Map;
+import java.util.WeakHashMap;
+import javax.persistence.PrePersist;
+import javax.persistence.PreUpdate;
+
+import org.hibernate.validator.ClassValidator;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class JPAValidateEventListener {
+ //No need for weakReference if the event listener is loaded by the same CL than
+ //the entities, but in a shared env this is not the case
+ //TODO check that this can be hot redeployable
+ private static final Map<Class, WeakReference<ClassValidator>> validators;
+ private static final ClassValidator<JPAValidateEventListener> NO_VALIDATOR;
+ static {
+ validators = new WeakHashMap<Class, WeakReference<ClassValidator>>();
+ NO_VALIDATOR = new
ClassValidator<JPAValidateEventListener>(JPAValidateEventListener.class);
+ }
+
+ @PrePersist
+ @PreUpdate
+ @SuppressWarnings( "unchecked" )
+ public void onChange(Object object) {
+ if (object == null) return;
+ Class entity = object.getClass();
+ WeakReference<ClassValidator> weakValidator = validators.get(entity);
+ ClassValidator validator = weakValidator != null ? weakValidator.get() : null;
+ if ( validator == null ) {
+ //initialize
+ //TODO reuse the same reflection manager?
+ validator = new ClassValidator(entity);
+ if ( ! validator.hasValidationRules() ) {
+ validator = NO_VALIDATOR;
+ }
+ validators.put( entity, new WeakReference<ClassValidator>(validator) );
+ }
+ if ( validator != NO_VALIDATOR ) {
+ validator.assertValid( object );
+ }
+ }
+}
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/event/ValidateEventListener.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/event/ValidateEventListener.java 2007-01-27
02:53:20 UTC (rev 11109)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/event/ValidateEventListener.java 2007-01-27
16:24:20 UTC (rev 11110)
@@ -136,7 +136,6 @@
if ( element == null ) return; //no validation to do
List<InvalidValue> consolidatedInvalidValues = new
ArrayList<InvalidValue>();
validateSubElements( element, entity, consolidatedInvalidValues );
- consolidatedInvalidValues.size();
InvalidValue[] invalidValues = element.validator == null ?
null :
element.validator.getInvalidValues( entity );
Show replies by date