001package org.apache.commons.jcs.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.dbcp.cpdsadapter.DriverAdapterCPDS; 028import org.apache.commons.dbcp.datasources.InstanceKeyDataSource; 029import org.apache.commons.dbcp.datasources.SharedPoolDataSource; 030import org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes; 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 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 * 039 * @author <a href="mailto:jmcnally@apache.org">John McNally</a> 040 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 041 */ 042public class SharedPoolDataSourceFactory implements DataSourceFactory 043{ 044 /** The log. */ 045 private static Log log = LogFactory.getLog(SharedPoolDataSourceFactory.class); 046 047 /** The name of the factory. */ 048 private String name; 049 050 /** The wrapped <code>DataSource</code>. */ 051 private SharedPoolDataSource ds = null; 052 053 /** 054 * @return the name of the factory. 055 */ 056 @Override 057 public String getName() 058 { 059 return name; 060 } 061 062 /** 063 * @see org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory#getDataSource() 064 */ 065 @Override 066 public DataSource getDataSource() 067 { 068 return ds; 069 } 070 071 /** 072 * @see org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory#initialize(JDBCDiskCacheAttributes) 073 */ 074 @Override 075 public void initialize(JDBCDiskCacheAttributes config) throws SQLException 076 { 077 this.name = config.getConnectionPoolName(); 078 ConnectionPoolDataSource cpds = initCPDS(config); 079 SharedPoolDataSource dataSource = new SharedPoolDataSource(); 080 initJdbc2Pool(dataSource, config); 081 dataSource.setConnectionPoolDataSource(cpds); 082 dataSource.setMaxActive(config.getMaxActive()); 083 this.ds = dataSource; 084 } 085 086 /** 087 * Closes the pool associated with this factory and releases it. 088 * @throws SQLException if the pool cannot be closed properly 089 */ 090 @Override 091 public void close() throws SQLException 092 { 093 try 094 { 095 ds.close(); 096 } 097 catch (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 ConnectionPoolDataSource initCPDS(final JDBCDiskCacheAttributes config) 112 throws SQLException 113 { 114 log.debug("Starting initCPDS"); 115 116 DriverAdapterCPDS cpds = new DriverAdapterCPDS(); 117 118 try 119 { 120 cpds.setDriver(config.getDriverClassName()); 121 } 122 catch (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 cpds.setMaxActive(config.getMaxActive()); 131 132 return cpds; 133 } 134 135 /** 136 * Initializes the Jdbc2PoolDataSource. 137 * 138 * @param dataSource the dataSource to initialize, not null. 139 * @param config where to read the settings from, not null. 140 * 141 * @throws SQLException if a property set fails. 142 */ 143 private void initJdbc2Pool(final InstanceKeyDataSource dataSource, final JDBCDiskCacheAttributes config) 144 throws SQLException 145 { 146 log.debug("Starting initJdbc2Pool"); 147 148 dataSource.setDescription(config.getConnectionPoolName()); 149 } 150}