diff --git a/domain/src/main/java/org/acegisecurity/domain/validation/ValidationManagerImpl.java b/domain/src/main/java/org/acegisecurity/domain/validation/ValidationManagerImpl.java
index 6011c64a16..fce5fd8128 100644
--- a/domain/src/main/java/org/acegisecurity/domain/validation/ValidationManagerImpl.java
+++ b/domain/src/main/java/org/acegisecurity/domain/validation/ValidationManagerImpl.java
@@ -35,6 +35,7 @@ import java.util.Vector;
* Default implementation of {@link ValidationManager}.
*
* @author Ben Alex
+ * @author Matthew E. Porter
* @version $Id$
*/
public class ValidationManagerImpl implements InitializingBean,
@@ -43,7 +44,7 @@ public class ValidationManagerImpl implements InitializingBean,
protected final Log logger = LogFactory.getLog(getClass());
private IntrospectionManager introspectionManager;
- private List validators;
+ private ValidationRegistryManager validationRegistryManager = new ValidationRegistryManagerImpl();
private boolean strictValidation = true;
//~ Methods ================================================================
@@ -60,7 +61,7 @@ public class ValidationManagerImpl implements InitializingBean,
/**
* Indicates whether a {@link ValidatorNotFoundException} should be thrown
* if any domain object does not have a corresponding
- * Validator defined against the {@link #validators}.
+ * Validator.
*
*
* Defaults to true. This is a reasonable default, as callers
@@ -80,37 +81,18 @@ public class ValidationManagerImpl implements InitializingBean,
return strictValidation;
}
- /**
- * Sets the {@link Validator} objects to be used.
- *
- * @param newList that should be used for validation.
- */
- public void setValidators(List newList) {
- Assert.notNull(newList, "A list of Validators is required");
- Assert.isTrue(newList.size() > 0,
- "At least one Validator must be defined");
-
- Iterator iter = newList.iterator();
-
- while (iter.hasNext()) {
- Object currentObject = null;
- currentObject = iter.next();
- Assert.isInstanceOf(Validator.class, currentObject,
- "Validator '" + currentObject
- + "' must be an instance of Validator");
- }
-
- this.validators = newList;
+ public void setValidationRegistryManager(
+ ValidationRegistryManager validationRegistryManager) {
+ this.validationRegistryManager = validationRegistryManager;
}
- public List getValidators() {
- return this.validators;
+ public ValidationRegistryManager getValidationRegistryManager() {
+ return validationRegistryManager;
}
public void afterPropertiesSet() throws Exception {
- Assert.notNull(validators, "A list of Validators is required");
- Assert.isTrue(validators.size() > 0,
- "At least one Validator must be defined");
+ Assert.notNull(validationRegistryManager,
+ "A ValidationRegistryManager is required");
Assert.notNull(introspectionManager,
"An IntrospectionManager is required");
}
@@ -133,7 +115,7 @@ public class ValidationManagerImpl implements InitializingBean,
Assert.notNull(domainObject,
"Cannot validate a null domain object, as unable to getClass()");
- // Construct a list of objects to be validated and add self
+ // Construct a list of objects to be validated and adds self
List allObjects = new Vector();
allObjects.add(domainObject);
@@ -197,18 +179,14 @@ public class ValidationManagerImpl implements InitializingBean,
throws ValidatorNotFoundException {
Assert.notNull(clazz, "Class cannot be null");
- Iterator iter = validators.iterator();
+ Validator validator = this.validationRegistryManager.findValidator(clazz);
- while (iter.hasNext()) {
- Validator validator = (Validator) iter.next();
-
- if (validator.supports(clazz)) {
- return validator;
- }
+ if (validator == null) {
+ throw new ValidatorNotFoundException(
+ "No Validator found for class '" + clazz + "'");
}
- throw new ValidatorNotFoundException("No Validator found for class '"
- + clazz + "'");
+ return validator;
}
/**
diff --git a/domain/src/main/java/org/acegisecurity/domain/validation/ValidationRegistryManager.java b/domain/src/main/java/org/acegisecurity/domain/validation/ValidationRegistryManager.java
new file mode 100644
index 0000000000..85467d96a2
--- /dev/null
+++ b/domain/src/main/java/org/acegisecurity/domain/validation/ValidationRegistryManager.java
@@ -0,0 +1,50 @@
+/* Copyright 2004, 2005 Acegi Technology Pty Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.sf.acegisecurity.domain.validation;
+
+import org.springframework.validation.Validator;
+
+
+/**
+ * ValidationRegistryManager implementations are able to
+ * authoritatively return a Validator instance that is suitable
+ * for a given domain object.
+ *
+ *
+ * Implementations are free to implement their own strategy for maintaining the
+ * list of Validators, or create them on-demand if preferred.
+ * This interface is non-prescriptive.
+ *
Validator that applies for a given domain
+ * object class.
+ *
+ * @param domainClass that a Validator is required for
+ *
+ * @return the Validator, or null if no
+ * Validator is known for the indicated
+ * domainClass
+ */
+ public Validator findValidator(Class domainClass);
+}
diff --git a/domain/src/main/java/org/acegisecurity/domain/validation/ValidationRegistryManagerImpl.java b/domain/src/main/java/org/acegisecurity/domain/validation/ValidationRegistryManagerImpl.java
new file mode 100644
index 0000000000..a3ca6b415e
--- /dev/null
+++ b/domain/src/main/java/org/acegisecurity/domain/validation/ValidationRegistryManagerImpl.java
@@ -0,0 +1,137 @@
+/* Copyright 2004, 2005 Acegi Technology Pty Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.sf.acegisecurity.domain.validation;
+
+import org.springframework.util.Assert;
+import org.springframework.util.ClassUtils;
+
+import org.springframework.validation.Validator;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+/**
+ * A basic implementation of {@link ValidationRegistryManager}.
+ *
+ *
+ * Like PropertyEditorManager,
+ * this implementation uses three techniques for locating a
+ * Validator for a given domain object class:
+ *
+ *
Validator to be expressly registered for a given domain object
+ * class.
+ * Validator by
+ * concatenating "Validator" to the fully qualified domain object classname
+ * (eg "foo.bah.PersonValidator").
+ *