KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > local > LocalRegistryFactory


1 package org.sapia.regis.local;
2
3 import java.io.InputStream JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import org.sapia.regis.RWNode;
9 import org.sapia.regis.Registry;
10 import org.sapia.regis.RegistryFactory;
11 import org.sapia.regis.impl.NodeImpl;
12 import org.sapia.regis.loader.RegistryConfigLoader;
13 import org.sapia.regis.util.PropertiesContext;
14 import org.sapia.regis.util.Utils;
15 import org.sapia.util.text.SystemContext;
16
17 /**
18  * This class instantiates an in-memory registry that supports writes, but not transactions
19  * (all writes are instantly committed).
20  * <p>
21  * An instance of this class can be specified the path to an XML configuration resource
22  * that must be used to initialize the registry before it is returned:
23  * <p>
24  * <pre>
25  * Properties props = new Properties();
26  * props.setProperty(RegistryContext.FACTORY_CLASS,
27  * LocalRegistryFactory.class.getName());
28  * props.setProperty(LocalRegistryFactory.BOOTSTRAP,
29  * "org/acme/registry/config.xml");
30  * RegistryContext context = new RegistryContext();
31  * Registry regis = context.connect(props);
32  * </pre>
33  *
34  * <p>
35  * Note that the bootstrap property can be optionally specified to indicate a comma-delimited list
36  * of configuration resources that will be loaded at initialization of the local registry. Each resource
37  * is interpreted as file (in the file system), a URL, or a classpath resource, whatever works first.
38  *
39  * @see org.sapia.regis.local.LocalRegistry
40  *
41  * @author yduchesne
42  *
43  */

44 public class LocalRegistryFactory implements RegistryFactory{
45   
46   public static final String JavaDoc BOOTSTRAP = "org.sapia.regis.local.bootstrap";
47   
48   /**
49    * @see RegistryFactory#connect(Properties)
50    * @return a <code>LocalRegistry</code> instance.
51    */

52   public Registry connect(Properties JavaDoc props) throws Exception JavaDoc {
53     RWNode root = new NodeImpl();
54
55     props = Utils.replaceVars(new PropertiesContext(props, new SystemContext()), props);
56     String JavaDoc bootstrap = props.getProperty(BOOTSTRAP);
57
58     if(bootstrap != null){
59       String JavaDoc[] resources = bootstrap.split(",");
60       for(int i = 0; i < resources.length; i++){
61         RegistryConfigLoader loader = new RegistryConfigLoader(root);
62         InputStream JavaDoc is = Utils.load(LocalRegistryFactory.class, resources[i].trim());
63         try{
64           Map JavaDoc vars = new HashMap JavaDoc();
65           Utils.copyPropsToMap(vars, props);
66           loader.load(is, vars);
67         }finally{
68           is.close();
69         }
70       }
71     }
72     return new LocalRegistry(root);
73   }
74 }
75
Popular Tags