001: /*
002: * Copyright 2006-2007 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen;
017:
018: import java.util.HashMap;
019: import java.util.Hashtable;
020: import java.util.Locale;
021: import java.util.Map;
022:
023: /**
024: * This class represents factories that can generate validation services from
025: * configuration files (or however the factory generates them). The
026: * static portion of this factory implementation is thread-safe.
027: *
028: * @author Shellman, Dan
029: */
030: public class ValidationFactory {
031: /**
032: * @deprecated This is no longer used for version 1.1 or later. For OGNL,
033: * use FACTORY_OGNL_XML. For MVEL, use FACTORY_MVEL_XML.
034: */
035: public static final String FACTORY_DEFAULT_XML = "FACTORY_DEFAULT_XML";
036: public static final String FACTORY_OGNL_XML = "FACTORY_OGNL_XML";
037: public static final String FACTORY_MVEL_XML = "FACTORY_MVEL_XML";
038:
039: private static Hashtable factoryMap = new Hashtable();
040:
041: protected Map serviceMap = new HashMap();
042: protected String configLocation;
043: protected Locale defaultLocale = Locale.getDefault();
044:
045: /**
046: * Register default factories.
047: */
048: static {
049: try {
050: registerFactory(Class
051: .forName("org.iscreen.ognl.OgnlXmlServiceFactory"),
052: FACTORY_DEFAULT_XML);
053: registerFactory(Class
054: .forName("org.iscreen.ognl.OgnlXmlServiceFactory"),
055: FACTORY_OGNL_XML);
056: } catch (Exception e) {
057: //TODO: Do something here.
058: }
059:
060: try {
061: registerFactory(Class
062: .forName("org.iscreen.mvel.MvelXmlServiceFactory"),
063: FACTORY_MVEL_XML);
064: } catch (Exception e) {
065: //TODO: Do something here.
066: }
067: }
068:
069: /**
070: * Protected constructor. In general, validation factories shouldn't
071: * be constructed directly (hence the need to register them, etc.).
072: */
073: protected ValidationFactory() {
074: } //end ValidationFactory()
075:
076: /**
077: * Builds a registered factory. The factory is tied to the
078: * configuration type (such as XML, etc.). Pre-defined factories
079: * (constants defined as FACTORY_xxx) are always available.
080: *
081: * @param factoryId The id of the factory to retrieve.
082: * @param configLocation Classpath location of the configuration
083: * @param theDefaultLocale The default locale
084: * @param services Services that are necessary for the factory
085: *
086: * @return Returns a ValidationFactory. If none exist with the given
087: * id, then a runtime exception is thrown.
088: */
089: public static ValidationFactory buildFactory(String factoryId,
090: String configLocation, Locale theDefaultLocale, Map services) {
091: Class factoryClass;
092: ValidationFactory factory;
093:
094: factoryClass = (Class) factoryMap.get(factoryId);
095: if (factoryClass == null) {
096: throw new ConfigurationException(
097: "No validation factory with factory id of "
098: + factoryId);
099: }
100:
101: try {
102: factory = (ValidationFactory) factoryClass.newInstance();
103:
104: factory.setDefaultLocale(theDefaultLocale);
105: factory.setServices(services);
106: factory.setConfigLocation(configLocation);
107: factory.loadConfig();
108: } catch (InstantiationException e) {
109: throw new ConfigurationException(
110: "Unable to instantiate validation factory. Factory id is "
111: + factoryId + " and class name is "
112: + factoryClass.getName(), e);
113: } catch (IllegalAccessException e) {
114: throw new ConfigurationException(
115: "Illegal access trying to instantiate validation factory. Factory id is "
116: + factoryId + " and class name is "
117: + factoryClass.getName(), e);
118: }
119:
120: return factory;
121: } //end buildFactory()
122:
123: /**
124: * Builds a registered factory. The factory is tied to the
125: * configuration type (such as XML, etc.). Pre-defined factories
126: * (constants defined as FACTORY_xxx) are always available.
127: *
128: * @param factoryId The id of the factory to retrieve.
129: * @param configLocation Classpath location of the configuration
130: * @param services Services that are necessary for the factory
131: *
132: * @return Returns a ValidationFactory. If none exist with the given
133: * id, then a runtime exception is thrown.
134: */
135: public static ValidationFactory buildFactory(String factoryId,
136: String configLocation, Map services) {
137: return buildFactory(factoryId, configLocation, Locale
138: .getDefault(), services);
139: } //end buildFactory()
140:
141: /**
142: * Registers a factory with a given factory id. Use this method to
143: * register a custom-built factory. If a factory of the given id already
144: * exists, it will be replaced.
145: *
146: * @param factoryClass The class of the factory to register
147: * @param factoryId The id of the factory (used to retrieve it later)
148: */
149: public static void registerFactory(Class factoryClass,
150: String factoryId) {
151: factoryMap.put(factoryId, factoryClass);
152: } //end registerFactory()
153:
154: /**
155: * Retrieves a validation service with the given service name.
156: *
157: * @param serviceName The name of the validation service.
158: *
159: * @return Returns the validation service with the given name.
160: */
161: public ValidationService getValidationService(String serviceName) {
162: return null;
163: } //end getValidationService()
164:
165: // ***
166: // Protected methods
167: // ***
168:
169: /**
170: * This is called right after the validation factory is instantiated
171: * and the services call (setServices()) has been called. This method
172: * informs the factory of the config file that will be used. If it's not
173: * in the format that the factory expects, the factory should throw an
174: * unchecked exception.
175: *
176: * @param location The location of the configuration file for the factory.
177: * The location is classpath-based.
178: */
179: protected void setConfigLocation(String location) {
180: configLocation = location;
181: } //end setConfigLocation()
182:
183: /**
184: * Retrieves the configuration file location for this validation factory.
185: *
186: * @return Returns the location of the config file. This may be null.
187: */
188: protected String getConfigLocation() {
189: return configLocation;
190: } //end getConfigLocation()
191:
192: /**
193: * Sets the default locale for this factory.
194: *
195: * @param locale The default locale for this factory.
196: */
197: protected void setDefaultLocale(Locale locale) {
198: defaultLocale = locale;
199: } //end setDefaultLocale()
200:
201: /**
202: * Retrieves the default locale for this factory.
203: *
204: * @return Returns the default locale for this factory.
205: */
206: protected Locale getDefaultLocale() {
207: return defaultLocale;
208: } //end getDefaultLocale()
209:
210: /**
211: * This is called right after the validation factory is instantiated.
212: * This map of services is meant to provide the factory with additional,
213: * external services that the factory can make available to validation
214: * services.
215: *
216: * @param theServices The services to provide to the factory.
217: */
218: protected void setServices(Map theServices) {
219: serviceMap.clear();
220:
221: if (theServices != null) {
222: serviceMap.putAll(theServices);
223: }
224: } //end setServices()
225:
226: /**
227: * Retrieves the map of services available to this factory.
228: *
229: * @return Returns the map of services.
230: */
231: protected Map getServices() {
232: return serviceMap;
233: } //end getServices()
234:
235: /**
236: * This method is called to initialize the factory. It is called after
237: * the services and configuration location have been set. After this
238: * call, the factory should be in a state that it can retrieve
239: * validation services.
240: */
241: protected void loadConfig() {
242: } //end loadConfig()
243: } //end ValidationFactory
|