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.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.html,v 1.1 2007/06/21 08:38:14 sbahloul 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     /***
109      * Creates new SimpleConnectionProvider.
110      *
111      * @throws ClassNotFoundException if driver class could not be found.
112      */
113     public SimpleConnectionProvider() throws ClassNotFoundException {
114         initDriver();
115     }
116 
117     /***
118      * Creates new SimpleConnectionProvider with given connection info.
119      *
120      * @param jdbcDriver JDBC driver
121      * @param jdbcUrl JDBC url
122      * @param jdbcUsername JDBC user name
123      * @param jdbcPassword JDBC password
124      *
125      * @throws ClassNotFoundException if the driver class could not be found.
126      */
127     public SimpleConnectionProvider(
128         final String jdbcDriver,
129         final String jdbcUrl,
130         final String jdbcUsername,
131         final String jdbcPassword) throws ClassNotFoundException {
132         driver = jdbcDriver;
133         url = jdbcUrl;
134         username = jdbcUsername;
135         password = jdbcPassword;
136         initDriver();
137     }
138 
139     /***
140      * Creates new SimpleConnectionProvider with properties (usefull
141      * for interbase).
142      *
143      * @param jdbcDriver JDBC driver
144      * @param jdbcUrl JDBC url
145      * @param props properties
146      *
147      * @throws ClassNotFoundException if driver class could not be found.
148      */
149     public SimpleConnectionProvider(
150         final String jdbcDriver,
151         final String jdbcUrl,
152         final Properties props) throws ClassNotFoundException {
153         driver = jdbcDriver;
154         url = jdbcUrl;
155         properties = props;
156         initDriver();
157     }
158 
159     /***
160      * @see ConnectionProvider#getConnection()
161      */
162     public final Connection getConnection() throws SQLException {
163         if (properties == null) {
164             return DriverManager.getConnection(url, username, password);
165         } else {
166             return DriverManager.getConnection(url, properties);
167         }
168     }
169 
170     /***
171      * @see ConnectionProvider#testConnection()
172      */
173     public final void testConnection() throws SQLException {
174         Connection conn = getConnection();
175         DatabaseMetaData dbmd = conn.getMetaData();
176         if (logger != null && logger.isInfoEnabled()) {
177             logger.info("Today we are feeding: "
178                 + dbmd.getDatabaseProductName() + " "
179                 + dbmd.getDatabaseProductVersion());
180         }
181         conn.close();
182         dbmd = null;
183         conn = null;
184     }
185 
186     /***
187      * Sets the logger.
188      *
189      * @param log a logger.
190      */
191     public final void setLogger(final Log log) {
192         logger = log;
193     }
194 
195     /***
196      * Shutdown this connection provider.
197      */
198     public final void shutdown() {
199         logger = null;
200         driver = null;
201         password = null;
202         properties = null;
203         url = null;
204         username = null;
205     }
206 
207     /***
208      * Initializes the JDBC driver.
209      *
210      * @throws ClassNotFoundException if driver class could not be found.
211      */
212     private final void initDriver() throws ClassNotFoundException {
213         Class.forName(driver);
214     }
215 }