1 package com.jcorporate.expresso.core.db; 2 3 import com.jcorporate.expresso.core.dataobjects.PersistenceManager; 4 import com.jcorporate.expresso.core.db.config.JDBCConfig; 5 import com.jcorporate.expresso.core.dbobj.ValidValue; 6 import com.jcorporate.expresso.kernel.ComponentLifecycle; 7 import com.jcorporate.expresso.kernel.Configuration; 8 import com.jcorporate.expresso.kernel.EmbeddedComponent; 9 import com.jcorporate.expresso.kernel.exception.ConfigurationException; 10 import com.jcorporate.expresso.kernel.metadata.ComponentMetadata; 11 import com.jcorporate.expresso.kernel.metadata.SimpleProperty; 12 import org.apache.commons.digester.Digester; 13 import org.apache.log4j.Logger; 14 import org.xml.sax.SAXException ; 15 16 import java.io.IOException ; 17 import java.lang.ref.SoftReference ; 18 import java.net.URL ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 23 28 29 public class DBConfig extends EmbeddedComponent implements ComponentLifecycle { 30 31 35 private SoftReference allConfigurations = null; 36 37 38 41 static final String CONFIG_PACKAGE = "com/jcorporate/expresso/core/db/config/"; 42 43 46 private JDBCConfig currentConfig = null; 47 48 51 private Logger log = Logger.getLogger(DBConfig.class); 52 53 56 public DBConfig() { 57 } 58 59 65 public String getMetadataName() { 66 return "DBConfig"; 67 } 68 69 72 public void initialize() { 73 74 } 75 76 84 public void configure(Configuration newConfig) throws ConfigurationException { 85 if (!(this.getParent() instanceof PersistenceManager)) { 86 throw new ConfigurationException("DBConfig must be a subcomponent of Persistence Manager"); 87 } 88 89 PersistenceManager pm = (PersistenceManager) this.getParent(); 90 String configName = pm.getConfigName(); 91 if (configName != null) { 92 try { 93 currentConfig = this.buildOneConfiguration(configName, buildDigester()); 94 } catch (SAXException ex) { 95 log.error("SAX Error on config parsing", ex); 96 throw new ConfigurationException("Error parsing configuration", ex); 97 } catch (IOException ex) { 98 log.error("IOException parsing db configuration", ex); 99 throw new ConfigurationException("Error parsing configuration", ex); 100 } catch (ConfigurationException ex) { 101 log.error("Configuraiton Exception parsing db configuration", ex); 102 throw new ConfigurationException("Error parsing configuration", ex); 103 } 104 } 105 } 106 107 108 116 public void reconfigure(Configuration newConfig) throws ConfigurationException { 117 currentConfig = null; 118 configure(newConfig); 119 } 120 121 124 public void destroy() { 125 126 } 127 128 134 public List getSupportedConfigurations() { 135 if (log.isDebugEnabled()) { 136 log.debug("Retrieving valid value list for all supported configurations"); 137 } 138 139 ComponentMetadata myMetadata = this.getMetaData(); 140 SimpleProperty prop = (SimpleProperty) myMetadata.getProperties().get("ConfigName"); 141 return prop.retrieveValidValues(); 142 143 } 144 145 150 public synchronized List getAllConfigurations() { 151 if (allConfigurations == null || allConfigurations.get() == null) { 152 if (log.isDebugEnabled()) { 153 log.debug("No allConfigurations list in memory. Building from scratch"); 154 } 155 List newConfigList = buildAllConfigurationsList(); 156 allConfigurations = new SoftReference (newConfigList); 157 } 158 159 return (List ) allConfigurations.get(); 160 } 161 162 167 public JDBCConfig getCurrentConfig() { 168 return currentConfig; 169 } 170 171 177 private List buildAllConfigurationsList() { 178 List l = this.getSupportedConfigurations(); 179 if (l == null || l.size() == 0) { 180 throw new IllegalArgumentException ("No valid database configuration files present"); 181 } 182 183 List allConfigurations = new java.util.ArrayList (l.size()); 184 185 try { 186 Digester d = buildDigester(); 187 for (Iterator i = l.iterator(); i.hasNext();) { 188 ValidValue vv = (ValidValue) i.next(); 189 try { 190 JDBCConfig config = buildOneConfiguration(CONFIG_PACKAGE + vv.getKey(), d); 191 allConfigurations.add(config); 192 } catch (IOException ex) { 193 log.error("Error digesting configuration: " + vv.getKey(), ex); 194 } catch (SAXException ex) { 195 log.error("Error digesting configuration: " + vv.getKey(), ex); 196 } catch (ConfigurationException ex) { 197 log.error("Error building JDBC Config: " + vv.getKey(), ex); 198 } 199 200 } 201 } catch (ConfigurationException ex) { 202 log.error("Error building digester", ex); 203 return new java.util.ArrayList (0); 204 } 205 206 return null; 207 } 208 209 219 private JDBCConfig buildOneConfiguration(String fileName, Digester d) 220 throws ConfigurationException, IOException , SAXException { 221 222 java.io.InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_PACKAGE 223 + fileName); 224 225 JDBCConfig configuration; 226 if (is == null) { 227 throw new ConfigurationException("Unable to locate resource : " + fileName); 228 } else { 229 configuration = (JDBCConfig) d.parse(is); 230 } 231 232 try { 233 PersistenceManager manager = (PersistenceManager) this.getParent(); 234 configuration.setUrl(manager.getDataURL()); 235 configuration.setPassword(manager.getPassword()); 236 configuration.setLogin(manager.getUserName()); 237 } finally { 238 is.close(); 239 } 240 241 return configuration; 242 } 243 244 250 private Digester buildDigester() throws ConfigurationException { 251 if (log.isDebugEnabled()) { 252 log.debug("Building Digester Rules for Database Configuration File"); 253 } 254 255 Digester digester = new Digester(); 256 257 258 URL url = Thread.currentThread().getContextClassLoader().getResource(CONFIG_PACKAGE + "db-config.dtd"); 259 260 if (url != null) { 261 digester.register("-//Jcorporate Ltd//DTD Expresso Database Configuration 5.1//EN", 262 url.toString()); 263 } else { 264 throw new ConfigurationException("Unable to get URL to the db-config.dtd"); 265 } 266 267 JDBCConfig myConfig = new JDBCConfig(); 268 269 ClassLoader cl = DBConfig.class.getClassLoader(); 270 if (cl != null) { 271 digester.setClassLoader(cl); 272 } else { 273 digester.setClassLoader(Thread.currentThread().getContextClassLoader()); 274 } 275 digester.push(myConfig); 277 278 279 280 281 digester.addCallMethod("db-config/jdbc/dbWildcard", 282 "addWildcard", 0); 283 digester.addSetProperties("db-config/jdbc"); 284 285 286 287 digester.addObjectCreate("db-config/type-mapping", 288 com.jcorporate.expresso.core.db.config.TypeMappingConfig.class.getName()); 289 digester.addSetNext("db-config/type-mapping", 290 "addTypeMapping", 291 com.jcorporate.expresso.core.db.config.TypeMappingConfig.class.getName()); 292 digester.addCallMethod("db-config/type-mapping/java-type", 293 "setJavaType", 0); 294 digester.addCallMethod("db-config/type-mapping/db-type", 295 "setDBType", 0); 296 digester.addCallMethod("db-config/type-mapping/expresso-type", 297 "setExpressoType", 0); 298 299 digester.setValidating(true); 300 301 return digester; 302 } 303 304 } 305 | Popular Tags |