KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > config > component > ComponentConfigBuilderRegistry


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.config.component;
8
9
10 import java.util.Collections;
11 import java.util.Enumeration;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.MissingResourceException;
15 import java.util.ResourceBundle;
16
17 import com.inversoft.config.ConfigurationException;
18 import com.inversoft.error.ErrorList;
19 import com.inversoft.util.ReflectionException;
20 import com.inversoft.util.ReflectionTools;
21
22
23 /**
24  * <p>
25  * This class stores the {@link ComponentConfigBuilder
26  * ComponentConfigBuilder} implementations that are used to
27  * parse and build the configuration for a component.
28  * </p>
29  *
30  * <p>
31  * This class uses ResourceBundles similar to the {@link
32  * com.inversoft.config.ConfigFactoryRegistry
33  * ConfigFactoryRegistry} class, but these are more
34  * configurable because each instance of this class can use
35  * a different bundle. The name of the bundle is provided
36  * during construction.
37  * </p>
38  *
39  * @author Brian Pontarelli
40  * @since 2.0
41  * @version 2.0
42  */

43 public class ComponentConfigBuilderRegistry {
44
45     /**
46      * The map of builders
47      */

48     private final Map builders = Collections.synchronizedMap(new HashMap());
49
50
51     /**
52      * <p>
53      * Attempt to read from the resource bundle to load the {@link
54      * ComponentConfigBuilder ComponentConfigBuilder} implementations.
55      * </p>
56      *
57      * @param bundleName The name of the bundle that is used to load the
58      * ConfigBuilder implementations
59      * @throws ConfigurationException If the bundleName is invalid or any of
60      * the entries are not ConfigBuilder implementations
61      */

62     public ComponentConfigBuilderRegistry(String bundleName)
63     throws ConfigurationException {
64
65         ResourceBundle bundle = null;
66         try {
67             bundle = ResourceBundle.getBundle(bundleName);
68         } catch (MissingResourceException mre) {
69             throw new ConfigurationException(mre);
70         }
71
72         Enumeration enum = bundle.getKeys();
73         String key;
74         String className;
75         Class klass;
76         ComponentConfigBuilder builder;
77         ErrorList errors = new ErrorList();
78         while (enum.hasMoreElements()) {
79             key = (String) enum.nextElement();
80             className = bundle.getString(key);
81
82             try {
83                 klass = ReflectionTools.findClass(className);
84             } catch (ReflectionException re) {
85                 errors.addError(re.toString());
86                 continue;
87             }
88
89             if (!ComponentConfigBuilder.class.isAssignableFrom(klass)) {
90                 errors.addError(klass.getName() + " is not a ConfigBuilder");
91                 continue;
92             }
93
94             try {
95                 builder = (ComponentConfigBuilder) ReflectionTools.instantiate(klass);
96                 builder.setRegistry(this);
97             } catch (ReflectionException re) {
98                 errors.addError(re.toString());
99                 continue;
100             }
101
102             register(key, builder);
103         }
104
105         if (!errors.isEmpty()) {
106             throw new ConfigurationException(errors);
107         }
108     }
109
110
111     /**
112      * <p>
113      * Registers the given {@link ComponentConfigBuilder ComponentConfigBuilder}
114      * under the given name.
115      * </p>
116      *
117      * @param name The name to register the builder under
118      * @param builder The ConfigBuilder to register
119      */

120     public void register(String name, ComponentConfigBuilder builder) {
121         builders.put(name, builder);
122         builder.setRegistry(this);
123     }
124
125     /**
126      * <p>
127      * Unregisters the {@link ComponentConfigBuilder ComponentConfigBuilder} that
128      * was previously registered under the given name.
129      * </p>
130      *
131      * @param name The name to unregister
132      * @return The previous registered ConfigBuilder or null if none was ever
133      * registered
134      */

135     public ComponentConfigBuilder unregister(String name) {
136         return (ComponentConfigBuilder) builders.remove(name);
137     }
138
139     /**
140      * <p>
141      * Finds and returns the ConfigBuilder registered under the given name
142      * </p>
143      *
144      * @param name The name the ConfigBuilder was previously registered under
145      * or the key from the resource bundle
146      * @return The ConfigBuilder or null if none was ever registered
147      */

148     public ComponentConfigBuilder lookup(String name) {
149         return (ComponentConfigBuilder) builders.get(name);
150     }
151 }
152
153
Popular Tags