KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > RegistryContext


1 package org.sapia.regis;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import org.sapia.regis.util.PropertiesContext;
8 import org.sapia.regis.util.Utils;
9 import org.sapia.util.text.SystemContext;
10 import org.sapia.util.text.TemplateException;
11 import org.sapia.util.text.TemplateFactory;
12
13 /**
14  * An instance of this class internally uses a given <code>RegistryFactory</code> class
15  * to instantiate a <code>Registry</code>; it expects the <code>org.sapia.regis.factory</code>
16  * property to correspond to the name of the <code>RegistryFactory</code> implementation
17  * to use.
18  * <p>
19  * Additionally, an instance of this class allows setting a property indicating one or multiple
20  * Java property resources to load within this instance's given properties (which are passed at
21  * instantiation time). That property is: <code>org.sapia.regis.boostrap</code>, and allows
22  * setting a comma-delimited list of resources are to be exclusively loaded. Each resource is
23  * interpreted as being either (whichever matches first):
24  * <p>
25  * <ul>
26  * <li>a file
27  * <li>a URL
28  * <li>a classpath resource
29  * </ul>
30  * The first resource that can be loaded will stop the loading process (i.e.: the Java properties
31  * in that resource will be loaded, but attempting to load the other resources will abort).
32  * <p>
33  * Note that specified resource names can contain variables of the form <code>${var_name}</code>. These
34  * variables are resolved using the properties passed to the instance of this class at construction
35  * time, or using system properties.
36  * <p>
37  * The properties in the resource that was found will be added to the ones passed to the instance
38  * of this class at contruction time, prior to the actual registry factory being instantiated.
39  * <p>
40  * This feature was introduced to allow connecting to different registries, depending on the environment.
41  * For example, imagine that a <code>registry.properties</code> file is kept in the user home directory on developer
42  * workstations. That file contains the properties necessary to connect to a {@link org.sapia.regis.local.LocalRegistry}
43  * (in order to avoid dealing with remote connections when developing). But let's say that when in other environments
44  * (dev, QA, prod...), the <code>registry.properties</code> file to use is stored by convention in the classpath, under the
45  * path <code>regis/conf/registry.properties</code>, and holds properties used to connect to a remote registry, shared by
46  * distributed applications (see {@link org.sapia.regis.remote.RemoteRegistry}).
47  * <p>
48  * In order to load the appropriate <code>registry.properties</code>, we could set a property (in the <code>Properties</code> passed to the
49  * constructor of this class) :
50  * <pre>
51  * org.sapia.regis.bootstrap=${user.home}/regis/registry.properties, regis/conf/registry.properties
52  * </pre>
53  * <p>
54  * Then, upon its <code>connect()</code> method being called, the instance of this class resolves the boostrap properties (according
55  * to the above-described algorithm), which add themselves to the properties that were passed in at construction time. Thus, the
56  * boostrap properties are additive, and are used just as the others when instantiation registry factory.
57  *
58  * @see org.sapia.regis.RegistryFactory
59  *
60  * @author yduchesne
61  *
62  */

63 public class RegistryContext {
64   
65   /**
66    * This constant corresponds to the property that indicates which <code>RegistryFactory</code>
67    * to use to instantiate a <code>Registry</code>.
68    */

69   public static final String JavaDoc FACTORY_CLASS = "org.sapia.regis.factory";
70   
71   /**
72    * This constant corresponds to the property that indicates which Java propertis (file, URL, classpath resource)
73    * to use to initialiase an instance of this class.
74    */

75   public static final String JavaDoc BOOTSTRAP = "org.sapia.regis.bootstrap";
76   
77   private Properties JavaDoc _props;
78
79   /**
80    * @param props the <code>Properties</code> to used to connect to the desired
81    * <code>Registry</code>.
82    */

83   public RegistryContext(Properties JavaDoc props){
84     _props = props;
85   }
86   
87   /**
88    * @return a <code>Registry</code>.
89    * @throws Exception if a problem occurs while attempting to connect.
90    */

91   public Registry connect() throws Exception JavaDoc{
92     _props = Utils.replaceVars(new PropertiesContext(_props, new SystemContext()), _props);
93     String JavaDoc bootstrap = _props.getProperty(BOOTSTRAP);
94     if(bootstrap != null){
95       loadBootstrap(bootstrap);
96     }
97     String JavaDoc className = _props.getProperty(FACTORY_CLASS);
98     if(className == null){
99       throw new IllegalArgumentException JavaDoc("Property not specified: " + FACTORY_CLASS + "; got: " + _props);
100     }
101     RegistryFactory factory = (RegistryFactory)Class.forName(className).newInstance();
102     return factory.connect(_props);
103   }
104   
105   private void loadBootstrap(String JavaDoc bootstrap) throws Exception JavaDoc{
106     String JavaDoc[] resources = bootstrap.split(",");
107     TemplateFactory fac = new TemplateFactory();
108     PropertiesContext context = new PropertiesContext(_props, new SystemContext());
109     for(int i = 0; i < resources.length; i++){
110       String JavaDoc resource = resources[i].trim();
111       try{
112         resource = fac.parse(resource).render(context);
113         Utils.loadProps(RegistryContext.class, _props, resource);
114         return;
115       }catch(TemplateException e){
116         continue;
117       }catch(IOException JavaDoc e){
118         continue;
119       }
120     }
121     throw new FileNotFoundException JavaDoc("Could not load any bootstrap resource: " + bootstrap);
122   }
123   
124
125 }
126
Popular Tags