KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > util > url > config > URLConfigBuilder


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.util.url.config;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.jdom.Document;
14 import org.jdom.Element;
15
16 import com.inversoft.config.ConfigBuilder;
17 import com.inversoft.config.ConfigRegistry;
18 import com.inversoft.config.ConfigurationException;
19 import com.inversoft.error.ErrorList;
20
21
22 /**
23  * <p>
24  * This class is the configuration builder for the URL
25  * system.
26  * </p>
27  *
28  * @author Brian Pontarelli
29  * @since 2.0
30  * @version 2.0
31  */

32 public class URLConfigBuilder implements ConfigBuilder {
33
34
35     /**
36      * Constructor for URLConfigBuilder.
37      */

38     public URLConfigBuilder() {
39         // Empty
40
}
41
42
43     /**
44      * <p>
45      * Builds the URL configuration from the given document. This builds all the
46      * categories defined in the given document and stores the newly created
47      * CategoryConfig Objects into the given ConfigRegistry
48      * </p>
49      *
50      * <p>
51      * This method, like all the configuration methods, is synchronized. This is
52      * not to prevent multiple thread usage but to enforce a proper write of the
53      * ConfigRegistry Object from Thread local memory to the JVM heap memory.
54      * This will ensure that when the ConfigRegistry is later set into the
55      * singleton, which is an atomic operation, it will be correctly populated.
56      * </p>
57
58      * @param document The XML Document containing the URL configuration
59      * @param registry The URLConfigRegistry used to store the config objects
60      * @throws ConfigurationException If the URL configuration is invalid
61      * @see ConfigBuilder#build(Document, ConfigRegistry)
62      */

63     public synchronized void build(Document document, ConfigRegistry registry)
64     throws ConfigurationException {
65         ErrorList errors = new ErrorList();
66         Element root = document.getRootElement();
67         Iterator JavaDoc iter = root.getChildren(Constants.CATEGORY_ELEMENT).iterator();
68         URLConfigRegistry reg = (URLConfigRegistry) registry;
69         Element category;
70         String JavaDoc name;
71         String JavaDoc protocol;
72         String JavaDoc host;
73         String JavaDoc port;
74         String JavaDoc context;
75         int portInt;
76         CategoryConfig cc;
77         boolean error;
78         
79         while (iter.hasNext()) {
80             category = (Element) iter.next();
81             error = false;
82             portInt = -1;
83
84             name = category.getAttributeValue(Constants.NAME_ATTRIBUTE);
85             if (name == null) {
86                 errors.addError("Name required for URL category");
87                 error = true;
88             } else if (reg.lookupCategory(name) != null) {
89                 errors.addError("Category named: " + name + " defined twice");
90                 error = true;
91             }
92
93             protocol = category.getChildText(Constants.PROTOCOL_ELEMENT);
94             if (protocol != null && !protocol.equalsIgnoreCase("http") &&
95                     !protocol.equalsIgnoreCase("https")) {
96                 errors.addError("Invalid protocol: " + protocol);
97                 error = true;
98             }
99             
100             host = category.getChildText(Constants.HOST_ELEMENT);
101             context = category.getChildText(Constants.CONTEXT_ELEMENT);
102             port = category.getChildText(Constants.PORT_ELEMENT);
103             
104             if (port != null) {
105                 try {
106                     portInt = Integer.parseInt(port);
107
108                     if (portInt <= 0) {
109                         errors.addError("Invalid port number: " + port);
110                         error = true;
111                     }
112                 } catch (NumberFormatException JavaDoc nfe) {
113                     errors.addError("Invalid port number: " + port);
114                     error = true;
115                 }
116             }
117
118             
119             if (error) {
120                 continue;
121             }
122
123             cc = new CategoryConfig(name, protocol, host, portInt, context);
124             reg.register(cc);
125         }
126         
127         if (!errors.isEmpty()) {
128             throw new ConfigurationException(errors);
129         }
130     }
131
132     /**
133      * Rebuilds the URL configuration from the given document. This is done by
134      * calling the build method.
135      *
136      * @param document The XML Document containing the URL configuration
137      * @param registry The URLConfigRegistry used to store the config objects
138      * @throws ConfigurationException If the URL configuration is invalid
139      * @see ConfigBuilder#rebuild(Document, ConfigRegistry)
140      */

141     public void rebuild(Document document, ConfigRegistry registry)
142     throws ConfigurationException {
143         build(document, registry);
144     }
145
146     /**
147      * Stores the newly created ConfigRegistry into the singleton
148      *
149      * @see ConfigBuilder#validate(ConfigRegistry, Map)
150      */

151     public void validate(ConfigRegistry registry, Map JavaDoc otherRegistries)
152     throws ConfigurationException {
153         // No-Op
154
}
155
156     /**
157      * If the validation was successfull, the registry should be all setup so we
158      * can go ahead and set the singleton instance.
159      *
160      * @param registry The URLConfigRegistry that was just built
161      * @param otherRegistries not used
162      */

163     public void commit(ConfigRegistry registry, Map JavaDoc otherRegistries) {
164         URLConfigRegistry reg = (URLConfigRegistry) registry;
165         URLConfigRegistry.setInstance(reg);
166     }
167 }
Popular Tags