1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.components.datasource;
1819import java.sql.Connection;
20import java.sql.DriverManager;
2122import javax.sql.DataSource;
2324import org.apache.commons.dbcp.ConnectionFactory;
25import org.apache.commons.dbcp.DriverManagerConnectionFactory;
26import org.apache.commons.dbcp.PoolableConnectionFactory;
27import org.apache.commons.dbcp.PoolingDataSource;
28import org.apache.commons.logging.Log;
29import org.apache.commons.logging.LogFactory;
30import org.apache.commons.pool.ObjectPool;
31import org.apache.commons.pool.impl.GenericObjectPool;
3233/***34 * <p>35 * DBCPDatasourceComponent36 * </p>37 * 38 * 39 * @40 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>41 * @version $ $42 *43 */44publicclassDBCPDatasourceComponent implements DatasourceComponent45 {
4647privatestaticfinal Log log = LogFactory.getLog(DBCPDatasourceComponent.class);
4849protected PoolingDataSource dataSource;
50//protected DataSource dataSource;5152private String user;
5354private String password;
5556private String driverName;
5758private String connectURI;
5960privateint maxActive;
6162privateint maxWait;
6364private byte whenExhausted;
6566private PoolableConnectionFactory dsConnectionFactory;
67/***68 * 69 * Creates a simple commons DBCP connection pool using the following70 * parameters.71 * <p>72 * If you need to bind the datasource of this component to 73 * JNDI please @see org.apache.jetspeed.components.jndi.JNDIComponent74 * </p>75 * 76 * @param user User name that will be used to connect to the DB77 * @param password Password that will be used to connect to the DB78 * @param driverName Fully qualified driver to be used by the connection pool79 * @param connectURI Fully qualified URI to the DB.80 * @param maxActive Maximum active connection81 * @param maxWait if <code>whenExhausted</code> is set to GenericObjectPool.WHEN_EXHAUSTED_BLOCK82 * the length of time to block while waiting for a connection to become 83 * available.84 * @param whenExhausted GenericObjectPool.WHEN_EXHAUSTED_BLOCK, GenericObjectPool.WHEN_EXHAUSTED_GROW or85 * GenericObjectPool.WHEN_EXHAUSTED_FAIL. @see org.apache.commons.pooling.GenericObjectPool86 * for more information on these settings87 * @param autoCommit Whether or not this datasource will autocommit88 * @throws ClassNotFoundException If the <code>driverName</code> could not be89 * located within any classloaders.90 */91publicDBCPDatasourceComponent(
92 String user,
93 String password,
94 String driverName,
95 String connectURI,
96int maxActive,
97int maxWait,
98 byte whenExhausted,
99boolean autoCommit)
100101 {
102103 log.info("Setting up data source pooling for " + driverName);
104105 log.info("Max active connnections set to: " + maxActive);
106107 log.info("Pool is set to \"" + whenExhausted + "\" when all connections are exhausted.");
108109this.user = user;
110this.password = password;
111this.driverName = driverName;
112this.connectURI = connectURI;
113this.maxActive = maxActive;
114this.maxWait = maxWait;
115 }
116117/***118 * 119 * <p>120 * getDatasource121 * </p>122 * 123 * <p>124 * returns the datasource created by this component125 * </p>126 * @return127 *128 */129public DataSource getDatasource()
130 {
131return dataSource;
132 }
133134/*** 135 * <p>136 * start137 * </p>138 * 139 * @see org.picocontainer.Startable#start()140 * 141 */142publicvoid start()
143 {
144145try146 {
147 log.info("Attempting to start DBCPCDatasourceComponent.");
148 Class.forName(driverName);
149150// Validate the connection before we go any further151try152 {
153 Connection conn = DriverManager.getConnection(connectURI, user, password);
154 conn.close();
155 }
156catch(Exception e)
157 {
158 log.error("Unable to obtain a connection database via URI: "+connectURI, e);
159throw e;
160 }
161162 ObjectPool connectionPool = new GenericObjectPool(null, maxActive, whenExhausted, maxWait);
163164 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, user, password);
165166 dsConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
167168 dataSource = new PoolingDataSource(connectionPool);
169170 log.info("DBCPCDatasourceComponent successfuly started!");
171 }
172catch (Throwable e)
173 {
174175 String msg = "Unable to start DBCPCDatasourceComponent: "+e.toString();
176 log.error(msg, e);
177thrownew IllegalStateException(msg);
178 }
179 }
180181/*** 182 * <p>183 * stop184 * </p>185 * 186 * @see org.picocontainer.Startable#stop()187 * 188 */189publicvoid stop()
190 {
191try192 {
193 dsConnectionFactory.getPool().close();
194 }
195catch (Exception e)
196 {
197 IllegalStateException ise =
198new IllegalStateException("Unable to sfaely shutdown the DBCPConnection pool: " + e.toString());
199 ise.initCause(e);
200 }
201 }
202203 }