View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package pl.kernelpanic.dbmonster.connection;
48  
49  import java.sql.Connection;
50  import java.sql.DatabaseMetaData;
51  import java.sql.SQLException;
52  import java.util.Properties;
53  
54  import javax.sql.DataSource;
55  
56  import org.apache.commons.dbcp.ConnectionFactory;
57  import org.apache.commons.dbcp.PoolingDataSource;
58  import org.apache.commons.dbcp.PoolableConnectionFactory;
59  import org.apache.commons.dbcp.DriverManagerConnectionFactory;
60  import org.apache.commons.logging.Log;
61  import org.apache.commons.pool.ObjectPool;
62  import org.apache.commons.pool.impl.GenericObjectPool;
63  
64  /***
65   * The connection provider which uses commons-dbcp.
66   * It uses the following properties to establish connection:
67   *
68   * <ul>
69   *   <li>dbmonster.jdbc.driver (ie. org.postgresql.Driver)</li>
70   *   <li>dbmonster.jdbc.url (ie. jdbc:postgresql://127.0.0.1/dbmonster)</li>
71   *   <li>dbmonster.jdbc.username (ie. dbmonster)</li>
72   *   <li>dbmonster.jdbc.password (ie. dbmonster)</li>
73   * </ul>
74   *
75   * By default these properties are taken from System.getProperty, but you may
76   * also provide them on your own.
77   *
78   * @author Piotr Maj
79   *
80   * @version $Id: DBCPConnectionProvider.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
81   */
82  public class DBCPConnectionProvider implements ConnectionProvider, LogEnabled {
83  
84      /***
85       * JDBC driver name.
86       */
87      private String driver = System.getProperty("dbmonster.jdbc.driver");
88  
89      /***
90       * JDBC url.
91       */
92      private String url = System.getProperty("dbmonster.jdbc.url");
93  
94      /***
95       * JDBC user name.
96       */
97      private String username = System.getProperty("dbmonster.jdbc.username");
98  
99      /***
100      * JDBC user's password.
101      */
102     private String password = System.getProperty("dbmonster.jdbc.password");
103 
104     /***
105      * Properties used to establish connection.
106      */
107     private Properties properties = null;
108 
109     /***
110      * A datasource.
111      */
112     private DataSource dataSource = null;
113 
114      /***
115      * Logger.
116      */
117     private Log logger = null;
118 
119     /***
120      * Connection pool handler.
121      */
122     private ObjectPool pool;
123 
124     /***
125      * Creates new SimpleConnectionProvider.
126      *
127      * @throws Exception if driver class could not be found.
128      */
129     public DBCPConnectionProvider() throws Exception {
130         initDriver();
131         setupPool();
132     }
133 
134     /***
135      * Creates new DBCPConnectionProvider with given connection info.
136      *
137      * @param jdbcDriver JDBC driver
138      * @param jdbcUrl JDBC url
139      * @param jdbcUsername JDBC user name
140      * @param jdbcPassword JDBC password
141      *
142      * @throws Exception if the driver class could not be found.
143      */
144     public DBCPConnectionProvider(
145         final String jdbcDriver,
146         final String jdbcUrl,
147         final String jdbcUsername,
148         final String jdbcPassword) throws Exception {
149         driver = jdbcDriver;
150         url = jdbcUrl;
151         username = jdbcUsername;
152         password = jdbcPassword;
153         initDriver();
154         setupPool();
155     }
156 
157     /***
158      * Creates new DBCPConnectionProvider with properties (usefull
159      * for interbase).
160      *
161      * @param jdbcDriver JDBC driver
162      * @param jdbcUrl JDBC url
163      * @param props properties
164      *
165      * @throws Exception if driver class could not be found.
166      */
167     public DBCPConnectionProvider(
168         final String jdbcDriver,
169         final String jdbcUrl,
170         final Properties props) throws Exception {
171         driver = jdbcDriver;
172         url = jdbcUrl;
173         properties = props;
174         initDriver();
175         setupPool();
176     }
177 
178     /***
179      * @see ConnectionProvider#getConnection()
180      */
181     public final Connection getConnection() throws SQLException {
182         if (dataSource == null) {
183             throw new SQLException("Data source is null!");
184         }
185         Connection conn = dataSource.getConnection();
186         conn.setAutoCommit(true);
187         return conn;
188     }
189 
190     /***
191      * @see ConnectionProvider#testConnection()
192      */
193     public final void testConnection() throws SQLException {
194         Connection conn = getConnection();
195         DatabaseMetaData dbmd = conn.getMetaData();
196         if (logger != null && logger.isInfoEnabled()) {
197             logger.info("Today we are feeding: "
198                 + dbmd.getDatabaseProductName() + " "
199                 + dbmd.getDatabaseProductVersion());
200         }
201         conn.close();
202         dbmd = null;
203         conn = null;
204     }
205 
206     /***
207      * Sets the logger.
208      *
209      * @param log a logger.
210      */
211     public final void setLogger(final Log log) {
212         logger = log;
213     }
214 
215     /***
216      * Shutdown this connection provider.
217      */
218     public final void shutdown() {
219         try {
220             pool.close();
221         } catch (Exception e) {
222         } finally {
223             pool = null;
224         }
225         dataSource = null;
226         driver = null;
227         logger = null;
228         password = null;
229         properties = null;
230         url = null;
231         username = null;
232     }
233 
234     /***
235      * Initializes the JDBC driver.
236      *
237      * @throws ClassNotFoundException if driver class could not be found.
238      */
239     private final void initDriver() throws ClassNotFoundException {
240         Class.forName(driver);
241     }
242 
243     /***
244      * Sets the pool up.
245      *
246      * @throws Exception if the pool cannot be set up
247      */
248     private final void setupPool() throws Exception {
249         pool = new GenericObjectPool(null);
250         ConnectionFactory connFactory = null;
251         if (properties != null) {
252             connFactory = new DriverManagerConnectionFactory(url, properties);
253         } else {
254             connFactory =
255                 new DriverManagerConnectionFactory(url, username, password);
256         }
257         new PoolableConnectionFactory(
258             connFactory, pool, null, null, false, true);
259         dataSource = new PoolingDataSource(pool);
260     }
261 }