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 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
public class ValidatorFactory { |
19 | 1 | private static final Logger LOG = Logger.getLogger(ValidatorFactory.class); |
20 | 9 | private volatile Map<String,Validator> customValidators = null; |
21 | 9 | private DefaultValidatorImpl defaultValidator = new DefaultValidatorImpl(); |
22 | |
|
23 | 9 | private List<Validator> validatorList = new ArrayList<Validator>(); |
24 | |
|
25 | 9 | public ValidatorFactory(){ |
26 | 9 | defaultValidator.setValidatorFactory(this); |
27 | 9 | } |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public Map<String,Validator> getCustomValidators(){ |
34 | 0 | Map<String,Validator> result = customValidators; |
35 | 0 | if(result == null) { |
36 | 0 | synchronized (this) { |
37 | 0 | result = customValidators; |
38 | 0 | if(result == null){ |
39 | 0 | result = new HashMap<String, Validator>(); |
40 | 0 | for(Validator validator: validatorList){ |
41 | 0 | String validatorName = validator.getClass().getName(); |
42 | 0 | result.put(validatorName, validator); |
43 | 0 | } |
44 | 0 | customValidators = result; |
45 | |
} |
46 | 0 | } |
47 | |
} |
48 | 0 | return result; |
49 | |
} |
50 | |
|
51 | |
|
52 | |
public Validator getValidator(String customValidator) { |
53 | |
|
54 | 0 | LOG.info("Retrieving validatior:" + customValidator); |
55 | |
|
56 | 0 | Validator v = getCustomValidators().get(customValidator); |
57 | |
|
58 | 0 | if(v != null && v instanceof BaseAbstractValidator) { |
59 | 0 | BaseAbstractValidator bv = (BaseAbstractValidator)v; |
60 | 0 | bv.setValidatorFactory(this); |
61 | 0 | return bv; |
62 | |
} else { |
63 | 0 | return v; |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
public Validator getValidator(){ |
68 | 0 | return defaultValidator; |
69 | |
} |
70 | |
|
71 | |
public DefaultValidatorImpl getDefaultValidator() { |
72 | 0 | return defaultValidator; |
73 | |
} |
74 | |
|
75 | |
public void setDefaultValidator(DefaultValidatorImpl defaultValidator) { |
76 | 9 | this.defaultValidator = defaultValidator; |
77 | 9 | this.defaultValidator.setValidatorFactory(this); |
78 | 9 | } |
79 | |
|
80 | |
public void setValidatorList(List<Validator> validatorList) { |
81 | 0 | this.validatorList = validatorList; |
82 | 0 | } |
83 | |
} |