KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > config > VergeBaseConfigBuilder


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.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.apache.log4j.Logger;
15 import org.jdom.Document;
16 import org.jdom.Element;
17
18 import com.inversoft.config.ConfigBuilder;
19 import com.inversoft.config.ConfigRegistry;
20 import com.inversoft.config.ConfigurationException;
21 import com.inversoft.verge.mvc.MVCRegistry;
22 import com.inversoft.verge.mvc.MVCURLTools;
23 import com.inversoft.verge.mvc.controller.ControllerHandler;
24 import com.inversoft.verge.mvc.controller.ControllerParser;
25 import com.inversoft.verge.mvc.model.ModelHandler;
26 import com.inversoft.verge.mvc.model.ModelParser;
27 import com.inversoft.verge.mvc.model.ModelResolver;
28 import com.inversoft.verge.mvc.validator.ValidatorHandler;
29 import com.inversoft.verge.mvc.validator.ValidatorParser;
30 import com.inversoft.util.StringTools;
31
32
33 /**
34  * <p>
35  * This class is the ComponentConfigBuilder, used by the Inversoft
36  * Configuration system, for the form based MVC configuration.
37  * </p>
38  *
39  * <p>
40  * This builder does its work outside of the scope of the config
41  * of component config systems.
42  * </p>
43  *
44  * @author Brian Pontarelli
45  * @since 2.0
46  * @version 2.0
47  */

48 public class VergeBaseConfigBuilder implements ConfigBuilder {
49
50     /**
51      * Constructs a new <code>VergeBaseConfigBuilder</code>.
52      */

53     public VergeBaseConfigBuilder() {
54     }
55
56
57     /**
58      * <p>
59      * Builds the base Verge configuration, which is not retained, from the
60      * given XML Document. Current the only configuration stored here is the
61      * logging configuration.
62      * </p>
63      *
64      * <p>
65      * This method, like all the configuration methods, is synchronized. This is
66      * not to prevent multiple thread usage but to enforce a proper write of the
67      * ConfigRegistry Object from Thread local memory to the JVM heap memory.
68      * This will ensure that when the ConfigRegistry is later set into the
69      * singleton, which is an atomic operation, it will be correctly populated.
70      * </p>
71
72      * @param document The Document to parse and build from
73      * @param registry Not used
74      */

75     public synchronized void build(Document document, ConfigRegistry registry) {
76         Element root = document.getRootElement();
77         buildMVC(root);
78     }
79
80     /**
81      * Builds the MVC configuration such as parsers, handlers, resolvers, etc.
82      *
83      * @param root The root element to read the configuration from
84      */

85     void buildMVC(Element root) {
86         Logger logger = Logger.getLogger("com.inversoft");
87         Element mvc = root.getChild(Constants.MVC_ELEMENT);
88         if (mvc != null) {
89             buildMVCConfiguration(mvc, ControllerParser.class,
90                 Constants.CONTROLLER_PARSER_ELEMENT, "ControllerParser", logger);
91             buildMVCConfiguration(mvc, ModelParser.class,
92                 Constants.MODEL_PARSER_ELEMENT, "ModelParser", logger);
93             buildMVCConfiguration(mvc, ValidatorParser.class,
94                 Constants.VALIDATOR_PARSER_ELEMENT, "ValidatorParser", logger);
95             buildMVCConfiguration(mvc, ValidatorHandler.class,
96                 Constants.VALIDATOR_HANDLER_ELEMENT, "ValidatorHandler", logger);
97             buildMVCConfiguration(mvc, ControllerHandler.class,
98                 Constants.CONTROLLER_HANDLER_ELEMENT, "ControllerHandler", logger);
99             buildMVCConfiguration(mvc, ModelResolver.class,
100                 Constants.MODEL_RESOLVER_ELEMENT, "ModelResolver", logger);
101             buildMVCConfiguration(mvc, ModelHandler.class,
102                 Constants.MODEL_HANDLER_ELEMENT, "ModelHandler", logger);
103             buildMVCURLEnding(mvc, logger);
104         } else {
105             logger.info("No MVC configuration");
106         }
107     }
108
109     /**
110      * Helper method to build Controller, Model and Validator parsers as well as
111      * all the other classes of the Inversoft MVC.
112      *
113      * @param root The root element to parse from
114      * @param type The Class of the object type being built
115      * @param elemName The name of the Element to parse
116      */

117     private void buildMVCConfiguration(Element root, Class JavaDoc type, String JavaDoc elemName,
118             String JavaDoc errorName, Logger logger) {
119         List JavaDoc elems = root.getChildren(elemName);
120         Iterator JavaDoc iter = elems.iterator();
121         Element elem;
122         String JavaDoc name;
123         String JavaDoc className;
124         Class JavaDoc klass;
125         while (iter.hasNext()) {
126             elem = (Element) iter.next();
127             name = elem.getAttributeValue(Constants.NAME_ATTRIBUTE);
128             className = elem.getTextTrim();
129             try {
130                 klass = Class.forName(className);
131             } catch (ClassNotFoundException JavaDoc cne) {
132                 logger.error("Invalid " + errorName + " class: " + className);
133                 continue;
134             }
135
136             if (!type.isAssignableFrom(klass)) {
137                 logger.error("Invalid " + errorName + " class: " + className);
138                 continue;
139             }
140
141             Object JavaDoc obj;
142             try {
143                 obj = klass.newInstance();
144             } catch (InstantiationException JavaDoc ie) {
145                 logger.error("Error creating " + errorName + " :" + ie.toString());
146                 continue;
147             } catch (IllegalAccessException JavaDoc iae) {
148                 logger.error("Error creating " + errorName + " :" + iae.toString());
149                 continue;
150             }
151
152             if (type == ControllerParser.class) {
153                 MVCRegistry.register(name, (ControllerParser) obj);
154             } else if (type == ModelParser.class) {
155                 MVCRegistry.register(name, (ModelParser) obj);
156             } else if (type == ValidatorParser.class) {
157                 MVCRegistry.register(name, (ValidatorParser) obj);
158             } else if (type == ValidatorHandler.class) {
159                 MVCRegistry.register(name, (ValidatorHandler) obj);
160             } else if (type == ControllerHandler.class) {
161                 MVCRegistry.register(name, (ControllerHandler) obj);
162             } else if (type == ModelResolver.class) {
163                 MVCRegistry.register(name, (ModelResolver) obj);
164             } else if (type == ModelHandler.class) {
165                 MVCRegistry.register(name, (ModelHandler) obj);
166             } else {
167                 assert (false) : "FATAL: Invalid type";
168             }
169         }
170     }
171
172     /**
173      * Sets up the URL ending value if any.
174      */

175     private void buildMVCURLEnding(Element mvc, Logger logger) {
176         Element urlElem = mvc.getChild(Constants.URL_BEGINNING_ELEMENT);
177         if (urlElem != null) {
178             String JavaDoc ending = urlElem.getTextTrim();
179             if (!StringTools.isEmpty(ending)) {
180                 MVCURLTools.setURLEnding(ending);
181             } else {
182                 logger.error("URL ending value is empty or null");
183             }
184         }
185     }
186
187     /**
188      * Since the Verge configuration is dumped and completely reloaded on
189      * refreshes, this calls build because running .
190      *
191      * @param document The Document to parse and build from
192      * @param registry The registry to store the objects in
193      * @throws com.inversoft.config.ConfigurationException If there were any errors
194      */

195     public void rebuild(Document document, ConfigRegistry registry)
196     throws ConfigurationException {
197         build(document, registry);
198     }
199
200     /**
201      * No-op
202      */

203     public void validate(ConfigRegistry registry, Map JavaDoc otherRegistries) {
204         // no-op
205
}
206
207     /**
208      * No-op
209      */

210     public void commit(ConfigRegistry registry, Map JavaDoc otherRegistries) {
211         // no-op
212
}
213 }
Popular Tags