View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.SQLException;
23  
24  import javax.sql.ConnectionPoolDataSource;
25  import javax.sql.DataSource;
26  
27  import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
28  import org.apache.commons.dbcp.datasources.InstanceKeyDataSource;
29  import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
30  import org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * A factory that looks up the DataSource using the JDBC2 pool methods.
36   *
37   * Borrowed and adapted from Apache DB Torque
38   *
39   * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
40   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
41   */
42  public class SharedPoolDataSourceFactory implements DataSourceFactory
43  {
44      /** The log. */
45      private static Log log = LogFactory.getLog(SharedPoolDataSourceFactory.class);
46  
47      /** The name of the factory. */
48      private String name;
49  
50      /** The wrapped <code>DataSource</code>. */
51      private SharedPoolDataSource ds = null;
52  
53      /**
54       * @return the name of the factory.
55       */
56      @Override
57  	public String getName()
58      {
59      	return name;
60      }
61  
62      /**
63       * @see org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory#getDataSource()
64       */
65      @Override
66      public DataSource getDataSource()
67      {
68          return ds;
69      }
70  
71      /**
72       * @see org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory#initialize(JDBCDiskCacheAttributes)
73       */
74      @Override
75  	public void initialize(JDBCDiskCacheAttributes config) throws SQLException
76      {
77      	this.name = config.getConnectionPoolName();
78          ConnectionPoolDataSource cpds = initCPDS(config);
79          SharedPoolDataSource dataSource = new SharedPoolDataSource();
80          initJdbc2Pool(dataSource, config);
81          dataSource.setConnectionPoolDataSource(cpds);
82          dataSource.setMaxActive(config.getMaxActive());
83          this.ds = dataSource;
84      }
85  
86      /**
87       * Closes the pool associated with this factory and releases it.
88       * @throws SQLException if the pool cannot be closed properly
89       */
90      @Override
91  	public void close() throws SQLException
92      {
93          try
94          {
95              ds.close();
96          }
97          catch (Exception e)
98          {
99          	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 }