Source code: org/mentawai/db/DBCPConnectionHandler.java
1 /*
2 * Mentawai Web Framework http://mentawai.lohis.com.br/
3 * Copyright (C) 2005 Sergio Oliveira Jr. (sergio.oliveira.jr@gmail.com)
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 St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 package org.mentawai.db;
20
21 import java.sql.*;
22 import javax.sql.*;
23 import java.lang.reflect.*;
24
25 /**
26 * A connection handler that uses the Commons DBCP connection pool. (http://jakarta.apache.org/commons/dbcp/)
27 * To use this class you must have the DBCP jars in your /WEB-INF/lib directory.
28 * You may access the underlying DBCP's BasicDataSource to configure the pool, before you start calling getConnection().
29 *
30 * @author Sergio Oliveira
31 */
32 public class DBCPConnectionHandler implements ConnectionHandler {
33
34 private DataSource bds;
35
36 /**
37 * Constructs a DBCPConnectionHandler with DBCP's BasicDataSource.
38 *
39 * @param driver The JDBC driver class name to use.
40 * @param url The JDBC url to connect to the database.
41 * @param user The database username to use.
42 * @param pass The database password to use.
43 * @throws IllegalStateException If the Commons DBCP jars are not in the /WEB-INF/lib directory or if the JDBC driver cannot be loaded.
44 */
45 public DBCPConnectionHandler(String driver, String url, String user, String pass) {
46
47 Class klass = null;
48
49 try {
50 klass = Class.forName("org.apache.commons.dbcp.BasicDataSource");
51 } catch(ClassNotFoundException e) {
52 e.printStackTrace();
53 throw new IllegalStateException("Commons DBCP cannot be found! You probably did not put the DBCP jars in your /WEB-INF/lib directory!");
54 }
55
56 try {
57 Class.forName(driver);
58 } catch(ClassNotFoundException e) {
59 e.printStackTrace();
60 throw new IllegalStateException("Cannot find jdbc driver " + driver + "! You probably did not put your JDBC driver in your /WEB-INF/lib directory!");
61 }
62
63 // by reflection to avoid external link library!
64 try {
65 Object bds = klass.newInstance();
66 setValue(bds, "driverClassName", driver);
67 setValue(bds, "username", user);
68 setValue(bds, "password", pass);
69 setValue(bds, "url", url);
70 this.bds = (DataSource) bds;
71 } catch(Exception e) {
72 e.printStackTrace();
73 throw new RuntimeException("Error trying to setup the DBCP's BasicDataSource: " + e.getMessage(), e);
74 }
75 }
76
77 /*
78 * Use reflection to set a property in the bean
79 */
80 private void setValue(Object bean, String name, Object value) throws Exception {
81 StringBuffer sb = new StringBuffer(30);
82 sb.append("set");
83 sb.append(name.substring(0,1).toUpperCase());
84 if (name.length() > 1) sb.append(name.substring(1));
85 Method m = bean.getClass().getMethod(sb.toString(), new Class[] { value.getClass() });
86 if (m != null) {
87 m.setAccessible(true);
88 m.invoke(bean, new Object[] { value });
89 }
90 }
91
92 /**
93 * Gets the underlying DBCP's BasicDataSource.
94 * You should cast this data source to DBCP's BasicDataSource.
95 *
96 * @return The BasicDataSource as a DataSource.
97 */
98 public DataSource getBasicDataSource() {
99 return bds;
100 }
101
102 public Connection getConnection() throws SQLException {
103 Connection conn = bds.getConnection();
104 if (conn == null) throw new SQLException("Pool returned null !!!");
105 return conn;
106 }
107
108 public void release(Connection conn) {
109 try {
110 if (conn != null) conn.close();
111 } catch(Exception e) {
112 e.printStackTrace();
113 }
114 }
115 }