001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.components.datasource;
018:
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021:
022: import javax.sql.DataSource;
023:
024: import org.apache.commons.dbcp.ConnectionFactory;
025: import org.apache.commons.dbcp.DriverManagerConnectionFactory;
026: import org.apache.commons.dbcp.PoolableConnectionFactory;
027: import org.apache.commons.dbcp.PoolingDataSource;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.commons.pool.ObjectPool;
031: import org.apache.commons.pool.impl.GenericObjectPool;
032:
033: /**
034: * <p>
035: * DBCPDatasourceComponent
036: * </p>
037: *
038: *
039: * @
040: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
041: * @version $ $
042: *
043: */
044: public class DBCPDatasourceComponent implements DatasourceComponent {
045:
046: private static final Log log = LogFactory
047: .getLog(DBCPDatasourceComponent.class);
048:
049: protected PoolingDataSource dataSource;
050: //protected DataSource dataSource;
051:
052: private String user;
053:
054: private String password;
055:
056: private String driverName;
057:
058: private String connectURI;
059:
060: private int maxActive;
061:
062: private int maxWait;
063:
064: private byte whenExhausted;
065:
066: private PoolableConnectionFactory dsConnectionFactory;
067:
068: /**
069: *
070: * Creates a simple commons DBCP connection pool using the following
071: * parameters.
072: * <p>
073: * If you need to bind the datasource of this component to
074: * JNDI please @see org.apache.jetspeed.components.jndi.JNDIComponent
075: * </p>
076: *
077: * @param user User name that will be used to connect to the DB
078: * @param password Password that will be used to connect to the DB
079: * @param driverName Fully qualified driver to be used by the connection pool
080: * @param connectURI Fully qualified URI to the DB.
081: * @param maxActive Maximum active connection
082: * @param maxWait if <code>whenExhausted</code> is set to GenericObjectPool.WHEN_EXHAUSTED_BLOCK
083: * the length of time to block while waiting for a connection to become
084: * available.
085: * @param whenExhausted GenericObjectPool.WHEN_EXHAUSTED_BLOCK, GenericObjectPool.WHEN_EXHAUSTED_GROW or
086: * GenericObjectPool.WHEN_EXHAUSTED_FAIL. @see org.apache.commons.pooling.GenericObjectPool
087: * for more information on these settings
088: * @param autoCommit Whether or not this datasource will autocommit
089: * @throws ClassNotFoundException If the <code>driverName</code> could not be
090: * located within any classloaders.
091: */
092: public DBCPDatasourceComponent(String user, String password,
093: String driverName, String connectURI, int maxActive,
094: int maxWait, byte whenExhausted, boolean autoCommit)
095:
096: {
097:
098: log.info("Setting up data source pooling for " + driverName);
099:
100: log.info("Max active connnections set to: " + maxActive);
101:
102: log.info("Pool is set to \"" + whenExhausted
103: + "\" when all connections are exhausted.");
104:
105: this .user = user;
106: this .password = password;
107: this .driverName = driverName;
108: this .connectURI = connectURI;
109: this .maxActive = maxActive;
110: this .maxWait = maxWait;
111: }
112:
113: /**
114: *
115: * <p>
116: * getDatasource
117: * </p>
118: *
119: * <p>
120: * returns the datasource created by this component
121: * </p>
122: * @return
123: *
124: */
125: public DataSource getDatasource() {
126: return dataSource;
127: }
128:
129: /**
130: * <p>
131: * start
132: * </p>
133: *
134: * @see org.picocontainer.Startable#start()
135: *
136: */
137: public void start() {
138:
139: try {
140: log.info("Attempting to start DBCPCDatasourceComponent.");
141: Class.forName(driverName);
142:
143: // Validate the connection before we go any further
144: try {
145: Connection conn = DriverManager.getConnection(
146: connectURI, user, password);
147: conn.close();
148: } catch (Exception e) {
149: log.error(
150: "Unable to obtain a connection database via URI: "
151: + connectURI, e);
152: throw e;
153: }
154:
155: ObjectPool connectionPool = new GenericObjectPool(null,
156: maxActive, whenExhausted, maxWait);
157:
158: ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
159: connectURI, user, password);
160:
161: dsConnectionFactory = new PoolableConnectionFactory(
162: connectionFactory, connectionPool, null, null,
163: false, true);
164:
165: dataSource = new PoolingDataSource(connectionPool);
166:
167: log.info("DBCPCDatasourceComponent successfuly started!");
168: } catch (Throwable e) {
169:
170: String msg = "Unable to start DBCPCDatasourceComponent: "
171: + e.toString();
172: log.error(msg, e);
173: throw new IllegalStateException(msg);
174: }
175: }
176:
177: /**
178: * <p>
179: * stop
180: * </p>
181: *
182: * @see org.picocontainer.Startable#stop()
183: *
184: */
185: public void stop() {
186: try {
187: dsConnectionFactory.getPool().close();
188: } catch (Exception e) {
189: IllegalStateException ise = new IllegalStateException(
190: "Unable to sfaely shutdown the DBCPConnection pool: "
191: + e.toString());
192: ise.initCause(e);
193: }
194: }
195:
196: }
|