1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 }