KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > config > ConfigFactoryRegistry


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;
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.ResourceBundle;
15
16 import org.apache.log4j.Logger;
17
18 import com.inversoft.util.ReflectionException;
19 import com.inversoft.util.ReflectionTools;
20
21
22 /**
23  * <p>
24  * This class stores the {@link ConfigFactory ConfigFactory}
25  * implementations that are used by the configuration system.
26  * This version of the ConfigFactoryRegistry can to load
27  * ConfigFactory instances from a properties file or the
28  * factories can be registered individually. The name that
29  * the ConfigFactory's are stored under is the key in the
30  * properties file. The value in the properties file is the
31  * full name of the ConfigFactory implementation class.
32  * </p>
33  *
34  * <p>
35  * By default this class is empty. The
36  * {@link #load(ResourceBundle)} method can be called
37  * to parse configuration from a resource bundle as described
38  * above. Otherwise, the {@link #register(String, ConfigFactory)}
39  * method can be called.
40  * </p>
41  *
42  * <p>
43  * For more information about the keys that the ConfigFactory
44  * Objects are registered under see the {@link ConfigMediator
45  * ConfigMediator} class.
46  * </p>
47  *
48  * @author Brian Pontarelli
49  * @since 2.0
50  * @version 2.0
51  */

52 public class ConfigFactoryRegistry {
53
54     /**
55      * This classes logger
56      */

57     private static final Logger logger = Logger.getLogger(ConfigFactoryRegistry.class);
58
59     /**
60      * The map of factories
61      */

62     private static final Map factories = Collections.synchronizedMap(new HashMap());
63
64
65     /**
66      * <p>
67      * Attempt to read from the resource bundle to load the factories. Since the
68      * Map of factories is synchronized, this locks the map while running to ensure
69      * freshness (hehe).
70      * </p>
71      *
72      * <p>
73      * All old factories are replaced when calling this method.
74      * </p>
75      */

76     public static void load(ResourceBundle bundle) {
77
78         synchronized(factories) {
79             Enumeration enum = bundle.getKeys();
80             String key;
81             String className;
82             Class klass;
83             ConfigFactory factory;
84             while (enum.hasMoreElements()) {
85                 key = (String) enum.nextElement();
86                 className = bundle.getString(key);
87
88                 try {
89                     klass = ReflectionTools.findClass(className, null);
90                 } catch (ReflectionException re) {
91                     logger.info("Configuration factory could not be loaded. " +
92                         "A system may be disabled." + re.toString());
93                     continue;
94                 }
95
96                 if (!ConfigFactory.class.isAssignableFrom(klass)) {
97                     logger.error(klass.getName() + " is not a ConfigFactory");
98                     continue;
99                 }
100
101                 try {
102                     factory = (ConfigFactory) ReflectionTools.instantiate(klass);
103                 } catch (ReflectionException re) {
104                     logger.error(re.toString());
105                     continue;
106                 }
107
108                 register(key, factory);
109             }
110         }
111     }
112
113     /**
114      * Constructs a new <code>ConfigFactoryRegistry</code>.
115      */

116     private ConfigFactoryRegistry() {
117     }
118
119
120     /**
121      * <p>
122      * Registers the given {@link ConfigFactory ConfigFactory} under the given
123      * name.
124      * </p>
125      *
126      * @param name The name to register the factory under
127      * @param factory The ConfigFactory to register
128      */

129     public static void register(String name, ConfigFactory factory) {
130         factories.put(name, factory);
131     }
132
133     /**
134      * <p>
135      * Unregisters the {@link ConfigFactory ConfigFactory} that was previously
136      * registered under the given name.
137      * </p>
138      *
139      * @param name The name to unregister
140      * @return The previous registered ConfigFactory or null if none was ever
141      * registered
142      */

143     public static ConfigFactory unregister(String name) {
144         return (ConfigFactory) factories.remove(name);
145     }
146
147     /**
148      * <p>
149      * Finds and returns the ConfigFactory registered under the given name
150      * </p>
151      *
152      * @param name The name the ConfigFactory was previously registered under
153      * or the key from the resource bundle
154      * @return The ConfigFactory or null if none was ever registered
155      */

156     public static ConfigFactory lookup(String name) {
157         return (ConfigFactory) factories.get(name);
158     }
159
160     /**
161      * Returns a Map that contains all the factories configured for configuration
162      * system. This Map is not live and changes will not effect the state of this
163      * Registry.
164      *
165      * @return A Map containing all of the registered factories
166      */

167     public static Map allFactories() {
168         return new HashMap(factories);
169     }
170 }
Popular Tags