View Javadoc

1   package org.kuali.student.r2.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  // This class is a special case, this class/equivelent doesn't exist in R2 
19  //packages and is a common and has methods used in both R1 and R2 packages, 
20  //this class was duplicated to R2 and modified to work with R2 services
21  //BaseAbstractValidator, BaseAbstractValidator, Validator, ValidatorFactory
22  
23  public class ValidatorFactory {
24  	private static final Logger LOG = Logger.getLogger(ValidatorFactory.class);
25  	private volatile Map<String,Validator> customValidators = null; 
26  	private DefaultValidatorImpl defaultValidator = new DefaultValidatorImpl();
27  	
28  	private List<Validator> validatorList = new ArrayList<Validator>();
29  	
30  	public ValidatorFactory(){
31  		defaultValidator.setValidatorFactory(this);
32  	}
33  	
34  	/**
35  	 * Updated to fix double check lock not working
36  	 * @return
37  	 */
38  	public Map<String,Validator> getCustomValidators(){
39  		Map<String,Validator> result = customValidators;
40  	    if(result == null) {
41  	    	synchronized (this) {
42  	    		result = customValidators;
43  	    		if(result == null){
44  	    	        result = new HashMap<String, Validator>();
45  	    	        for(Validator validator: validatorList){
46  	    	            String validatorName = validator.getClass().getName();
47  	    	            result.put(validatorName, validator);
48  	    	        }
49  	    			customValidators = result;
50  	    		}
51  			}
52  	    }
53  	    return result;
54  	}
55  	
56  	
57  	public Validator getValidator(String customValidator) {
58  	
59  		LOG.info("Retrieving validatior:" + customValidator);
60  	    
61  	    Validator v = getCustomValidators().get(customValidator); 
62  	    
63  	    if(v != null && v instanceof BaseAbstractValidator) {
64  	        BaseAbstractValidator bv = (BaseAbstractValidator)v;
65  	        bv.setValidatorFactory(this);
66  	        return bv;
67  	    } else {
68  	       return v;
69  	    }
70  	}
71  	
72  	public Validator getValidator(){
73  		return defaultValidator;
74  	}
75  	
76  	public DefaultValidatorImpl getDefaultValidator() {
77  		return defaultValidator;
78  	}
79  
80  	public void setDefaultValidator(DefaultValidatorImpl defaultValidator) {
81  		this.defaultValidator = defaultValidator;
82  		this.defaultValidator.setValidatorFactory(this);
83  	}
84  
85      public void setValidatorList(List<Validator> validatorList) {
86          this.validatorList = validatorList;
87      }
88  }