001package org.apache.commons.jcs3.auxiliary.disk.jdbc.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
022import java.sql.SQLException;
023
024import javax.sql.ConnectionPoolDataSource;
025import javax.sql.DataSource;
026
027import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS;
028import org.apache.commons.dbcp2.datasources.InstanceKeyDataSource;
029import org.apache.commons.dbcp2.datasources.SharedPoolDataSource;
030import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCacheAttributes;
031import org.apache.commons.jcs3.log.Log;
032import org.apache.commons.jcs3.log.LogManager;
033
034/**
035 * A factory that looks up the DataSource using the JDBC2 pool methods.
036 *
037 * Borrowed and adapted from Apache DB Torque
038 */
039public class SharedPoolDataSourceFactory implements DataSourceFactory
040{
041    /** The log. */
042    private static final Log log = LogManager.getLog(SharedPoolDataSourceFactory.class);
043
044    /** The name of the factory. */
045    private String name;
046
047    /** The wrapped <code>DataSource</code>. */
048    private SharedPoolDataSource ds;
049
050    /**
051     * @return the name of the factory.
052     */
053    @Override
054        public String getName()
055    {
056        return name;
057    }
058
059    /**
060     * @see org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.DataSourceFactory#getDataSource()
061     */
062    @Override
063    public DataSource getDataSource()
064    {
065        return ds;
066    }
067
068    /**
069     * @see org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.DataSourceFactory#initialize(JDBCDiskCacheAttributes)
070     */
071    @Override
072        public void initialize(final JDBCDiskCacheAttributes config) throws SQLException
073    {
074        this.name = config.getConnectionPoolName();
075        final ConnectionPoolDataSource cpds = initCPDS(config);
076        final SharedPoolDataSource dataSource = new SharedPoolDataSource();
077        initJdbc2Pool(dataSource, config);
078        dataSource.setConnectionPoolDataSource(cpds);
079        dataSource.setMaxTotal(config.getMaxTotal());
080        this.ds = dataSource;
081    }
082
083    /**
084     * Closes the pool associated with this factory and releases it.
085     * @throws SQLException if the pool cannot be closed properly
086     */
087    @Override
088        public void close() throws SQLException
089    {
090        try
091        {
092            if (ds != null)
093            {
094                ds.close();
095            }
096        }
097        catch (final Exception e)
098        {
099                throw new SQLException("Exception caught closing data source", e);
100        }
101        ds = null;
102    }
103
104    /**
105     * Initializes the ConnectionPoolDataSource.
106     *
107     * @param config where to read the settings from
108     * @throws SQLException if a property set fails
109     * @return a configured <code>ConnectionPoolDataSource</code>
110     */
111    private static ConnectionPoolDataSource initCPDS(final JDBCDiskCacheAttributes config)
112        throws SQLException
113    {
114        log.debug("Starting initCPDS");
115
116        final DriverAdapterCPDS cpds = new DriverAdapterCPDS();
117
118        try
119        {
120                        cpds.setDriver(config.getDriverClassName());
121                }
122        catch (final ClassNotFoundException e)
123        {
124                        throw new SQLException("Driver class not found " + config.getDriverClassName(), e);
125                }
126
127        cpds.setUrl(config.getUrl());
128        cpds.setUser(config.getUserName());
129        cpds.setPassword(config.getPassword());
130
131        return cpds;
132    }
133
134    /**
135     * Initializes the Jdbc2PoolDataSource.
136     *
137     * @param dataSource the dataSource to initialize, not null.
138     * @param config where to read the settings from, not null.
139     *
140     * @throws SQLException if a property set fails.
141     */
142    private static void initJdbc2Pool(final InstanceKeyDataSource dataSource, final JDBCDiskCacheAttributes config)
143        throws SQLException
144    {
145        log.debug("Starting initJdbc2Pool");
146
147        dataSource.setDescription(config.getConnectionPoolName());
148    }
149}