KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > repository > config > RepositoryConfigBuilder


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.repository.config;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Properties 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.config.component.ComponentConfigBuilderRegistry;
22 import com.inversoft.error.ErrorList;
23
24
25 /**
26  * <p>
27  * This class is the master builder for the repository. This uses
28  * the BuilderRegistry to locate individual Builders for the
29  * different types of items that might be stored in the repository.
30  * </p>
31  *
32  * <p>
33  * This builder uses the the BuilderRegistry to locate all the
34  * Builder instances to be used.
35  * </p>
36  *
37  * @author Brian Pontarelli
38  * @since 2.0
39  * @version 2.0
40  */

41 public class RepositoryConfigBuilder implements ConfigBuilder {
42
43     /**
44      * This class' logger
45      */

46     private static final Logger logger = Logger.getLogger(RepositoryConfigBuilder.class);
47
48     /**
49      * The name of the resource bundle that stores the component config buidlers
50      * for the repository system
51      */

52     public static final String JavaDoc BUNDLE_NAME =
53         "com.inversoft.verge.repository.config.ConfigBuilders";
54
55     /**
56      * The component config builder registry
57      */

58     private ComponentConfigBuilderRegistry builderRegistry;
59
60
61     /**
62      * Constructs a new <code>RepositoryConfigBuilder</code>
63      */

64     public RepositoryConfigBuilder() {
65         try {
66             builderRegistry = new ComponentConfigBuilderRegistry(BUNDLE_NAME);
67         } catch (ConfigurationException ce) {
68             assert (false) : ce.toString();
69         }
70     }
71
72
73     /**
74      * <p>
75      * This method does the actual building of the repository configuration.
76      * This uses the builder registry and all the Builders stored in it to build
77      * the configuration objects, which are stored in the
78      * RepositoryConfigRegistry object.
79      * </p>
80      *
81      * <p>
82      * This method, like all the configuration methods, is synchronized. This is
83      * not to prevent multiple thread usage but to enforce a proper write of the
84      * ConfigRegistry Object from Thread local memory to the JVM heap memory.
85      * This will ensure that when the ConfigRegistry is later set into the
86      * singleton, which is an atomic operation, it will be correctly populated.
87      * </p>
88
89      * @param document The document currently being parsed and built by the
90      * Inversoft configuration system
91      * @param registry The ConfigRegistry where all the created objects are
92      * stored
93      * @throws ConfigurationException If anything goes wrong
94      */

95     public synchronized void build(Document document, ConfigRegistry registry)
96     throws ConfigurationException {
97
98         // Global variables
99
ErrorList errors = new ErrorList();
100
101         // Variables for building the config objects
102
Iterator JavaDoc iter = document.getRootElement().getChildren().iterator();
103         Element currentElement;
104         Builder builder;
105
106         while (iter.hasNext()) {
107             currentElement = (Element) iter.next();
108             builder = (Builder) builderRegistry.lookup(currentElement.getName());
109
110             try {
111                 builder.build(currentElement, (RepositoryConfigRegistry) registry);
112             } catch (ConfigurationException ce) {
113                 logger.error("Error building item", ce);
114                 errors.addErrorList(ce.getErrors());
115                 continue;
116             }
117         }
118
119         if (!errors.isEmpty()) {
120             throw new ConfigurationException(errors);
121         }
122     }
123
124     /**
125      * Since the Portal configuration is dumped and completely reloaded on
126      * refreshes, this calls build because the registry should be fresh.
127      *
128      * @param document The Document to parse and build from
129      * @param registry The registry to store the objects in
130      * @throws ConfigurationException If there were any errors
131      */

132     public void rebuild(Document document, ConfigRegistry registry)
133     throws ConfigurationException {
134         build(document, registry);
135     }
136
137     /**
138      * Validates the entire configuration after all the configuration has been
139      * read in. Currently, this checks to make sure that references between
140      * repository items are all valid.
141      *
142      * @param registry The RepositoryConfigRegistry being built
143      * @param otherRegistries All the registry objects for the entire Portal
144      * framework configuration
145      * @throws ConfigurationException If anything is invalid
146      */

147     public void validate(ConfigRegistry registry, Map JavaDoc otherRegistries)
148     throws ConfigurationException {
149
150         RepositoryConfigRegistry repositoryRegistry =
151             (RepositoryConfigRegistry) registry;
152
153         ErrorList errors = new ErrorList();
154         Iterator JavaDoc iter = repositoryRegistry.getConfigurations().iterator();
155         Config config;
156         Properties JavaDoc refs;
157         Iterator JavaDoc refIter;
158         String JavaDoc refName;
159
160         while (iter.hasNext()) {
161             config = (Config) iter.next();
162             refs = config.getReferences();
163
164             refIter = refs.values().iterator();
165             while (refIter.hasNext()) {
166                 refName = (String JavaDoc) refIter.next();
167                 if (repositoryRegistry.lookup(refName) == null) {
168                     errors.addError("Invalid reference: " + refName);
169                 }
170             }
171         }
172
173         if (!errors.isEmpty()) {
174             throw new ConfigurationException(errors);
175         }
176     }
177
178     /**
179      * If the validation was successfull, the registry should be all setup so we
180      * can go ahead and set the singleton instance.
181      *
182      * @param registry The RepositoryConfigRegistry that was just built
183      * @param otherRegistries not used
184      */

185     public void commit(ConfigRegistry registry, Map JavaDoc otherRegistries) {
186         RepositoryConfigRegistry.setInstance((RepositoryConfigRegistry) registry);
187     }
188 }
Popular Tags