KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > connectionpool > PoolManager


1 /*
2  * PoolManager is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/connectionpool/PoolManager.java,v 1.2.2.1 2006/12/11 16:33:22 fxrobin Exp $
21  */

22
23 package org.cofax.connectionpool;
24
25 import java.sql.*;
26 import java.io.*;
27 import java.util.*;
28
29 public class PoolManager {
30
31     static private PoolManager instance;
32
33     static private int clients;
34
35     private LogWriter logWriter;
36
37     private PrintWriter pw;
38
39     private Vector drivers = new Vector();
40
41     private Hashtable pools = new Hashtable();
42
43     private PoolManager() {
44         init();
45     }
46
47     private PoolManager(String JavaDoc configFile) {
48         init(configFile);
49     }
50
51     private PoolManager(Properties dbProps) {
52         init(dbProps);
53     }
54
55     static synchronized public PoolManager getInstance() {
56         if (instance == null) {
57             instance = new PoolManager();
58         }
59         clients++;
60         return instance;
61     }
62
63     static synchronized public PoolManager getInstance(String JavaDoc configFile) {
64         if (instance == null) {
65             instance = new PoolManager(configFile);
66         }
67         clients++;
68         return instance;
69     }
70
71     static synchronized public PoolManager getInstance(Properties dbProps) {
72         if (instance == null) {
73             instance = new PoolManager(dbProps);
74         }
75         clients++;
76         return instance;
77     }
78
79     private void init(Properties dbProps) {
80         // Log to System.err until we have read the logfile property
81
pw = new PrintWriter(System.err, true);
82         logWriter = new LogWriter("PoolManager", LogWriter.INFO, pw);
83
84         String JavaDoc logFile = dbProps.getProperty("logfile");
85         if (logFile != null) {
86             try {
87                 pw = new PrintWriter(new FileWriter(logFile, true), true);
88                 logWriter.setPrintWriter(pw);
89             } catch (IOException e) {
90                 logWriter.log("Can't open the log file: " + logFile + ". Using System.err instead", LogWriter.ERROR);
91             }
92         }
93         loadDrivers(dbProps);
94         createPools(dbProps);
95     }
96
97     private void init(String JavaDoc configFile) {
98         // Log to System.err until we have read the logfile property
99
pw = new PrintWriter(System.err, true);
100         logWriter = new LogWriter("PoolManager", LogWriter.INFO, pw);
101         InputStream is = null;
102         try {
103             is = new FileInputStream(configFile);
104         } catch (Exception JavaDoc e) {
105             logWriter.log("Cannot find the properties file. " + configFile, LogWriter.ERROR);
106             return;
107         }
108         Properties dbProps = new Properties();
109         try {
110             dbProps.load(is);
111         } catch (Exception JavaDoc e) {
112             logWriter.log("Can't read the properties file. " + configFile, LogWriter.ERROR);
113             return;
114         }
115         String JavaDoc logFile = dbProps.getProperty("logfile");
116         if (logFile != null) {
117             try {
118                 pw = new PrintWriter(new FileWriter(logFile, true), true);
119                 logWriter.setPrintWriter(pw);
120             } catch (IOException e) {
121                 logWriter.log("Can't open the log file: " + logFile + ". Using System.err instead", LogWriter.ERROR);
122             }
123         }
124         loadDrivers(dbProps);
125         createPools(dbProps);
126     }
127
128     private void init() {
129         // Log to System.err until we have read the logfile property
130
pw = new PrintWriter(System.err, true);
131         logWriter = new LogWriter("PoolManager", LogWriter.INFO, pw);
132         InputStream is = getClass().getResourceAsStream("/db.properties");
133         Properties dbProps = new Properties();
134         try {
135             dbProps.load(is);
136         } catch (Exception JavaDoc e) {
137             logWriter.log("Can't read the properties file. " + "Make sure db.properties is in the CLASSPATH", LogWriter.ERROR);
138             return;
139         }
140         String JavaDoc logFile = dbProps.getProperty("logfile");
141         if (logFile != null) {
142             try {
143                 pw = new PrintWriter(new FileWriter(logFile, true), true);
144                 logWriter.setPrintWriter(pw);
145             } catch (IOException e) {
146                 logWriter.log("Can't open the log file: " + logFile + ". Using System.err instead", LogWriter.ERROR);
147             }
148         }
149         loadDrivers(dbProps);
150         createPools(dbProps);
151     }
152
153     private void loadDrivers(Properties props) {
154         String JavaDoc driverClasses = props.getProperty("drivers");
155         StringTokenizer st = new StringTokenizer(driverClasses);
156         while (st.hasMoreElements()) {
157             String JavaDoc driverClassName = st.nextToken().trim();
158             try {
159                 Driver driver = (Driver) Class.forName(driverClassName).newInstance();
160                 DriverManager.registerDriver(driver);
161                 drivers.addElement(driver);
162                 logWriter.log("Registered JDBC driver " + driverClassName, LogWriter.INFO);
163             } catch (Exception JavaDoc e) {
164                 logWriter.log(e, "Can't register JDBC driver: " + driverClassName, LogWriter.ERROR);
165             }
166         }
167     }
168
169     private void createPools(Properties props) {
170         Enumeration propNames = props.propertyNames();
171         while (propNames.hasMoreElements()) {
172             String JavaDoc name = (String JavaDoc) propNames.nextElement();
173             if (name.endsWith(".url")) {
174                 String JavaDoc poolName = name.substring(0, name.lastIndexOf("."));
175                 String JavaDoc url = props.getProperty(poolName + ".url");
176                 if (url == null) {
177                     logWriter.log("No URL specified for " + poolName, LogWriter.ERROR);
178                     continue;
179                 }
180
181                 String JavaDoc user = props.getProperty(poolName + ".user");
182                 String JavaDoc password = props.getProperty(poolName + ".password");
183
184                 String JavaDoc maxConns = props.getProperty(poolName + ".maxconns", "0");
185                 int max;
186                 try {
187                     max = Integer.valueOf(maxConns).intValue();
188                 } catch (NumberFormatException JavaDoc e) {
189                     logWriter.log("Invalid maxconns value " + maxConns + " for " + poolName, LogWriter.ERROR);
190                     max = 0;
191                 }
192
193                 String JavaDoc initConns = props.getProperty(poolName + ".initconns", "0");
194                 int init;
195                 try {
196                     init = Integer.valueOf(initConns).intValue();
197                 } catch (NumberFormatException JavaDoc e) {
198                     logWriter.log("Invalid initconns value " + initConns + " for " + poolName, LogWriter.ERROR);
199                     init = 0;
200                 }
201
202                 String JavaDoc loginTimeOut = props.getProperty(poolName + ".logintimeout", "5");
203                 int timeOut;
204                 try {
205                     timeOut = Integer.valueOf(loginTimeOut).intValue();
206                 } catch (NumberFormatException JavaDoc e) {
207                     logWriter.log("Invalid logintimeout value " + loginTimeOut + " for " + poolName, LogWriter.ERROR);
208                     timeOut = 5;
209                 }
210
211                 String JavaDoc logLevelProp = props.getProperty(poolName + ".loglevel", String.valueOf(LogWriter.ERROR));
212                 int logLevel = LogWriter.INFO;
213                 if (logLevelProp.equalsIgnoreCase("none")) {
214                     logLevel = LogWriter.NONE;
215                 } else if (logLevelProp.equalsIgnoreCase("error")) {
216                     logLevel = LogWriter.ERROR;
217                 } else if (logLevelProp.equalsIgnoreCase("debug")) {
218                     logLevel = LogWriter.DEBUG;
219                 }
220
221                 String JavaDoc usageLimitTmp = props.getProperty(poolName + ".connusagelimit", "50");
222                 int connUsageLimit;
223                 try {
224                     connUsageLimit = Integer.valueOf(usageLimitTmp).intValue();
225                 } catch (NumberFormatException JavaDoc e) {
226                     logWriter.log("Invalid usage limit value " + usageLimitTmp + " for " + poolName, LogWriter.ERROR);
227                     connUsageLimit = 100;
228                 }
229
230                 String JavaDoc testQuery = props.getProperty(poolName + ".testquery");
231                 if (testQuery == null) {
232                     logWriter.log("No test query specified for " + poolName, LogWriter.ERROR);
233                 }
234
235                 ConnectionPool pool = new ConnectionPool(poolName, url, user, password, max, init, timeOut, pw, logLevel, testQuery, connUsageLimit);
236                 pools.put(poolName, pool);
237             }
238         }
239     }
240
241     public ConnectionWrapper getConnection(String JavaDoc name) {
242         ConnectionWrapper conn = null;
243         ConnectionPool pool = (ConnectionPool) pools.get(name);
244         if (pool != null) {
245             try {
246                 conn = pool.getConnection();
247             } catch (SQLException e) {
248                 // logWriter.log(e, "Exception getting connection from " + name,
249
// LogWriter.ERROR) ;
250
// 20010823 Sam-- Commented this out for now to stop stack trace
251
// in the log file.
252
}
253         }
254         return conn;
255     }
256
257     public void freeConnection(String JavaDoc name, ConnectionWrapper con) {
258         ConnectionPool pool = (ConnectionPool) pools.get(name);
259         if (pool != null) {
260             pool.freeConnection(con);
261         }
262     }
263
264     public synchronized void release() {
265         // Wait until called by the last client
266
if (--clients != 0) {
267             return;
268         }
269
270         Enumeration allPools = pools.elements();
271         while (allPools.hasMoreElements()) {
272             ConnectionPool pool = (ConnectionPool) allPools.nextElement();
273             pool.release();
274         }
275
276         Enumeration allDrivers = drivers.elements();
277         while (allDrivers.hasMoreElements()) {
278             Driver driver = (Driver) allDrivers.nextElement();
279             try {
280                 DriverManager.deregisterDriver(driver);
281                 logWriter.log("Deregistered JDBC driver " + driver.getClass().getName(), LogWriter.INFO);
282             } catch (SQLException e) {
283                 logWriter.log(e, "Couldn't deregister JDBC driver: " + driver.getClass().getName(), LogWriter.ERROR);
284             }
285         }
286     }
287
288     public String JavaDoc getConnectionStats(String JavaDoc name) {
289
290         ConnectionPool pool = (ConnectionPool) pools.get(name);
291         if (pool != null)
292             return (pool.getConnectionStats());
293         return ("");
294
295     }
296
297     public String JavaDoc getURL() {
298         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
299         if (pool != null)
300             return pool.getURL();
301         return ("");
302     }
303
304     public String JavaDoc getUser() {
305         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
306         if (pool != null)
307             return pool.getUser();
308         return ("");
309     }
310
311     public String JavaDoc getPassword() {
312         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
313         if (pool != null)
314             return pool.getPassword();
315         return ("");
316     }
317
318     public int getMaxConns() {
319         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
320         if (pool != null)
321             return pool.getMaxConns();
322         return (0);
323     }
324
325     public int getInitConns() {
326         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
327         if (pool != null)
328             return pool.getInitConns();
329         return (0);
330     }
331
332     public int getTimeOut() {
333         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
334         if (pool != null)
335             return pool.getTimeOut();
336         return (0);
337     }
338
339     public int getConUsageLimit() {
340         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
341         if (pool != null)
342             return pool.getConUsageLimit();
343         return (0);
344     }
345
346     public long getKillTime() {
347         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
348         if (pool != null)
349             return pool.getKillTime();
350         return (0);
351     }
352
353     public void setURL(String JavaDoc in) {
354         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
355         if (pool != null)
356             pool.setURL(in);
357     }
358
359     public void setUser(String JavaDoc in) {
360         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
361         if (pool != null)
362             pool.setUser(in);
363     }
364
365     public void setPassword(String JavaDoc in) {
366         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
367         if (pool != null)
368             pool.setPassword(in);
369     }
370
371     public void setMaxConns(int in) {
372         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
373         if (pool != null)
374             pool.setMaxConns(in);
375     }
376
377     public void setInitConns(int in) {
378         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
379         if (pool != null)
380             pool.setInitConns(in);
381     }
382
383     public void setTimeOut(int in) {
384         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
385         if (pool != null)
386             pool.setTimeOut(in);
387     }
388
389     public void setConUsageLimit(int in) {
390         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
391         if (pool != null)
392             pool.setConUsageLimit(in);
393     }
394
395     public void setKillTime(long in) {
396         ConnectionPool pool = (ConnectionPool) pools.get("cofax");
397         if (pool != null)
398             pool.setKillTime(in);
399     }
400
401 }
402
Popular Tags