1 package de.webman.util.registry ; 2 3 import java.util.HashMap ; 4 import java.io.IOException ; 5 import javax.xml.parsers.DocumentBuilderFactory ; 6 import javax.xml.parsers.DocumentBuilder ; 7 import javax.xml.parsers.ParserConfigurationException ; 8 import org.w3c.dom.Node ; 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.Element ; 11 import org.xml.sax.SAXException ; 12 import org.xml.sax.InputSource ; 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.FileInputStream ; 17 import java.io.BufferedInputStream ; 18 import org.apache.log4j.Category; 19 20 21 64 public class Registry 65 { 66 67 68 71 private static Category cat = Category.getInstance(Registry.class); 72 73 76 public static final String START_LOAD_SCHEME = "start"; 77 78 81 public static final String LAZY_LOAD_SCHEME = "lazy"; 82 83 84 85 88 private static Registry singleton = null; 89 90 94 private HashMap instMgrs = new HashMap (); 95 96 102 private HashMap managers = new HashMap (); 103 104 107 private String basedir = "/"; 108 109 112 private boolean configuredYet = false; 113 114 115 116 119 122 private Registry() { 123 } 124 125 129 public static Registry getInstance() { 130 if (singleton == null) 131 singleton = new Registry(); 132 return singleton; 133 } 134 135 136 139 151 public boolean setConfiguration(String _basedir, String _cfg) 152 throws IOException , RegistryException 153 { 154 if (!configuredYet) { 155 basedir = _basedir; 156 readXMLStream(new BufferedInputStream (new FileInputStream (new File (_basedir, _cfg)))); 157 configuredYet = true; 158 return true; 159 } 160 return false; 161 } 162 163 166 private void readXMLStream(BufferedInputStream in) 167 throws IOException , RegistryException 168 { 169 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 170 factory.setNamespaceAware(false); 171 factory.setValidating(false); 172 173 Document doc = null; 174 try { 175 DocumentBuilder builder = factory.newDocumentBuilder(); 176 doc = builder.parse(new InputSource (in)); 177 } 178 catch (ParserConfigurationException pce) { 179 throw new RegistryException(pce); 180 } 181 catch (SAXException se) { 182 throw new RegistryException(se); 183 } 184 185 Node nd = doc.getDocumentElement(); 186 if (!("registry".equals(nd.getNodeName()))) 187 throw new RegistryException("bad root element '" + nd.getNodeName() + "'"); 188 189 for (Node c1 = nd.getFirstChild(); c1 != null; c1 = c1.getNextSibling()) { 190 if (c1.getNodeType() == Node.ELEMENT_NODE) { 191 if ("factories".equals(c1.getNodeName())) { 192 for (Node c2 = c1.getFirstChild(); c2 != null; c2 = c2.getNextSibling()) { 193 if (c2.getNodeType() == Node.ELEMENT_NODE) { 194 if ("manager".equals(c2.getNodeName())) { 195 String load_scheme = ((Element )c2).getAttribute("load-scheme"); 196 String factory_class = ((Element )c2).getAttribute("factory-class"); 197 198 registerFactoryClass(load_scheme, factory_class); 199 } 200 else 201 throw new RegistryException("unknown element: '" + c2.getNodeName() + "'"); 202 } 203 } 204 } 205 else 206 throw new RegistryException("unknown element: '" + c1.getNodeName() + "'"); 207 } 208 } 209 } 210 211 214 private String getTextData(Node cntx) { 215 cntx.normalize(); 216 217 for (Node n = cntx.getFirstChild(); n != null; n = n.getNextSibling()) { 218 if (n.getNodeType() == Node.TEXT_NODE) { 219 return n.getNodeValue(); 220 } 221 else if (n.getNodeType() == Node.CDATA_SECTION_NODE) { 222 return n.getNodeValue(); 223 } 224 } 225 return null; 226 } 227 228 229 242 private void registerFactoryClass(String load_scheme, String factory_class) { 243 try { 244 Class factclass = Class.forName(factory_class); 245 246 ManagerFactory fact = (ManagerFactory)factclass.newInstance(); 247 String mgrid = fact.getID(); 248 249 if (START_LOAD_SCHEME.equals(load_scheme)) { 250 try { 251 instAndRegisterManager(fact); 252 managers.put(mgrid, fact); 253 cat.debug("Loading manager factory '" + fact.getID() + "': success"); 254 } 255 catch (RegistryException re) { 256 cat.error ("Manager could not be instanciated: " + re); 257 259 } 260 } 261 else if (LAZY_LOAD_SCHEME.equals(load_scheme)) { 262 cat.debug("Loading manager factory '" + fact.getID() + "': success"); 263 managers.put(mgrid, fact); 264 } 265 else { 266 cat.debug("Loading manager factory '" + fact.getID() + "': success"); 267 managers.put(mgrid, fact); 268 } 269 } 270 catch (ClassNotFoundException cnfe) { 271 cat.error("Class not found: '" + cnfe + "'"); 272 } 273 catch (IllegalAccessException iae) { 274 cat.error("Class could not be instantiated: " + iae); 275 } 276 catch (InstantiationException ie) { 277 cat.error("Class could not be instantiated: " + ie); 278 } 279 catch (ClassCastException cce) { 280 cat.error("Class does not implement ManagerFactory: " + cce); 281 } 282 } 283 284 285 286 289 300 public Manager lookup(String mgrid) 301 throws RegistryException 302 { 303 Manager mgr = (Manager)instMgrs.get(mgrid); 304 305 if (mgr == null) { 306 ManagerFactory fact = (ManagerFactory) managers.get(mgrid); 307 if (fact != null) 308 mgr = instAndRegisterManager(fact); 309 } 310 311 return mgr; 312 } 313 314 317 private Manager instAndRegisterManager(ManagerFactory mgrfact) 318 throws RegistryException 319 { 320 Manager mgr = mgrfact.newManager(basedir); 321 instMgrs.put(mgrfact.getID(), mgr); 322 return mgr; 323 } 324 } 325 326 | Popular Tags |