KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > db > DBSourceManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * XMLConfigurationException.java
20  *
21  * DBSourceManager.java
22  *
23  * This object is responsible for managing the database sources used by the
24  * coadunation environments.
25  */

26
27 // package path
28
package com.rift.coad.lib.db;
29
30 // java import
31
import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 import javax.naming.Reference JavaDoc;
38 import javax.naming.StringRefAddr JavaDoc;
39 import java.util.HashMap JavaDoc;
40
41 // logging import
42
import org.apache.log4j.Logger;
43
44 // coadunation imports
45
import com.rift.coad.lib.configuration.ConfigurationFactory;
46 import com.rift.coad.lib.configuration.Configuration;
47 import com.rift.coad.lib.naming.ContextManager;
48 import com.rift.coad.lib.loader.MasterClassLoader;
49
50
51 /**
52  * This object is responsible for managing the database sources used by the
53  * coadunation environments.
54  *
55  * @author Brett Chaldecott
56  */

57 public class DBSourceManager {
58     
59     // class constants
60
private final static String JavaDoc DB_SOURCES = "db_sources";
61     private final static String JavaDoc DB_SOURCE = "db_source";
62     private final static String JavaDoc DB_CONTEXT = "java:comp/env/jdbc";
63     private final static String JavaDoc DB_CONTEXT_ID = "db_context";
64     
65     // the singleton reference.
66
private static DBSourceManager singleton = null;
67     
68     // member variables
69
protected Logger log =
70         Logger.getLogger(DBSourceManager.class.getName());
71     private ContextManager contextManager = null;
72     private String JavaDoc jndiBase = null;
73     
74     /**
75      * Creates a new instance of DBSourceManager
76      *
77      * @exception DBException
78      */

79     private DBSourceManager() throws DBException {
80         try {
81             Configuration sourceConfig = ConfigurationFactory.getInstance().
82                     getConfig(this.getClass());
83             // init the context
84
try {
85                 jndiBase = sourceConfig.getString(
86                         DB_CONTEXT);
87                 contextManager = new ContextManager(jndiBase);
88             } catch (Exception JavaDoc ex) {
89                 jndiBase = DB_CONTEXT;
90                 contextManager = new ContextManager(jndiBase);
91             }
92             
93             StringTokenizer JavaDoc dbSources = new
94                     StringTokenizer JavaDoc(sourceConfig.getString(DB_SOURCES),",");
95             while(dbSources.hasMoreTokens()) {
96                 String JavaDoc dbSource = dbSources.nextToken().trim();
97                 try {
98                     Configuration dbSourceConfig = ConfigurationFactory.getInstance().
99                             getConfig(Class.forName(dbSource));
100                     Set JavaDoc keys = dbSourceConfig.getKeys();
101                     Reference JavaDoc ref = new Reference JavaDoc("javax.sql.DataSource",
102                             "org.objectweb.jndi.DataSourceFactory",
103                             null);
104                     ref.add(new StringRefAddr JavaDoc("driverClassName",dbSource));
105                     for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
106                         String JavaDoc key = (String JavaDoc)iter.next();
107                         if (key.equals(DB_SOURCE) ||
108                                 !dbSourceConfig.isString(key)) {
109                             continue;
110                         }
111                         ref.add(new StringRefAddr JavaDoc(key,
112                                 dbSourceConfig.getString(key)));
113                     }
114                     contextManager.bind(dbSourceConfig.getString(DB_SOURCE),ref);
115                 } catch (Exception JavaDoc ex) {
116                     log.error("Failed to instanciate the datase source for [" +
117                             dbSource + "] because : " + ex.getMessage(), ex);
118                 }
119             }
120         } catch (Exception JavaDoc ex) {
121             throw new DBException(
122                     "Failed to instanciate the db source manager : " +
123                     ex.getMessage(),ex);
124         }
125     }
126     
127     
128     /**
129      * This method initializes the database source manager.
130      */

131     public static synchronized void init() throws DBException {
132         if (singleton == null) {
133             singleton = new DBSourceManager();
134         }
135     }
136     
137     
138     /**
139      * This method returns an instance of the database source manager.
140      *
141      * @return The instance of the database source manager.
142      * @exception DBException
143      */

144     public static synchronized DBSourceManager getInstance() throws DBException {
145         if (singleton == null) {
146             throw new DBException(
147                     "The Database Source manager has not been initialized.");
148         }
149         return singleton;
150     }
151     
152     
153     /**
154      * This method adds a new database source. This method must not be
155      * used by any Daemon instances!!!!!
156      *
157      * @param driverPath The path to the driver library.
158      * @param driverClass The driver class to load.
159      * @param jndi The name to register this jdbc connection with the jndi.
160      * @param env The environment for the jdbc connetion.
161      * @exception DBException
162      */

163     public void addDBSource(String JavaDoc driverPath, String JavaDoc driverClass, String JavaDoc jndi,
164             HashMap JavaDoc env) throws DBException {
165         try {
166             MasterClassLoader.getInstance().addLib(driverPath);
167             Class.forName(driverClass);
168             Reference JavaDoc ref = new Reference JavaDoc("javax.sql.DataSource",
169                     "org.apache.commons.dbcp.BasicDatabSourceFactory",
170                     null);
171             ref.add(new StringRefAddr JavaDoc("driverClassName",driverClass));
172             Set JavaDoc keys = env.keySet();
173             for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
174                 String JavaDoc key = (String JavaDoc)iter.next();
175                 if (key.equals(DB_SOURCE)) {
176                     continue;
177                 }
178                 ref.add(new StringRefAddr JavaDoc(key,(String JavaDoc)env.get(key)));
179             }
180             String JavaDoc jndiRef = jndi;
181             if (jndiRef.contains(jndiBase)) {
182                 jndiRef = jndiRef.substring(jndiRef.indexOf(jndiBase) +
183                         jndiBase.length());
184             }
185             contextManager.bind(jndiRef,ref);
186         } catch (Exception JavaDoc ex) {
187             throw new DBException("Failed to add a data source because : " +
188                     ex.getMessage(),ex);
189         }
190     }
191     
192     
193     /**
194      * This method adds the database source.
195      *
196      * @param driverClass The driver class to load.
197      * @param jndi The name to register this jdbc connection with the jndi.
198      * @param env The environment for the jdbc connetion.
199      * @exception DBException
200      */

201     public void addDBSource(String JavaDoc driverClass, String JavaDoc jndi,
202             HashMap JavaDoc env) throws DBException {
203         try {
204             Class.forName(driverClass);
205             Reference JavaDoc ref = new Reference JavaDoc("javax.sql.DataSource",
206                     "org.apache.commons.dbcp.BasicDatabSourceFactory",
207                     null);
208             ref.add(new StringRefAddr JavaDoc("driverClassName",driverClass));
209             Set JavaDoc keys = env.keySet();
210             for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
211                 String JavaDoc key = (String JavaDoc)iter.next();
212                 if (key.equals(DB_SOURCE)) {
213                     continue;
214                 }
215                 ref.add(new StringRefAddr JavaDoc(key,(String JavaDoc)env.get(key)));
216             }
217             String JavaDoc jndiRef = jndi;
218             if (jndiRef.contains(jndiBase)) {
219                 jndiRef = jndiRef.substring(jndiRef.indexOf(jndiBase) +
220                         jndiBase.length());
221             }
222             contextManager.bind(jndiRef,ref);
223         } catch (Exception JavaDoc ex) {
224             throw new DBException("Failed to add a data source because : " +
225                     ex.getMessage(),ex);
226         }
227     }
228     
229     
230     /**
231      * This method removes the database source from JNDI. It does not unload
232      * the library that requires a restart.
233      *
234      * @param jndi The path to the jndi variable.
235      * @exception DBException
236      */

237     public void removeDBSource(String JavaDoc jndi) throws DBException {
238         try {
239             String JavaDoc jndiRef = jndi;
240             if (jndiRef.contains(jndiBase)) {
241                 jndiRef = jndiRef.substring(jndiRef.indexOf(jndiBase) +
242                         jndiBase.length());
243             }
244             contextManager.unbind(jndiRef);
245         } catch (Exception JavaDoc ex) {
246             throw new DBException("Failed to remove [" + jndi + "] because : " +
247                     ex.getMessage(),ex);
248         }
249     }
250 }
251
Popular Tags