KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > DBCPDataSourceBuilder


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.conf;
20
21 import java.sql.Connection JavaDoc;
22
23 import javax.sql.DataSource JavaDoc;
24
25 import org.apache.cayenne.ConfigurationException;
26 import org.apache.commons.dbcp.ConnectionFactory;
27 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
28 import org.apache.commons.dbcp.PoolableConnectionFactory;
29 import org.apache.commons.dbcp.PoolingDataSource;
30 import org.apache.commons.pool.KeyedObjectPoolFactory;
31 import org.apache.commons.pool.ObjectPool;
32 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
33 import org.apache.commons.pool.impl.GenericObjectPool;
34
35 /**
36  * A helper class that loads DBCP DataSource from properties.
37  *
38  * @author Andrus Adamchik
39  * @since 2.0
40  */

41 class DBCPDataSourceBuilder {
42
43     // DataSource Properties
44
static final String JavaDoc DRIVER_CLASS_NAME = "driverClassName";
45     static final String JavaDoc URL = "url";
46     static final String JavaDoc USER_NAME = "username";
47     static final String JavaDoc PASSWORD = "password";
48
49     // Connection Pool Properties
50
static final String JavaDoc MAX_ACTIVE = "maxActive";
51     static final String JavaDoc MIN_IDLE = "minIdle";
52     static final String JavaDoc MAX_IDLE = "maxIdle";
53     static final String JavaDoc MAX_WAIT = "maxWait";
54     static final String JavaDoc VALIDATION_QUERY = "validationQuery";
55     static final String JavaDoc TEST_ON_BORROW = "testOnBorrow";
56     static final String JavaDoc TEST_ON_RETURN = "testOnReturn";
57     static final String JavaDoc TEST_IDLE = "testWhileIdle";
58     static final String JavaDoc TIME_BETWEEN_EVICTIONS = "timeBetweenEvictionRunsMillis";
59     static final String JavaDoc NUM_TEST_PER_EVICTION = "numTestsPerEvictionRun";
60     static final String JavaDoc MIN_EVICTABLE_TIME = "minEvictableIdleTimeMillis";
61     static final String JavaDoc EXHAUSTED_ACTION = "whenExhaustedAction";
62     static final String JavaDoc AUTO_COMMIT = "defaultAutoCommit";
63     static final String JavaDoc READ_ONLY = "defaultReadOnly";
64     static final String JavaDoc TRANSACTION_ISOLATION = "defaultTransactionIsolation";
65     static final String JavaDoc CONNECTION_NOWRAP = "accessToUnderlyingConnectionAllowed";
66     static final String JavaDoc CATALOG = "defaultCatalog";
67
68     // PreparedStatementPool properties
69

70     static final String JavaDoc POOL_PS = "poolPreparedStatements";
71     static final String JavaDoc PS_MAX_ACTIVE = "ps.maxActive";
72     static final String JavaDoc PS_MAX_IDLE = "ps.maxIdle";
73     static final String JavaDoc PS_MAX_TOTAL = "ps.maxTotal";
74     static final String JavaDoc PS_MAX_WAIT = "ps.maxWait";
75     static final String JavaDoc PS_MIN_EVICTABLE_TIME = "ps.minEvictableIdleTimeMillis";
76     static final String JavaDoc PS_NUM_TEST_PER_EVICTION = "ps.numTestsPerEvictionRun";
77     static final String JavaDoc PS_TEST_ON_BORROW = "ps.testOnBorrow";
78     static final String JavaDoc PS_TEST_ON_RETURN = "ps.testOnReturn";
79     static final String JavaDoc PS_TEST_IDLE = "ps.testWhileIdle";
80     static final String JavaDoc PS_TIME_BETWEEN_EVICTIONS = "ps.timeBetweenEvictionRunsMillis";
81     static final String JavaDoc PS_EXHAUSTED_ACTION = "ps.whenExhaustedAction";
82
83     private DBCPDataSourceProperties config;
84
85     DBCPDataSourceBuilder(DBCPDataSourceProperties properties) {
86         this.config = properties;
87     }
88
89     DataSource JavaDoc createDataSource() {
90         boolean connectionNoWrap = config.getBoolean(CONNECTION_NOWRAP, false);
91         ObjectPool connectionPool = createConnectionPool();
92         PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
93         dataSource.setAccessToUnderlyingConnectionAllowed(connectionNoWrap);
94
95         return dataSource;
96     }
97
98     private void loadDriverClass() {
99         String JavaDoc driver = config.getString(DRIVER_CLASS_NAME, true);
100         try {
101             Class.forName(driver);
102         }
103         catch (ClassNotFoundException JavaDoc e) {
104             throw new ConfigurationException("Error loading driver " + driver, e);
105         }
106     }
107
108     private ObjectPool createConnectionPool() {
109
110         ConnectionFactory factory = createConnectionFactory();
111         GenericObjectPool.Config poolConfig = createConnectionPoolConfig();
112         KeyedObjectPoolFactory statementPool = createPreparedStatementPool();
113
114         String JavaDoc validationQuery = config.getString(VALIDATION_QUERY);
115         boolean defaultReadOnly = config.getBoolean(READ_ONLY, false);
116         boolean defaultAutoCommit = config.getBoolean(AUTO_COMMIT, false);
117         int defaultTransactionIsolation = config.getTransactionIsolation(
118                 TRANSACTION_ISOLATION,
119                 Connection.TRANSACTION_SERIALIZABLE);
120         String JavaDoc defaultCatalog = config.getString(CATALOG);
121
122         ObjectPool connectionPool = new GenericObjectPool(null, poolConfig);
123
124         // a side effect of PoolableConnectionFactory constructor call is that newly
125
// created factory object is assigned to "connectionPool", which is definitely a
126
// very confusing part of DBCP - new object is not visibly assigned to anything,
127
// still it is preserved...
128
new PoolableConnectionFactory(
129                 factory,
130                 connectionPool,
131                 statementPool,
132                 validationQuery,
133                 defaultReadOnly ? Boolean.TRUE : Boolean.FALSE,
134                 defaultAutoCommit,
135                 defaultTransactionIsolation,
136                 defaultCatalog,
137                 null);
138
139         return connectionPool;
140     }
141
142     private ConnectionFactory createConnectionFactory() {
143         loadDriverClass();
144         String JavaDoc url = config.getString(URL, true);
145         String JavaDoc userName = config.getString(USER_NAME);
146         String JavaDoc password = config.getString(PASSWORD);
147         return new DriverManagerConnectionFactory(url, userName, password);
148     }
149
150     private KeyedObjectPoolFactory createPreparedStatementPool() {
151
152         if (!config.getBoolean("poolPreparedStatements", false)) {
153             return null;
154         }
155
156         // the GenericKeyedObjectPool.Config object isn't used because
157
// although it has provision for the maxTotal parameter when
158
// passed to the GenericKeyedObjectPoolFactory constructor
159
// this parameter is not being properly set as a default for
160
// creating prepared statement pools
161

162         int maxActive = config
163                 .getInt(PS_MAX_ACTIVE, GenericObjectPool.DEFAULT_MAX_ACTIVE);
164         byte whenExhaustedAction = config.getWhenExhaustedAction(
165                 PS_EXHAUSTED_ACTION,
166                 GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION);
167         long maxWait = config.getLong(PS_MAX_WAIT, GenericObjectPool.DEFAULT_MAX_WAIT);
168         int maxIdle = config.getInt(PS_MAX_IDLE, GenericObjectPool.DEFAULT_MAX_IDLE);
169         int maxTotal = config.getInt(PS_MAX_TOTAL, 1);
170
171         boolean testOnBorrow = config.getBoolean(
172                 PS_TEST_ON_BORROW,
173                 GenericObjectPool.DEFAULT_TEST_ON_BORROW);
174         boolean testOnReturn = config.getBoolean(
175                 PS_TEST_ON_RETURN,
176                 GenericObjectPool.DEFAULT_TEST_ON_RETURN);
177
178         long timeBetweenEvictionRunsMillis = config.getLong(
179                 PS_TIME_BETWEEN_EVICTIONS,
180                 GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
181         int numTestsPerEvictionRun = config.getInt(
182                 PS_NUM_TEST_PER_EVICTION,
183                 GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
184
185         long minEvictableIdleTimeMillis = config.getLong(
186                 PS_MIN_EVICTABLE_TIME,
187                 GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
188
189         boolean testWhileIdle = config.getBoolean(
190                 PS_TEST_IDLE,
191                 GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
192
193         return new GenericKeyedObjectPoolFactory(
194                 null,
195                 maxActive,
196                 whenExhaustedAction,
197                 maxWait,
198                 maxIdle,
199                 maxTotal,
200                 testOnBorrow,
201                 testOnReturn,
202                 timeBetweenEvictionRunsMillis,
203                 numTestsPerEvictionRun,
204                 minEvictableIdleTimeMillis,
205                 testWhileIdle);
206     }
207
208     private GenericObjectPool.Config createConnectionPoolConfig() {
209         GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
210
211         poolConfig.maxIdle = config.getInt(MAX_IDLE, GenericObjectPool.DEFAULT_MAX_IDLE);
212         poolConfig.minIdle = config.getInt(MIN_IDLE, GenericObjectPool.DEFAULT_MIN_IDLE);
213         poolConfig.maxActive = config.getInt(
214                 MAX_ACTIVE,
215                 GenericObjectPool.DEFAULT_MAX_ACTIVE);
216         poolConfig.maxWait = config.getLong(MAX_WAIT, GenericObjectPool.DEFAULT_MAX_WAIT);
217
218         poolConfig.testOnBorrow = config.getBoolean(
219                 TEST_ON_BORROW,
220                 GenericObjectPool.DEFAULT_TEST_ON_BORROW);
221         poolConfig.testOnReturn = config.getBoolean(
222                 TEST_ON_RETURN,
223                 GenericObjectPool.DEFAULT_TEST_ON_RETURN);
224         poolConfig.testWhileIdle = config.getBoolean(
225                 TEST_IDLE,
226                 GenericObjectPool.DEFAULT_TEST_WHILE_IDLE);
227
228         poolConfig.timeBetweenEvictionRunsMillis = config.getLong(
229                 TIME_BETWEEN_EVICTIONS,
230                 GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
231         poolConfig.numTestsPerEvictionRun = config.getInt(
232                 NUM_TEST_PER_EVICTION,
233                 GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
234         poolConfig.minEvictableIdleTimeMillis = config.getLong(
235                 MIN_EVICTABLE_TIME,
236                 GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
237
238         poolConfig.whenExhaustedAction = config.getWhenExhaustedAction(
239                 EXHAUSTED_ACTION,
240                 GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION);
241
242         return poolConfig;
243     }
244 }
245
Popular Tags