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.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
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 }