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