View Javadoc
1   package org.apache.commons.jcs3.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.dbcp2.cpdsadapter.DriverAdapterCPDS;
28  import org.apache.commons.dbcp2.datasources.InstanceKeyDataSource;
29  import org.apache.commons.dbcp2.datasources.SharedPoolDataSource;
30  import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCacheAttributes;
31  import org.apache.commons.jcs3.log.Log;
32  import org.apache.commons.jcs3.log.LogManager;
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  public class SharedPoolDataSourceFactory implements DataSourceFactory
40  {
41      /** The log. */
42      private static final Log log = LogManager.getLog(SharedPoolDataSourceFactory.class);
43  
44      /** The name of the factory. */
45      private String name;
46  
47      /** The wrapped <code>DataSource</code>. */
48      private SharedPoolDataSource ds;
49  
50      /**
51       * @return the name of the factory.
52       */
53      @Override
54  	public String getName()
55      {
56      	return name;
57      }
58  
59      /**
60       * @see org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.DataSourceFactory#getDataSource()
61       */
62      @Override
63      public DataSource getDataSource()
64      {
65          return ds;
66      }
67  
68      /**
69       * @see org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.DataSourceFactory#initialize(JDBCDiskCacheAttributes)
70       */
71      @Override
72  	public void initialize(final JDBCDiskCacheAttributes config) throws SQLException
73      {
74      	this.name = config.getConnectionPoolName();
75          final ConnectionPoolDataSource cpds = initCPDS(config);
76          final SharedPoolDataSource dataSource = new SharedPoolDataSource();
77          initJdbc2Pool(dataSource, config);
78          dataSource.setConnectionPoolDataSource(cpds);
79          dataSource.setMaxTotal(config.getMaxTotal());
80          this.ds = dataSource;
81      }
82  
83      /**
84       * Closes the pool associated with this factory and releases it.
85       * @throws SQLException if the pool cannot be closed properly
86       */
87      @Override
88  	public void close() throws SQLException
89      {
90          try
91          {
92              if (ds != null)
93              {
94                  ds.close();
95              }
96          }
97          catch (final 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 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 }