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 pm@jcake.com.
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.DriverManager;
52  import java.sql.SQLException;
53  import java.util.Properties;
54  
55  import org.apache.commons.logging.Log;
56  
57  /***
58   * The most simple connection provider. It uses the following properies
59   * to establish connection:
60   *
61   * <ul>
62   *   <li>dbmonster.jdbc.driver (ie. org.postgresql.Driver)</li>
63   *   <li>dbmonster.jdbc.url (ie. jdbc:postgresql://127.0.0.1/dbmonster)</li>
64   *   <li>dbmonster.jdbc.username (ie. dbmonster)</li>
65   *   <li>dbmonster.jdbc.password (ie. dbmonster)</li>
66   * </ul>
67   *
68   * By default these properties are taken from System.getProperty, but you may
69   * also provide them on your own.
70   *
71   * @author Piotr Maj
72   *
73   * @version $Id: SimpleConnectionProvider.java,v 1.2 2006/01/05 16:29:37 majek Exp $
74   */
75  public class SimpleConnectionProvider
76      implements ConnectionProvider, LogEnabled {
77  
78      /***
79       * JDBC driver name.
80       */
81      private String driver = System.getProperty("dbmonster.jdbc.driver");
82  
83      /***
84       * JDBC url.
85       */
86      private String url = System.getProperty("dbmonster.jdbc.url");
87  
88      /***
89       * JDBC user name.
90       */
91      private String username = System.getProperty("dbmonster.jdbc.username");
92  
93      /***
94       * JDBC user's password.
95       */
96      private String password = System.getProperty("dbmonster.jdbc.password");
97  
98      /***
99       * Properties used to establish connection.
100      */
101     private Properties properties = null;
102 
103     /***
104      * Logger.
105      */
106     private Log logger = null;
107 
108     private Connection connection;
109 
110     private boolean autoCommit = true;
111 
112     /***
113      * Creates new SimpleConnectionProvider.
114      *
115      * @throws ClassNotFoundException if driver class could not be found.
116      */
117     public SimpleConnectionProvider() throws ClassNotFoundException {
118         initDriver();
119     }
120 
121     /***
122      * Creates new SimpleConnectionProvider with given connection info.
123      *
124      * @param jdbcDriver JDBC driver
125      * @param jdbcUrl JDBC url
126      * @param jdbcUsername JDBC user name
127      * @param jdbcPassword JDBC password
128      *
129      * @throws ClassNotFoundException if the driver class could not be found.
130      */
131     public SimpleConnectionProvider(
132         String jdbcDriver,
133         String jdbcUrl,
134         String jdbcUsername,
135         String jdbcPassword) throws ClassNotFoundException {
136         driver = jdbcDriver;
137         url = jdbcUrl;
138         username = jdbcUsername;
139         password = jdbcPassword;
140         initDriver();
141     }
142 
143     /***
144      * Creates new SimpleConnectionProvider with properties (usefull
145      * for interbase).
146      *
147      * @param jdbcDriver JDBC driver
148      * @param jdbcUrl JDBC url
149      * @param props properties
150      *
151      * @throws ClassNotFoundException if driver class could not be found.
152      */
153     public SimpleConnectionProvider(
154         String jdbcDriver,
155         String jdbcUrl,
156         Properties props) throws ClassNotFoundException {
157         driver = jdbcDriver;
158         url = jdbcUrl;
159         properties = props;
160         initDriver();
161     }
162 
163     /***
164      * @see ConnectionProvider#getConnection()
165      */
166     public Connection getConnection() throws SQLException {
167         connection = null;
168         if (properties == null) {
169             connection = DriverManager.getConnection(url, username, password);
170         } else {
171             connection = DriverManager.getConnection(url, properties);
172         }
173         connection.setAutoCommit(autoCommit);
174         return connection;
175     }
176 
177     /***
178      * @see ConnectionProvider#testConnection()
179      */
180     public void testConnection() throws SQLException {
181         Connection conn = getConnection();
182         DatabaseMetaData dbmd = conn.getMetaData();
183         if (logger != null && logger.isInfoEnabled()) {
184             logger.info("Today we are feeding: "
185                 + dbmd.getDatabaseProductName() + " "
186                 + dbmd.getDatabaseProductVersion());
187         }
188         conn.close();
189         dbmd = null;
190         conn = null;
191     }
192 
193     /***
194      * Sets the logger.
195      *
196      * @param log a logger.
197      */
198     public void setLogger(Log log) {
199         logger = log;
200     }
201 
202     /***
203      * Shutdown this connection provider.
204      */
205     public void shutdown() {
206         try {
207             if (connection != null && !connection.isClosed()) {
208                 connection.close();
209             }
210         } catch (SQLException e) {
211             logger.error("Counld not close the JDBC connection.", e);
212             // ignoring the rest
213         }
214         logger = null;
215         driver = null;
216         password = null;
217         properties = null;
218         url = null;
219         username = null;
220     }
221 
222     /***
223      * Initializes the JDBC driver.
224      *
225      * @throws ClassNotFoundException if driver class could not be found.
226      */
227     private void initDriver() throws ClassNotFoundException {
228         Class.forName(driver);
229     }
230 
231     public void setAutoCommit(boolean autoCommit) {
232         this.autoCommit = autoCommit;
233     }
234 }