01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
10: * For questions, suggestions, bug-reports, enhancement-requests etc.
11: * visit http://www.quickserver.org
12: *
13: */
14:
15: package echoserver;
16:
17: import org.quickserver.net.*;
18: import org.quickserver.net.server.*;
19: import org.quickserver.sql.*;
20: import org.quickserver.util.xmlreader.*;
21:
22: import java.io.*;
23: import java.util.logging.*;
24: import java.util.*;
25: import java.sql.*;
26:
27: public class DBPoolUtil implements org.quickserver.sql.DBPoolUtil {
28: private HashMap dbPool;
29:
30: public void setDatabaseConnections(Iterator iterator)
31: throws Exception {
32: dbPool = new HashMap();
33: while (iterator.hasNext()) {
34: DatabaseConnectionConfig dcc = (DatabaseConnectionConfig) iterator
35: .next();
36: dbPool.put(dcc.getId(), dcc);
37: }
38: }
39:
40: public boolean initPool() {
41: if (dbPool == null)
42: throw new IllegalStateException(
43: "Call setDatabaseConnections first.!!");
44: Iterator iterator = dbPool.keySet().iterator();
45: try {
46: while (iterator.hasNext()) {
47: DatabaseConnectionConfig dcc = (DatabaseConnectionConfig) dbPool
48: .get((String) iterator.next());
49: Class.forName(dcc.getDriver());
50: }
51: return true;
52: } catch (Exception e) {
53: System.err.println("In DBPoolUtil.initPool : " + e);
54: return false;
55: }
56: }
57:
58: public boolean clean() {
59: dbPool = null;
60: return true;
61: }
62:
63: public Connection getConnection(String id) throws Exception {
64: DatabaseConnectionConfig dcc = (DatabaseConnectionConfig) dbPool
65: .get(id);
66: return DriverManager.getConnection(dcc.getUrl(), dcc
67: .getUsername(), dcc.getPassword());
68: }
69: }
|