1 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 52 public class ConfigFactoryRegistry { 53 54 57 private static final Logger logger = Logger.getLogger(ConfigFactoryRegistry.class); 58 59 62 private static final Map factories = Collections.synchronizedMap(new HashMap()); 63 64 65 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 116 private ConfigFactoryRegistry() { 117 } 118 119 120 129 public static void register(String name, ConfigFactory factory) { 130 factories.put(name, factory); 131 } 132 133 143 public static ConfigFactory unregister(String name) { 144 return (ConfigFactory) factories.remove(name); 145 } 146 147 156 public static ConfigFactory lookup(String name) { 157 return (ConfigFactory) factories.get(name); 158 } 159 160 167 public static Map allFactories() { 168 return new HashMap(factories); 169 } 170 }
| Popular Tags
|