1 package org.sapia.regis; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.util.Properties ; 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 63 public class RegistryContext { 64 65 69 public static final String FACTORY_CLASS = "org.sapia.regis.factory"; 70 71 75 public static final String BOOTSTRAP = "org.sapia.regis.bootstrap"; 76 77 private Properties _props; 78 79 83 public RegistryContext(Properties props){ 84 _props = props; 85 } 86 87 91 public Registry connect() throws Exception { 92 _props = Utils.replaceVars(new PropertiesContext(_props, new SystemContext()), _props); 93 String bootstrap = _props.getProperty(BOOTSTRAP); 94 if(bootstrap != null){ 95 loadBootstrap(bootstrap); 96 } 97 String className = _props.getProperty(FACTORY_CLASS); 98 if(className == null){ 99 throw new IllegalArgumentException ("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 bootstrap) throws Exception { 106 String [] 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 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 e){ 118 continue; 119 } 120 } 121 throw new FileNotFoundException ("Could not load any bootstrap resource: " + bootstrap); 122 } 123 124 125 } 126 | Popular Tags |