KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > db > DBConfig


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 JavaDoc;
15
16 import java.io.IOException JavaDoc;
17 import java.lang.ref.SoftReference JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22
23 /**
24  * Database Configuration Component
25  *
26  * @author Michael Rimov
27  */

28
29 public class DBConfig extends EmbeddedComponent implements ComponentLifecycle {
30
31     /**
32      * A list of all configurations. This way we can load them up and keep
33      * them in memory for a particular period of time.
34      */

35     private SoftReference JavaDoc allConfigurations = null;
36
37
38     /**
39      * Location of the config files.
40      */

41     static final String JavaDoc CONFIG_PACKAGE = "com/jcorporate/expresso/core/db/config/";
42
43     /**
44      *
45      */

46     private JDBCConfig currentConfig = null;
47
48     /**
49      * The log4j Logger
50      */

51     private Logger log = Logger.getLogger(DBConfig.class);
52
53     /**
54      * Default constructor.
55      */

56     public DBConfig() {
57     }
58
59     /**
60      * Retrieve the name of the metadata that is embedded in the parent
61      * component.
62      *
63      * @return java.lang.String
64      */

65     public String JavaDoc getMetadataName() {
66         return "DBConfig";
67     }
68
69     /**
70      * Initialize Lifecycle Event
71      */

72     public void initialize() {
73
74     }
75
76     /**
77      * Configuration Event. In this implementation there is no actual configuration
78      * parameters, but we get our configuration info from the parent.
79      *
80      * @param newConfig the new configuration bean.
81      * @throws ConfigurationException upon configuration error (such as bad
82      * nesting, for example)
83      */

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 JavaDoc configName = pm.getConfigName();
91         if (configName != null) {
92             try {
93                 currentConfig = this.buildOneConfiguration(configName, buildDigester());
94             } catch (SAXException JavaDoc ex) {
95                 log.error("SAX Error on config parsing", ex);
96                 throw new ConfigurationException("Error parsing configuration", ex);
97             } catch (IOException JavaDoc 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     /**
109      * ReconfigurationEvent In this implementation there is no actual configuration
110      * parameters, but we get our configuration info from the parent.
111      *
112      * @param newConfig the new configuration bean.
113      * @throws ConfigurationException upon configuration error (such as bad
114      * nesting, for example)
115      */

116     public void reconfigure(Configuration newConfig) throws ConfigurationException {
117         currentConfig = null;
118         configure(newConfig);
119     }
120
121     /**
122      * Destroy method.
123      */

124     public void destroy() {
125
126     }
127
128     /**
129      * Retrieves a list of all supported configuration keys and types as retrieved
130      * from the metadata for this component.
131      *
132      * @return java.util.List of ValidValue objects
133      */

134     public List JavaDoc 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     /**
146      * Retrieves a list of all JDBCConfig objects.
147      *
148      * @return java.util.List of JDBCConfig objects
149      */

150     public synchronized List JavaDoc 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 JavaDoc newConfigList = buildAllConfigurationsList();
156             allConfigurations = new SoftReference JavaDoc(newConfigList);
157         }
158
159         return (List JavaDoc) allConfigurations.get();
160     }
161
162     /**
163      * Retrieves the current Configuration
164      *
165      * @return JDBCConfig object
166      */

167     public JDBCConfig getCurrentConfig() {
168         return currentConfig;
169     }
170
171     /**
172      * Builds all the JDBCConfig configurations. It digests all known configuration .xml
173      * files sitting in the com.jcorporate.expresso.core.db.config package.
174      *
175      * @return a List of JDBC Configurations
176      */

177     private List JavaDoc buildAllConfigurationsList() {
178         List JavaDoc l = this.getSupportedConfigurations();
179         if (l == null || l.size() == 0) {
180             throw new IllegalArgumentException JavaDoc("No valid database configuration files present");
181         }
182
183         List JavaDoc allConfigurations = new java.util.ArrayList JavaDoc(l.size());
184
185         try {
186             Digester d = buildDigester();
187             for (Iterator JavaDoc 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 JavaDoc ex) {
193                     log.error("Error digesting configuration: " + vv.getKey(), ex);
194                 } catch (SAXException JavaDoc 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 JavaDoc(0);
204         }
205
206         return null;
207     }
208
209     /**
210      * Load one Digester class
211      *
212      * @param fileName the URL of the file to parse
213      * @param d The digester to use for parsing
214      * @return a built JDBCConfig
215      * @throws ConfigurationException if the resource cannot be located
216      * @throws IOException on i/o error reading in the document
217      * @throws SAXException upon reading in the document.
218      */

219     private JDBCConfig buildOneConfiguration(String JavaDoc fileName, Digester d)
220             throws ConfigurationException, IOException JavaDoc, SAXException JavaDoc {
221
222         java.io.InputStream JavaDoc 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     /**
245      * Creates and configures a XML digester to parse the JDBC Configurations.
246      *
247      * @return a build Digester
248      * @throws ConfigurationException if it was unable to build any Digester
249      */

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 JavaDoc 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 JavaDoc cl = DBConfig.class.getClassLoader();
270         if (cl != null) {
271             digester.setClassLoader(cl);
272         } else {
273             digester.setClassLoader(Thread.currentThread().getContextClassLoader());
274         }
275         // digester.setDebug(9); // is a no-op in digester v.1.3
276
digester.push(myConfig);
277
278         /* Top-level configuration */
279
280         /* Jdbc definition within the context */
281         digester.addCallMethod("db-config/jdbc/dbWildcard",
282                 "addWildcard", 0);
283         digester.addSetProperties("db-config/jdbc");
284
285
286         /* Type mappings */
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