View Javadoc

1   package org.kuali.student.common.validator;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.apache.log4j.Logger;
9   
10  /**
11   * ValidatorFactory provides a mechanism to 
12   *  
13   * 
14   * @author Kuali Rice Team (kuali-rice@googlegroups.com)
15   *
16   */
17  
18  public class ValidatorFactory {
19  	private static final Logger LOG = Logger.getLogger(ValidatorFactory.class);
20  	private volatile Map<String,Validator> customValidators = null; 
21  	private DefaultValidatorImpl defaultValidator = new DefaultValidatorImpl();
22  	
23  	private List<Validator> validatorList = new ArrayList<Validator>();
24  	
25  	public ValidatorFactory(){
26  		defaultValidator.setValidatorFactory(this);
27  	}
28  	
29  	/**
30  	 * Updated to fix double check lock not working
31  	 * @return
32  	 */
33  	public Map<String,Validator> getCustomValidators(){
34  		Map<String,Validator> result = customValidators;
35  	    if(result == null) {
36  	    	synchronized (this) {
37  	    		result = customValidators;
38  	    		if(result == null){
39  	    	        result = new HashMap<String, Validator>();
40  	    	        for(Validator validator: validatorList){
41  	    	            String validatorName = validator.getClass().getName();
42  	    	            result.put(validatorName, validator);
43  	    	        }
44  	    			customValidators = result;
45  	    		}
46  			}
47  	    }
48  	    return result;
49  	}
50  	
51  	
52  	public Validator getValidator(String customValidator) {
53  	
54  		LOG.info("Retrieving validatior:" + customValidator);
55  	    
56  	    Validator v = getCustomValidators().get(customValidator); 
57  	    
58  	    if(v != null && v instanceof BaseAbstractValidator) {
59  	        BaseAbstractValidator bv = (BaseAbstractValidator)v;
60  	        bv.setValidatorFactory(this);
61  	        return bv;
62  	    } else {
63  	       return v;
64  	    }
65  	}
66  	
67  	public Validator getValidator(){
68  		return defaultValidator;
69  	}
70  	
71  	public DefaultValidatorImpl getDefaultValidator() {
72  		return defaultValidator;
73  	}
74  
75  	public void setDefaultValidator(DefaultValidatorImpl defaultValidator) {
76  		this.defaultValidator = defaultValidator;
77  		this.defaultValidator.setValidatorFactory(this);
78  	}
79  
80      public void setValidatorList(List<Validator> validatorList) {
81          this.validatorList = validatorList;
82      }
83  }