Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/db/DbManager.java


1   /*
2       Javatools (modified version) - Some useful general classes.
3       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      Contact me at: brenmcguire@users.sourceforge.net
20   */
21  package javatools.db;
22  import java.util.*;
23  import java.io.*;
24  import javatools.util.Props;
25  
26  /**
27   * A class to manage all the other DbDatabases. Usually there will be only one
28   * DbManager object, thus you should use singleton() to access it. <P>
29   *
30   * e.g. DbDatabase db = DbManager.singleton().getDatabase("foo");
31   *
32   * @author Chris Bitmead
33   * @created 29 August 2001
34   * @version 0.7
35   * @commentedby Antonio Petrelli
36   */
37  
38  public class DbManager {
39      /** The one and only DbManager.
40       */    
41    static DbManager single = new DbManager();
42          /** A map for matching names to DBMSs.
43           */        
44    Map nameDatabaseMap = new HashMap();
45          /** A map for matching DBMSs to names.
46           */        
47    Map databaseNameMap = new HashMap();
48          /** The complete properties.
49           */        
50    Props props = null;
51  
52    /**
53           * Return THE DbManager.
54           *
55           * @return The manager itself.
56           */
57    public static DbManager singleton() {
58      return single;
59    }
60  
61    /**
62     *  Return a Map that maps names to DbDatabases.
63     *
64     * @return    The nameDatabaseMap value
65     */
66    public Map getNameDatabaseMap() {
67      return nameDatabaseMap;
68    }
69  
70    /**
71     *  Return a Map that maps DbDatabases to names.
72     *
73     * @return    The databaseNameMap value
74     */
75    public Map getDatabaseNameMap() {
76      return databaseNameMap;
77    }
78  
79    /**
80           * Return a properties object for the db subsystem. Currently we always use a
81           * "db.properties" that is found on the classpath.
82           *
83           * @return The props value
84           * @exception IOException If something goes wrong in loading properties file.
85           */
86    public Props getProps() throws IOException {
87      if (props == null) {
88        props = Props.singleton("db");
89      }
90      return props;
91    }
92  
93    /**
94           * Return the database object associated with this name or parameters. If you
95           * don't give a name DbManager won't be able to cache DbDatabase objects, so
96           * giving a name is recommended. Usually the name will be just something which
97           * identifies this application. Once a DbDatabase is in the cache you can use
98           * the getDatabase(name) with one argument. Alternatively you can always use
99           * the one argument version and the parameters will come from the
100          * db.properties file.
101          *
102          * @param name             An arbitrary name used to identify this DbDatabase.
103          * @param driver           The Java JDBC driver class name.
104          * @param connectString    The JDBC connect string.
105          * @param userName         The database connect user name.
106          * @param password         The database connect password.
107          * @return The database value
108          * @exception DbException If something goes wrong.
109          */
110   public DbDatabase getDatabase(String name, String driver, String connectString, String userName, String password) throws DbException {
111     DbDatabase rtn = null;
112     if (name == null) {
113       rtn = new DbDatabase(this, name, driver, connectString, userName, password);
114     } else {
115       rtn = (DbDatabase) nameDatabaseMap.get(name);
116       if (rtn == null) {
117         rtn = new DbDatabase(this, name, driver, connectString, userName, password);
118       }
119       nameDatabaseMap.put(name, rtn);
120       databaseNameMap.put(rtn, name);
121     }
122     return rtn;
123   }
124 
125   /**
126          * Return the database associated with this name. If the database is already
127          * in the cache we return that, otherwise we attempt to read a db properties
128          * file and get a connect configuration from that with <name>.<property>
129          *
130          * . For example, you could have a notional database called "foo". Then in
131          * your db.properties file you might have a setup like: <PRE>
132          * foo.driver  = oracle.jdbc.driver.OracleDriver
133          * foo.connect = jdbc:oracle:thin:@dbdev01:1521:devu02
134          * foo.userId = foouserid
135          * foo.password = foopassword
136          * </PRE> In terms of where the db.properties file will be located, that is
137          * determined by the javatools.util.Props class.
138          *
139          * @param name             The name in the cache and/or in the db.properties
140          *     file.
141          * @return The database value
142          * @exception DbException If something goes wrong.
143          * @see javatools.util.Props
144          */
145   public DbDatabase getDatabase(String name) throws DbException {
146     DbDatabase rtn = null;
147     if (props == null) {
148       try {
149         props = Props.singleton("db");
150       } catch (IOException e) {
151         throw new DbException("Can't find db.properties file", e);
152       }
153     }
154     String driver = props.getProperty(name + ".driver");
155     String connectString = props.getProperty(name + ".connect");
156     String userName = props.getProperty(name + ".userId");
157     String password = props.getProperty(name + ".password");
158     return getDatabase(name, driver, connectString, userName, password);
159   }
160 
161   /**
162          * Commit and close ALL connections associated with this database.
163          *
164          * @exception DbException If something goes wrong.
165          */
166   public void commitClose() throws DbException {
167     Iterator it = databaseNameMap.keySet().iterator();
168     DbException te = null;
169     while (it.hasNext()) {
170       try {
171         DbDatabase db = (DbDatabase) it.next();
172         DbConnection con = db.getExistingThreadConnection();
173         if (con != null) {
174           con.commitClose();
175         }
176       } catch (DbException e) {
177         te = e;
178       }
179     }
180     if (te != null) {
181       throw te;
182     }
183   }
184 
185   /**
186          * Rollback and close ALL connections associated with this database.
187          *
188          * @exception DbException If something goes wrong.
189          */
190   public void rollbackClose() throws DbException {
191     Iterator it = databaseNameMap.keySet().iterator();
192     DbException te = null;
193     while (it.hasNext()) {
194       try {
195         DbDatabase db = (DbDatabase) it.next();
196         DbConnection con = db.getExistingThreadConnection();
197         if (con != null) {
198           con.rollbackClose();
199         }
200       } catch (DbException e) {
201         te = e;
202       }
203     }
204     if (te != null) {
205       throw te;
206     }
207   }
208 
209   /**
210          * Perform a commit on ALL connections associated with this database.
211          *
212          * @exception DbException If something goes wrong.
213          */
214   public void commit() throws DbException {
215     Iterator it = databaseNameMap.keySet().iterator();
216     DbException te = null;
217     while (it.hasNext()) {
218       try {
219         DbDatabase db = (DbDatabase) it.next();
220         DbConnection con = db.getExistingThreadConnection();
221         if (con != null) {
222           con.commit();
223         }
224       } catch (DbException e) {
225         te = e;
226       }
227     }
228     if (te != null) {
229       throw te;
230     }
231   }
232 
233   /**
234          * Perform a rollback on ALL connections associated with this database.
235          *
236          * @exception DbException If something goes wrong.
237          */
238   public void rollback() throws DbException {
239     Iterator it = databaseNameMap.keySet().iterator();
240     DbException te = null;
241     while (it.hasNext()) {
242       try {
243         DbDatabase db = (DbDatabase) it.next();
244         DbConnection con = db.getExistingThreadConnection();
245         if (con != null) {
246           con.commit();
247         }
248       } catch (DbException e) {
249         te = e;
250       }
251     }
252     if (te != null) {
253       throw te;
254     }
255   }
256 
257   /**
258    *  Close ALL connections associated with this database.
259    *
260    * @exception  DbException  Description of Exception
261    */
262   public void close() throws DbException {
263     Iterator it = databaseNameMap.keySet().iterator();
264     DbException te = null;
265     while (it.hasNext()) {
266       try {
267         DbDatabase db = (DbDatabase) it.next();
268         DbConnection con = db.getExistingThreadConnection();
269         if (con != null) {
270           con.close();
271         }
272       } catch (DbException e) {
273         te = e;
274       }
275     }
276     if (te != null) {
277       throw te;
278     }
279   }
280 }