1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package org.osjava.sj.loader;
34
35 import java.sql.SQLException;
36 import java.sql.DriverManager;
37 import java.util.Properties;
38
39
40 import org.apache.commons.pool.impl.GenericObjectPool;
41 import org.apache.commons.dbcp.ConnectionFactory;
42 import org.apache.commons.dbcp.PoolingDriver;
43 import org.apache.commons.dbcp.PoolableConnectionFactory;
44 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
45
46 /***
47 * This is a wrapper for the Pooling functionality, currently provided
48 * by Jakarta DBCP. Having the wrapper allows the dependency to be
49 * optional.
50 */
51 public class PoolSetup {
52
53 public static void setupConnection(String pool, String url, String username, String password, Properties properties) throws SQLException {
54
55 GenericObjectPool connectionPool = new GenericObjectPool(null,
56 toInt(properties.getProperty("dbcpMaxActive"), GenericObjectPool.DEFAULT_MAX_ACTIVE),
57 (byte) toInt(properties.getProperty("dbcpWhenExhaustedAction"), GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION),
58 toLong(properties.getProperty("dbcpMaxWait"), GenericObjectPool.DEFAULT_MAX_WAIT),
59 toInt(properties.getProperty("dbcpMaxIdle"), GenericObjectPool.DEFAULT_MAX_IDLE),
60 toInt(properties.getProperty("dbcpMinIdle"), GenericObjectPool.DEFAULT_MIN_IDLE),
61 toBoolean(properties.getProperty("dbcpTestOnBorrow"), GenericObjectPool.DEFAULT_TEST_ON_BORROW),
62 toBoolean(properties.getProperty("dbcpTestOnReturn"), GenericObjectPool.DEFAULT_TEST_ON_RETURN),
63 toLong(properties.getProperty("dbcpTimeBetweenEvictionRunsMillis"), GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS),
64 toInt(properties.getProperty("dbcpNumTestsPerEvictionRun"), GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN),
65 toLong(properties.getProperty("dbcpMinEvictableIdleTimeMillis"), GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS),
66 toBoolean(properties.getProperty("dbcpTestWhileIdle"), GenericObjectPool.DEFAULT_TEST_WHILE_IDLE),
67 toLong(properties.getProperty("dbcpSoftMinEvictableIdleTimeMillis"), GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS)
68 );
69
70
71 ConnectionFactory connectionFactory = null;
72 if(username == null || password == null) {
73
74 connectionFactory = new DriverManagerConnectionFactory(url, null);
75 } else {
76 connectionFactory = new DriverManagerConnectionFactory(url, username, password);
77 }
78 new PoolableConnectionFactory(connectionFactory, connectionPool, null, properties.getProperty("dbcpValidationQuery"), toBoolean(properties.getProperty("dbcpDefaultReadOnly"), false), toBoolean(properties.getProperty("dbcpDefaultAutoCommit"), true));
79 try {
80 Class.forName("org.apache.commons.dbcp.PoolingDriver");
81 } catch(ClassNotFoundException cnfe) {
82
83 System.err.println("WARNING: DBCP needed but not in the classpath. ");
84 }
85 PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
86 driver.registerPool(pool, connectionPool);
87
88
89 }
90
91 public static String getUrl(String pool) {
92 return "jdbc:apache:commons:dbcp:"+pool;
93 }
94
95 private static int toInt(String str, int def) {
96 if(str == null) {
97 return def;
98 }
99 try {
100 return Integer.parseInt(str);
101 } catch(NumberFormatException nfe) {
102 throw new RuntimeException("Unable to parse as int: '" + str + "'", nfe);
103 }
104 }
105
106 private static long toLong(String str, long def) {
107 if(str == null) {
108 return def;
109 }
110 try {
111 return Long.parseLong(str);
112 } catch(NumberFormatException nfe) {
113 throw new RuntimeException("Unable to parse as long: '" + str + "'", nfe);
114 }
115 }
116
117 private static boolean toBoolean(String str, boolean def) {
118 if(str == null) {
119 return def;
120 } else
121 if("true".equals(str)) {
122 return true;
123 } else
124 if("false".equals(str)) {
125 return false;
126 } else {
127 throw new RuntimeException("Unable to parse as boolean: '" + str + "'");
128 }
129 }
130
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155