KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > form > config > FormMVCConfigBuilder


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller.form.config;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import org.jdom.Document;
15 import org.jdom.Element;
16
17 import com.inversoft.config.ConfigBuilder;
18 import com.inversoft.config.ConfigRegistry;
19 import com.inversoft.config.ConfigurationException;
20 import com.inversoft.config.component.ComponentConfigBuilderRegistry;
21 import com.inversoft.error.ErrorList;
22 import com.inversoft.verge.config.VergeConfigConstants;
23 import com.inversoft.verge.mvc.config.BaseConfig;
24 import com.inversoft.verge.mvc.config.BaseConfigBuilder;
25 import com.inversoft.verge.repository.config.RepositoryConfigRegistry;
26
27
28 /**
29  * <p>
30  * This class is the ComponentConfigBuilder, used by the Inversoft
31  * Configuration system, for the form based MVC configuration.
32  * </p>
33  *
34  * <p>
35  * This builder uses the com.inversoft.config.component
36  * sub-package to build the configuration.
37  * </p>
38  *
39  * @author Brian Pontarelli
40  * @since 2.0
41  * @version 2.0
42  */

43 public class FormMVCConfigBuilder implements ConfigBuilder {
44
45     /**
46      * The ResourceBundle that stores all the ComponentConfigBuilder information
47      */

48     public static final String JavaDoc BUILDERS_BUNDLE =
49         "com.inversoft.verge.mvc.controller.form.config.FormMVCConfigBuilders";
50
51     /**
52      * This is the component ComponentConfigBuilderRegistry that stores the individual
53      * ConfigBuilders for this package
54      */

55     private ComponentConfigBuilderRegistry builderRegistry;
56
57
58     /**
59      * Constructs a new <code>FormMVCConfigBuilder</code>.
60      *
61      * @throws ConfigurationException If the ComponentConfigBuilderRegistry could not be
62      * initialized properly
63      */

64     public FormMVCConfigBuilder() throws ConfigurationException {
65         builderRegistry = new ComponentConfigBuilderRegistry(BUILDERS_BUNDLE);
66     }
67
68
69     /**
70      * <p>
71      * Builds the form based MVC configuration objects using the given Document
72      * and stores them in the given registry.
73      * </p>
74      *
75      * <p>
76      * This method, like all the configuration methods, is synchronized. This is
77      * not to prevent multiple thread usage but to enforce a proper write of the
78      * ConfigRegistry Object from Thread local memory to the JVM heap memory.
79      * This will ensure that when the ConfigRegistry is later set into the
80      * singleton, which is an atomic operation, it will be correctly populated.
81      * </p>
82      *
83      * @param document The Document to parse and build from
84      * @param registry The registry to store the objects in
85      * @throws ConfigurationException If there were any errors
86      */

87     public synchronized void build(Document document, ConfigRegistry registry)
88     throws ConfigurationException {
89
90         ErrorList errors = new ErrorList();
91         FormMVCConfigRegistry formRegistry = (FormMVCConfigRegistry) registry;
92
93         Iterator JavaDoc iter = document.getRootElement().getChildren().iterator();
94         Element element;
95         BaseConfigBuilder configBuilder;
96         BaseConfig configObject;
97
98         while (iter.hasNext()) {
99             element = (Element) iter.next();
100             configBuilder = (BaseConfigBuilder) builderRegistry.lookup(element.getName());
101             if (configBuilder == null) {
102                 errors.addError("element named: " + element.getName() +
103                     " is not a valid element");
104                 continue;
105             }
106
107             try {
108                 configObject = configBuilder.build(element);
109             } catch (ConfigurationException ce) {
110                 errors.addErrorList(ce.getErrors());
111                 continue;
112             }
113
114             if (configObject instanceof ActionConfig) {
115                 ActionConfig config = (ActionConfig) configObject;
116                 if (formRegistry.lookupAction(config.getName()) != null) {
117                     errors.addError("action named: " + config.getName() +
118                         " is defined twice");
119                     continue;
120                 }
121
122                 formRegistry.register(config.getName(), config);
123             } else if (configObject instanceof FormConfig) {
124                 FormConfig config = (FormConfig) configObject;
125                 if (formRegistry.lookupForm(config.getName()) != null) {
126                     errors.addError("form named: " + config.getName() +
127                         " is defined twice");
128                     continue;
129                 }
130
131                 formRegistry.register(config.getName(), config);
132             } else if (configObject instanceof MappingConfig) {
133                 MappingConfig config = (MappingConfig) configObject;
134                 if (formRegistry.lookupMapping(config.getName()) != null) {
135                     errors.addError("mapping named: " + config.getName() +
136                         " is defined twice");
137                     continue;
138                 }
139
140                 formRegistry.register(config.getName(), config);
141             }
142         }
143
144         if (!errors.isEmpty()) {
145             throw new ConfigurationException(errors);
146         }
147     }
148
149     /**
150      * Since the Portal configuration is dumped and completely reloaded on
151      * refreshes, this calls build because the registry should be fresh.
152      *
153      * @param document The Document to parse and build from
154      * @param registry The registry to store the objects in
155      * @throws ConfigurationException If there were any errors
156      */

157     public void rebuild(Document document, ConfigRegistry registry)
158     throws ConfigurationException {
159         build(document, registry);
160     }
161
162     /**
163      * Sets up any repository items that might be used for the FormBean or action
164      * handlers. Then set the Singleton instance to the new registry.
165      */

166     public void validate(ConfigRegistry registry, Map JavaDoc registries)
167     throws ConfigurationException {
168
169         // The repository Map can not be null because we rely on it
170
assert (registries != null) : "registries == null";
171
172         ActionConfigBuilder actionBuilder =
173             (ActionConfigBuilder) builderRegistry.lookup(Constants.ACTION_ELEMENT);
174         FormConfigBuilder formBuilder =
175             (FormConfigBuilder) builderRegistry.lookup(Constants.FORM_BUILDER_NAME);
176
177         RepositoryConfigRegistry repositoryRegistry = (RepositoryConfigRegistry)
178             registries.get(VergeConfigConstants.REPOSITORY_KEY);
179
180         ErrorList errors = new ErrorList();
181         FormMVCConfigRegistry formRegistry = (FormMVCConfigRegistry) registry;
182         List JavaDoc configs = formRegistry.getAllConfigs();
183         Iterator JavaDoc iter = configs.iterator();
184         Object JavaDoc config;
185
186         while (iter.hasNext()) {
187             config = iter.next();
188
189             try {
190                 if (config instanceof ActionConfig) {
191                     actionBuilder.validate((ActionConfig) config, repositoryRegistry);
192                 } else if (config instanceof FormConfig) {
193                     formBuilder.validate((FormConfig) config, repositoryRegistry,
194                         formRegistry);
195                 }
196             } catch (ConfigurationException ce) {
197                 errors.addErrorList(ce.getErrors());
198             }
199         }
200
201         if (!errors.isEmpty()) {
202             throw new ConfigurationException(errors);
203         }
204     }
205
206     /**
207      * If the validation was successfull, the registry should be all setup so we
208      * can go ahead and set the singleton instance.
209      *
210      * @param registry The FormMVCConfigRegistry that was just built
211      * @param otherRegistries not used
212      */

213     public void commit(ConfigRegistry registry, Map JavaDoc otherRegistries) {
214         FormMVCConfigRegistry.setInstance((FormMVCConfigRegistry) registry);
215     }
216 }
217
Popular Tags