KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > config > DbFormsConfig


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/DbFormsConfig.java,v 1.19 2004/10/23 13:35:36 hkollmann Exp $
3  * $Revision: 1.19 $
4  * $Date: 2004/10/23 13:35:36 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.config;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.dom.DOMFactory;
30
31 import org.dbforms.util.IEscaper;
32 import org.dbforms.util.ReflectionUtil;
33 import org.dbforms.util.Util;
34
35 import java.sql.Connection JavaDoc;
36 import java.sql.SQLException JavaDoc;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Vector JavaDoc;
41
42 import javax.servlet.ServletConfig JavaDoc;
43 import javax.servlet.ServletContext JavaDoc;
44
45 import javax.sql.DataSource JavaDoc;
46
47
48
49 /**
50  * <p>
51  * This class gets populated with data from the dbforms-config.xml file by the
52  * ConfigServlet. This class is a kind of "single point of entry" for
53  * configuration data: it contains the definitions of tables, fields,
54  * fieldtypes, keys etc. and even the definitions of data sources (see
55  * dbforms-config.xml) Many components of the dbforms-frameworks use the data
56  * stored in this class.
57  * </p>
58  *
59  * @author Joe Peer
60  */

61 public class DbFormsConfig {
62    /** DOCUMENT ME! */
63    public static final String JavaDoc CONFIG = "dbformsConfig";
64
65    /** contains connection put by addDbConnection */
66    private ArrayList JavaDoc dbConnectionsList = new ArrayList JavaDoc();
67
68    /** the default db connection */
69    private DbConnection defaultDbConnection;
70    private IEscaper escaper = null;
71    private Hashtable JavaDoc dbConnectionsHash = new Hashtable JavaDoc();
72
73    /** for quicker lookup by name */
74    private Hashtable JavaDoc tableNameHash = new Hashtable JavaDoc();
75    private Log logCat = LogFactory.getLog(this.getClass().getName());
76    private ServletConfig JavaDoc servletConfig;
77    private String JavaDoc defaultEscaperClass = "org.dbforms.util.DefaultEscaperImpl";
78    private String JavaDoc defaultFormatterClass = "org.dbforms.util.DefaultFormatEmbeddedDataImpl";
79    private String JavaDoc realPath;
80
81    /** global application hookups */
82    private Vector JavaDoc interceptors = new Vector JavaDoc();
83    private Vector JavaDoc tables = new Vector JavaDoc();
84
85    /**
86     * Creates a new DbFormsConfig object.
87     *
88     * @param realPath local path to the application on local server
89     */

90    public DbFormsConfig(String JavaDoc realPath) {
91       setRealPath(realPath);
92    }
93
94    /**
95     * Just returns the default connection
96     *
97     * @return a JDBC connection object
98     *
99     * @throws IllegalArgumentException if any error occurs
100     */

101    public Connection JavaDoc getConnection()
102                             throws IllegalArgumentException JavaDoc, SQLException JavaDoc {
103       return getConnection(null);
104    }
105
106
107    /**
108     * Get a connection using the connection name specified into the xml
109     * configuration file.
110     *
111     * @param dbConnectionName the name of the DbConnection element
112     *
113     * @return a JDBC connection object
114     *
115     * @throws IllegalArgumentException if any error occurs
116     */

117    public Connection JavaDoc getConnection(String JavaDoc dbConnectionName)
118                             throws IllegalArgumentException JavaDoc, SQLException JavaDoc {
119       DataSource JavaDoc dbConnection = null;
120       Connection JavaDoc con = null;
121
122       // get the DbConnection object having the input name;
123
if ((dbConnection = getDataSource(dbConnectionName)) == null) {
124          throw new IllegalArgumentException JavaDoc("No DbConnection object configured with name '"
125                                             + dbConnectionName + "'");
126       }
127
128       // now try to get the JDBC connection from the retrieved DbConnection object;
129
if ((con = dbConnection.getConnection()) == null) {
130          throw new IllegalArgumentException JavaDoc("JDBC-Troubles: was not able to create connection from "
131                                             + dbConnection);
132       }
133
134       return con;
135    }
136
137
138    /**
139     * DOCUMENT ME!
140     *
141     * @param string
142     */

143    public void setDOMFactoryClass(String JavaDoc string) {
144       DOMFactory.setFactoryClass(string);
145    }
146
147
148    /**
149     * DOCUMENT ME!
150     *
151     * @param dbConnectionName DOCUMENT ME!
152     *
153     * @return DOCUMENT ME!
154     */

155    public DataSource JavaDoc getDataSource(String JavaDoc dbConnectionName) {
156       DbConnection connection = null;
157
158       if (!Util.isNull(dbConnectionName)) {
159          try {
160             connection = (DbConnection) dbConnectionsList.get(Integer.parseInt(dbConnectionName));
161          } catch (Exception JavaDoc ex) {
162             // wanted! logCat.error("getDbConnection", ex);
163
connection = null;
164          }
165
166          if (connection == null) {
167             connection = (DbConnection) dbConnectionsHash.get(dbConnectionName);
168          }
169       }
170
171       if (connection == null) {
172          connection = defaultDbConnection;
173       }
174
175       if ((connection != null)
176                 && !Util.isNull(connection.getContextDataSource())) {
177          DataSource JavaDoc ds = (DataSource JavaDoc) getServletContext()
178                                          .getAttribute(connection
179                                                        .getContextDataSource());
180
181          if (ds != null) {
182             return ds;
183          }
184       }
185
186       return connection;
187    }
188
189
190    /**
191     * DOCUMENT ME!
192     *
193     * @param string
194     */

195    public void setDefaultEscaperClass(String JavaDoc string) {
196       defaultEscaperClass = string;
197    }
198
199
200    /**
201     * DOCUMENT ME!
202     *
203     * @return
204     */

205    public String JavaDoc getDefaultEscaperClass() {
206       return defaultEscaperClass;
207    }
208
209
210    /**
211     * DOCUMENT ME!
212     *
213     * @param string
214     */

215    public void setDefaultFormatterClass(String JavaDoc string) {
216       defaultFormatterClass = string;
217    }
218
219
220    /**
221     * DOCUMENT ME!
222     *
223     * @return
224     */

225    public String JavaDoc getDefaultFormatterClass() {
226       return defaultFormatterClass;
227    }
228
229
230    /**
231     * DOCUMENT ME!
232     *
233     * @return DOCUMENT ME!
234     */

235    public IEscaper getEscaper() {
236       if (escaper == null) {
237          String JavaDoc s = getDefaultEscaperClass();
238
239          if (!Util.isNull(s)) {
240             try {
241                escaper = (IEscaper) ReflectionUtil.newInstance(s);
242             } catch (Exception JavaDoc e) {
243                logCat.error("cannot create the new escaper [" + s + "]", e);
244             }
245          }
246       }
247
248       return escaper;
249    }
250
251
252    /**
253     * Get all the global interceptor objects
254     *
255     * @return a vector containing all the interceptor objects
256     */

257    public Vector JavaDoc getInterceptors() {
258       return interceptors;
259    }
260
261
262    /**
263     * Sets the realPath.
264     *
265     * @param realPath The realPath to set
266     */

267    public void setRealPath(String JavaDoc realPath) {
268       if (!Util.isNull(realPath)) {
269          realPath = realPath.replace('\\', '/');
270       }
271
272       this.realPath = realPath;
273    }
274
275
276    /**
277     * Returns the realPath.
278     *
279     * @return the realPath
280     */

281    public String JavaDoc getRealPath() {
282       return realPath;
283    }
284
285
286    /**
287     * DOCUMENT ME!
288     *
289     * @param servletConfig DOCUMENT ME!
290     */

291    public void setServletConfig(ServletConfig JavaDoc servletConfig) {
292       this.servletConfig = servletConfig;
293    }
294
295
296    /**
297     * get access to configuration of config servlet
298     *
299     * @return the store config
300     */

301    public ServletConfig JavaDoc getServletConfig() {
302       return servletConfig;
303    }
304
305
306    /**
307     * Get access to servlet context in order to interoperate with other
308     * components of the web application
309     *
310     * @return the stored context
311     */

312    public ServletContext JavaDoc getServletContext() {
313       return servletConfig.getServletContext();
314    }
315
316
317    /**
318     * DOCUMENT ME!
319     *
320     * @param index DOCUMENT ME!
321     *
322     * @return DOCUMENT ME!
323     */

324    public Table getTable(int index) {
325       try {
326          return (Table) tables.elementAt(index);
327       } catch (Exception JavaDoc e) {
328          return null;
329       }
330    }
331
332
333    /**
334     * DOCUMENT ME!
335     *
336     * @param name DOCUMENT ME!
337     *
338     * @return DOCUMENT ME!
339     */

340    public Table getTableByName(String JavaDoc name) {
341       try {
342          return (Table) tableNameHash.get(name);
343       } catch (Exception JavaDoc e) {
344          return null;
345       }
346    }
347
348
349    /**
350     * DOCUMENT ME!
351     *
352     * @param dbConnection DOCUMENT ME!
353     */

354    public void addDbConnection(DbConnection dbConnection) {
355       dbConnection.setName(Util.replaceRealPath(dbConnection.getName(), realPath));
356       dbConnectionsList.add(dbConnection);
357
358       if (!Util.isNull(dbConnection.getId())) {
359          dbConnectionsHash.put(dbConnection.getId(), dbConnection);
360       }
361
362       // if a default connection does not exist yet,
363
// use the input connection as the default one;
364
if ((dbConnection.isDefaultConnection()
365                 && ((defaultDbConnection == null)
366                 || !defaultDbConnection.isDefaultConnection()))
367                 || (defaultDbConnection == null)) {
368          defaultDbConnection = dbConnection;
369          dbConnection.setDefaultConnection(true);
370       }
371
372       logCat.info("::addDbConnection - added the dbConnection [" + dbConnection
373                   + "]");
374    }
375
376
377    /**
378     * Add an global interceptor
379     *
380     * @param interceptor the interceptor to add
381     */

382    public void addInterceptor(Interceptor interceptor) {
383       interceptors.addElement(interceptor);
384    }
385
386
387    /**
388     * DOCUMENT ME!
389     *
390     * @param table DOCUMENT ME!
391     */

392    public void addTable(Table table) {
393       logCat.info("add table called");
394       table.setId(tables.size());
395       table.initDefaultOrder();
396       tables.addElement(table);
397       tableNameHash.put(table.getName(), table);
398    }
399
400
401    /**
402     * Check if this table has got interceptors.
403     *
404     * @return true if the table contains interceptors, false otherwise
405     */

406    public boolean hasInterceptors() {
407       return (interceptors != null) && (interceptors.size() > 0);
408    }
409
410
411    /**
412     * DOCUMENT ME!
413     *
414     * @return DOCUMENT ME!
415     */

416    public String JavaDoc toString() {
417       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
418
419       for (int i = 0; i < tables.size(); i++) {
420          Table t = (Table) tables.elementAt(i);
421          buf.append("table:\n");
422          buf.append(t.toString());
423       }
424
425       return buf.toString();
426    }
427 }
428
Popular Tags