View Javadoc
1   /*
2    * #%L
3    * Nuiton Validator
4    * %%
5    * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
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   * Factory to obtain new validators.
33   *
34   * The factory contains a cache of {@link NuitonValidatorModel}.
35   *
36   * To obtain a new validator with no context, use this code :
37   * <pre>
38   * NuitonValidator&lt;O&gt; validator = NuitonValidatorFactory.newValidator(O.class);
39   * </pre>
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 2.0
43   */
44  public class NuitonValidatorFactory {
45  
46      /** Logger. */
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          // get the provider
84          NuitonValidatorProvider provider = getProvider(providerName);
85  
86  
87          // obtain validator model form the provider
88          NuitonValidatorModel<O> model =
89                  provider.getModel(type, context, scopes);
90  
91          // obtain validator from the the provider
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             // take the default validator provider name
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             // takes the first provider from existing provider
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         // check provider exists
164         getProvider(defaultProviderName);
165 
166         NuitonValidatorFactory.defaultProviderName = defaultProviderName;
167     }
168 
169     protected NuitonValidatorFactory() {
170         // avoid instanciation of this factory
171     }
172 
173 
174 }