1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.nuiton.validator;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.util.Map;
28 import java.util.ServiceLoader;
29 import java.util.TreeMap;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class NuitonValidatorFactory {
45
46
47 private static final Log log =
48 LogFactory.getLog(NuitonValidatorFactory.class);
49
50 protected static String defaultProviderName;
51
52 protected static Map<String, NuitonValidatorProvider> providers;
53
54 public static <O> NuitonValidator<O> newValidator(Class<O> type,
55 NuitonValidatorScope... scopes) {
56 NuitonValidator<O> result = newValidator(type, null, scopes);
57 return result;
58 }
59
60 public static <O> NuitonValidator<O> newValidator(Class<O> type,
61 String context,
62 NuitonValidatorScope... scopes) {
63
64 String providerName = getDefaultProviderName();
65
66 NuitonValidator<O> result = newValidator(providerName,
67 type,
68 context,
69 scopes);
70 return result;
71 }
72
73 public static <O> NuitonValidator<O> newValidator(String providerName,
74 Class<O> type,
75 String context,
76 NuitonValidatorScope... scopes) throws NullPointerException {
77
78 if (type == null) {
79 throw new NullPointerException(
80 "type parameter can not be null.");
81 }
82
83
84 NuitonValidatorProvider provider = getProvider(providerName);
85
86
87
88 NuitonValidatorModel<O> model =
89 provider.getModel(type, context, scopes);
90
91
92 NuitonValidator<O> result = provider.newValidator(model);
93 return result;
94 }
95
96 public static Map<String, NuitonValidatorProvider> getProviders() {
97 if (providers == null) {
98 providers = new TreeMap<String, NuitonValidatorProvider>();
99 ServiceLoader<NuitonValidatorProvider> serviceLoader =
100 ServiceLoader.load(NuitonValidatorProvider.class);
101
102 for (NuitonValidatorProvider provider : serviceLoader) {
103 if (log.isInfoEnabled()) {
104 log.info("obtain validator provider " + provider.getName());
105 }
106 providers.put(provider.getName(), provider);
107 }
108 }
109 return providers;
110 }
111
112 public static NuitonValidatorProvider getProvider(String providerName) throws IllegalArgumentException, NullPointerException {
113
114 if (providerName == null) {
115
116
117 throw new NullPointerException(
118 "providerName parameter can not be null.");
119 }
120
121 NuitonValidatorProvider provider = getProviders().get(providerName);
122 if (provider == null) {
123 throw new IllegalArgumentException(
124 "Could not find provider named '" +
125 defaultProviderName + "', existing providers are : " +
126 getProviders().keySet());
127 }
128 return provider;
129 }
130
131 public static NuitonValidatorProvider getDefaultProvider() {
132 String providerName = getDefaultProviderName();
133 NuitonValidatorProvider provider = getProvider(providerName);
134 return provider;
135 }
136
137 public static String getDefaultProviderName() throws IllegalStateException {
138
139 if (defaultProviderName == null) {
140
141
142
143 Map<String, NuitonValidatorProvider> providers = getProviders();
144 if (providers.isEmpty()) {
145 throw new IllegalStateException(
146 "Could not find any provider of validator.");
147 }
148 defaultProviderName = providers.keySet().iterator().next();
149
150 if (log.isInfoEnabled()) {
151 log.info("Set the default provider name to " + defaultProviderName);
152 }
153 }
154
155 return defaultProviderName;
156 }
157
158 public static void setDefaultProviderName(String defaultProviderName) throws IllegalArgumentException, NullPointerException {
159
160 if (defaultProviderName == null) {
161 throw new NullPointerException("defaultProviderName can not be null.");
162 }
163
164 getProvider(defaultProviderName);
165
166 NuitonValidatorFactory.defaultProviderName = defaultProviderName;
167 }
168
169 protected NuitonValidatorFactory() {
170
171 }
172
173
174 }