1 /* 2 * Copyright 2004-2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springmodules.validation.commons; 18 19 import java.util.Locale; 20 21 import org.apache.commons.validator.ValidatorException; 22 import org.slf4j.Logger; 23 import org.slf4j.LoggerFactory; 24 import org.springframework.validation.Errors; 25 import org.springframework.validation.Validator; 26 27 /** 28 * @author Rob Harrop 29 */ 30 public abstract class AbstractBeanValidator implements Validator { 31 32 private static final Logger log = LoggerFactory.getLogger(AbstractBeanValidator.class); 33 34 private ValidatorFactory validatorFactory; 35 36 /** 37 * Checks if the validatorFactory is configured to handle this class. Will 38 * convert the class into a form name, suitable for commons validator. 39 * 40 * @return <code>true</code> if the validatorFactory supports the class, 41 * or <code>false</code> if not 42 * @see #getFormName(Class) 43 */ 44 public boolean supports(Class clazz) { 45 boolean canSupport = validatorFactory.hasRulesForBean(getFormName(clazz), getLocale()); 46 if (log.isDebugEnabled()) { 47 log.debug("validatorFactory " + (canSupport ? "does" : "does not") 48 + " support class " + clazz + " with form name " + getFormName(clazz)); 49 } 50 return canSupport; 51 } 52 53 /** 54 * Validates the supplied object using a <code>org.apache.commons.validator.Validator</code> loaded from 55 * the configured <code>ValidatorFactory</code>. 56 * 57 * @see ValidatorFactory 58 */ 59 public void validate(Object obj, Errors errors) { 60 org.apache.commons.validator.Validator commonsValidator = getValidator(obj, errors); 61 initValidator(commonsValidator); 62 try { 63 commonsValidator.validate(); 64 } 65 catch (ValidatorException e) { 66 log.error("Exception while validating object " + obj, e); 67 } finally { 68 cleanupValidator(commonsValidator); 69 } 70 } 71 72 public void setValidatorFactory(ValidatorFactory validatorFactory) { 73 this.validatorFactory = validatorFactory; 74 } 75 76 /** 77 * Gets the <code>Locale</code> used to lookup <code>Validator</code> instances. Default implementation 78 * returns the value of <code>Locale.getDefault()</code>. Subclasses can override this to change 79 * <code>Locale</code>-handling behavior. 80 */ 81 protected Locale getLocale() { 82 return Locale.getDefault(); 83 } 84 85 /** 86 * Retrieves an instance of <code>org.apache.commons.validator.Validator</code> 87 * for the specified <code>Object</code> 88 * from the configured <code>ValidatorFactory</code>. 89 * 90 * @param obj the <code>Object</code> being validated. 91 * @param errors the <code>Errors</code> object to write validation errors to. 92 */ 93 private org.apache.commons.validator.Validator getValidator(Object obj, Errors errors) { 94 return this.validatorFactory.getValidator(getFormName(obj.getClass()), obj, errors); 95 } 96 97 /** 98 * A callback method that is called just before the validate() method is called on the given validator. 99 * This can be used to further configure the validator. 100 * 101 * @param validator 102 */ 103 protected void initValidator(org.apache.commons.validator.Validator validator) { 104 } 105 106 /** 107 * A callback method that is called just after the validate() method is called on the given validator. 108 * This method can be used to clean up all the extra configuration added in the {@link #initValidator(org.apache.commons.validator.Validator)} 109 * method. 110 * 111 * @param validator 112 */ 113 protected void cleanupValidator(org.apache.commons.validator.Validator validator) { 114 } 115 116 /** 117 * Returns the name of the Commons Validator <code>Form</code> used to 118 * validate instances of the supplied class. 119 * 120 * @param aClass 121 * @return the form name that Commons Validator can use to look up a form 122 */ 123 protected abstract String getFormName(Class aClass); 124 }