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.Map;
019: import java.util.HashMap;
020: import java.util.Locale;
021:
022: /**
023: * This JavaBean is purely for configuration. It's used to retrieve an
024: * instance of the ValidationFactory via a getter, as opposed to getting/building
025: * one directly by calling the ValidationFactory.buildFactory() method. Either
026: * use the appropriate constructor, or call the appropriate setters, and then
027: * retrieve the factory by calling the getFactory() method. Note that the
028: * default factory is the default XML one (so, you don't need to set that if
029: * you plan to use the XML factory). The location of the configuration file
030: * is required, so if it's not set, an exception will be thrown. Since services
031: * are optional, they do not need to be set.
032: *
033: * @author Shellman, Dan
034: */
035: public class ValidationFactoryConfig {
036: protected String factoryId = ValidationFactory.FACTORY_OGNL_XML;
037: protected String configLocation;
038: protected Locale defaultLocale = Locale.getDefault();
039: protected Map services = new HashMap();
040: protected ValidationFactory factory;
041:
042: /**
043: * Default constructor.
044: */
045: public ValidationFactoryConfig() {
046: } //end ValidationFactoryConfig()
047:
048: /**
049: * Constructor that defines all available config properties.
050: *
051: * @param theFactoryId The id of the factory to use (it must have been previously
052: * registered.
053: * @param theConfigurationLocation The location of the configuration file.
054: * @param locale The default locale.
055: * @param theServices The set of services the factory requires.
056: */
057: public ValidationFactoryConfig(String theFactoryId,
058: String theConfigurationLocation, Locale locale,
059: Map theServices) {
060: factoryId = theFactoryId;
061: configLocation = theConfigurationLocation;
062: defaultLocale = locale;
063:
064: if (theServices != null) {
065: services.putAll(theServices);
066: }
067: } //end ValidationFactoryConfig()
068:
069: /**
070: * Constructor that defines all available config properties except services.
071: *
072: * @param theFactoryId The id of the factory to use (it must have been previously
073: * registered.
074: * @param theConfigurationLocation The location of the configuration file.
075: */
076: public ValidationFactoryConfig(String theFactoryId,
077: String theConfigurationLocation) {
078: factoryId = theFactoryId;
079: configLocation = theConfigurationLocation;
080: } //end ValidationFactoryConfig()
081:
082: /**
083: * Constructor that defines only the configuration file. The factory
084: * will be the default XML one, and no services will be defined.
085: *
086: * @param theConfigurationLocation The location of the configuration file.
087: */
088: public ValidationFactoryConfig(String theConfigurationLocation) {
089: configLocation = theConfigurationLocation;
090: } //end ValidationFactoryConfig()
091:
092: /**
093: * Constructor that defines only the configuration file. The factory
094: * will be the default XML one, and no services will be defined.
095: *
096: * @param theConfigurationLocation The location of the configuration file.
097: * @param locale The default locale.
098: */
099: public ValidationFactoryConfig(String theConfigurationLocation,
100: Locale locale) {
101: configLocation = theConfigurationLocation;
102: defaultLocale = locale;
103: } //end ValidationFactoryConfig()
104:
105: /**
106: * Constructor that defines only the configuration file. The factory
107: * will be the default XML one, and no services will be defined.
108: *
109: * @param theConfigurationLocation The location of the configuration file.
110: * @param theServices The set of services the factory requires.
111: */
112: public ValidationFactoryConfig(String theConfigurationLocation,
113: Map theServices) {
114: configLocation = theConfigurationLocation;
115: services.putAll(theServices);
116: } //end ValidationFactoryConfig()
117:
118: /**
119: * Defines the location of the configuration file. This will override the
120: * configuration location defined in the constructor (if it was).
121: *
122: * @param location The location of the configuration file.
123: */
124: public void setConfigurationLocation(String location) {
125: configLocation = location;
126: } //end setConfigurationLocation()
127:
128: /**
129: * Sets the factory id. This will override the factory id defined in the
130: * constructor (if it was).
131: *
132: * @param theFactoryId The id of the factory to use.
133: */
134: public void setFactoryId(String theFactoryId) {
135: factoryId = theFactoryId;
136: } //end setFactoryId()
137:
138: /**
139: * Sets the default locale for this factory.
140: *
141: * @param locale The default locale.
142: */
143: public void setDefaultLocale(Locale locale) {
144: defaultLocale = locale;
145: } //end setDefaultLocale()
146:
147: /**
148: * Sets the services the factory will use. This will remove any prior
149: * defined services and replace them with those passed in.
150: *
151: * @param theServices The services the factory requires.
152: */
153: public void setServices(Map theServices) {
154: services.clear();
155: services.putAll(theServices);
156: } //end setServices()
157:
158: /**
159: * Adds a service to those already defined for the factory.
160: *
161: * @param serviceId The id of the individual service.
162: * @param value The service object, itself.
163: */
164: public void addService(String serviceId, Object value) {
165: services.put(serviceId, value);
166: } //end addService()
167:
168: /**
169: * Builds and returns the factory based upon the configuration defined
170: * within this configuration. If some of the configuration is invalid
171: * or missing (such as no configuration location), no exception will be
172: * thrown, so ensure that all configuration has been set properly.<br />
173: * <br />
174: * If this method is called multiple times, a new factory is NOT constructed.
175: * The same factory returned on the first call will be returned on
176: * subsequent calls.
177: *
178: * @return Returns a built factory.
179: */
180: public ValidationFactory getFactory() {
181: if (factory == null) {
182: factory = ValidationFactory.buildFactory(factoryId,
183: configLocation, defaultLocale, services);
184: }
185:
186: return factory;
187: } //end getFactory()
188: } //end ValidationFactoryConfig
|