001: package org.apache.torque.dsfactory;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import javax.sql.ConnectionPoolDataSource;
023: import javax.sql.DataSource;
024:
025: import org.apache.commons.configuration.Configuration;
026:
027: import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.apache.torque.Torque;
033: import org.apache.torque.TorqueException;
034:
035: /**
036: * A factory that looks up the DataSource using the JDBC2 pool methods.
037: *
038: * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
039: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040: * @version $Id: SharedPoolDataSourceFactory.java 476550 2006-11-18 16:08:37Z tfischer $
041: */
042: public class SharedPoolDataSourceFactory extends
043: AbstractDataSourceFactory {
044:
045: /** The log. */
046: private static Log log = LogFactory
047: .getLog(SharedPoolDataSourceFactory.class);
048:
049: /** The wrapped <code>DataSource</code>. */
050: private SharedPoolDataSource ds = null;
051:
052: /**
053: * @see org.apache.torque.dsfactory.DataSourceFactory#getDataSource
054: */
055: public DataSource getDataSource() {
056: return ds;
057: }
058:
059: /**
060: * @see org.apache.torque.dsfactory.DataSourceFactory#initialize
061: */
062: public void initialize(Configuration configuration)
063: throws TorqueException {
064: super .initialize(configuration);
065:
066: ConnectionPoolDataSource cpds = initCPDS(configuration);
067: SharedPoolDataSource dataSource = initJdbc2Pool(configuration);
068: dataSource.setConnectionPoolDataSource(cpds);
069: this .ds = dataSource;
070: }
071:
072: /**
073: * Initializes the Jdbc2PoolDataSource.
074: *
075: * @param configuration where to read the settings from
076: * @throws TorqueException if a property set fails
077: * @return a configured <code>Jdbc2PoolDataSource</code>
078: */
079: private SharedPoolDataSource initJdbc2Pool(
080: Configuration configuration) throws TorqueException {
081: log.debug("Starting initJdbc2Pool");
082: SharedPoolDataSource dataSource = new SharedPoolDataSource();
083: Configuration c = Torque.getConfiguration();
084:
085: if (c == null || c.isEmpty()) {
086: log.warn("Global Configuration not set,"
087: + " no Default pool data source configured!");
088: } else {
089: Configuration conf = c.subset(DEFAULT_POOL_KEY);
090: applyConfiguration(conf, dataSource);
091: }
092:
093: Configuration conf = configuration.subset(POOL_KEY);
094: applyConfiguration(conf, dataSource);
095: return dataSource;
096: }
097:
098: /**
099: * Closes the pool associated with this factory and releases it.
100: * @throws TorqueException if the pool cannot be closed properly
101: */
102: public void close() throws TorqueException {
103: try {
104: ds.close();
105: } catch (Exception e) {
106: log.error("Exception caught during close()", e);
107: throw new TorqueException(e);
108: }
109: ds = null;
110: }
111: }
|