001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jdbc.datasource;
018:
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.SQLException;
022:
023: import org.compass.gps.device.jdbc.CannotGetJdbcConnectionException;
024:
025: /**
026: * Simple implementation of the standard JDBC DataSource interface, configuring
027: * a plain old JDBC Driver via bean properties, and returning a new Connection
028: * for every <code>getConnection</code> call.
029: * <p>
030: * Useful for test or standalone environments outside of a J2EE container.
031: * Pool-assuming <code>Connection.close()</code> calls will simply close the
032: * Connection, so any DataSource-aware persistence code should work.
033: * <p>
034: * In a J2EE container, it is recommended to use a JNDI DataSource provided by
035: * the container.
036: * <p>
037: * If you need a "real" connection pool outside of a J2EE container, consider <a
038: * href="http://jakarta.apache.org/commons/dbcp">Apache's Jakarta Commons DBCP</a>.
039: * Its BasicDataSource is a full connection pool bean, supporting the same basic
040: * properties as this class plus specific settings.
041: * <p>
042: * Taken from Spring.
043: *
044: * @author kimchy
045: */
046: public class DriverManagerDataSource extends AbstractDataSource {
047:
048: private String driverClassName;
049:
050: private String url;
051:
052: private String username;
053:
054: private String password;
055:
056: /**
057: * Constructor for bean-style configuration.
058: */
059: public DriverManagerDataSource() {
060: }
061:
062: /**
063: * Create a new DriverManagerDataSource with the given standard
064: * DriverManager parameters.
065: *
066: * @param driverClassName
067: * the JDBC driver class name
068: * @param url
069: * the JDBC URL to use for accessing the DriverManager
070: * @param username
071: * the JDBC username to use for accessing the DriverManager
072: * @param password
073: * the JDBC password to use for accessing the DriverManager
074: * @see java.sql.DriverManager#getConnection(String, String, String)
075: */
076: public DriverManagerDataSource(String driverClassName, String url,
077: String username, String password)
078: throws CannotGetJdbcConnectionException {
079: setDriverClassName(driverClassName);
080: setUrl(url);
081: setUsername(username);
082: setPassword(password);
083: }
084:
085: /**
086: * Create a new DriverManagerDataSource with the given standard
087: * DriverManager parameters.
088: *
089: * @param url
090: * the JDBC URL to use for accessing the DriverManager
091: * @param username
092: * the JDBC username to use for accessing the DriverManager
093: * @param password
094: * the JDBC password to use for accessing the DriverManager
095: * @see java.sql.DriverManager#getConnection(String, String, String)
096: */
097: public DriverManagerDataSource(String url, String username,
098: String password) throws CannotGetJdbcConnectionException {
099: setUrl(url);
100: setUsername(username);
101: setPassword(password);
102: }
103:
104: /**
105: * Create a new DriverManagerDataSource with the given JDBC URL, not
106: * specifying a username or password for JDBC access.
107: *
108: * @param url
109: * the JDBC URL to use for accessing the DriverManager
110: * @see java.sql.DriverManager#getConnection(String)
111: */
112: public DriverManagerDataSource(String url)
113: throws CannotGetJdbcConnectionException {
114: setUrl(url);
115: }
116:
117: /**
118: * Set the JDBC driver class name. This driver will get initialized on
119: * startup, registering itself with the JDK's DriverManager.
120: * <p>
121: * Alternatively, consider initializing the JDBC driver yourself before
122: * instantiating this DataSource.
123: *
124: * @see Class#forName(String)
125: * @see java.sql.DriverManager#registerDriver(java.sql.Driver)
126: */
127: public void setDriverClassName(String driverClassName)
128: throws CannotGetJdbcConnectionException {
129: this .driverClassName = driverClassName;
130: try {
131: Class.forName(this .driverClassName, true, Thread
132: .currentThread().getContextClassLoader());
133: } catch (ClassNotFoundException ex) {
134: throw new CannotGetJdbcConnectionException(
135: "Could not load JDBC driver class ["
136: + this .driverClassName + "]", ex);
137: }
138: if (log.isInfoEnabled()) {
139: log.info("Loaded JDBC driver [" + this .driverClassName
140: + "]");
141: }
142: }
143:
144: /**
145: * Return the JDBC driver class name, if any.
146: */
147: public String getDriverClassName() {
148: return driverClassName;
149: }
150:
151: /**
152: * Set the JDBC URL to use for accessing the DriverManager.
153: *
154: * @see java.sql.DriverManager#getConnection(String, String, String)
155: */
156: public void setUrl(String url) {
157: this .url = url;
158: }
159:
160: /**
161: * Return the JDBC URL to use for accessing the DriverManager.
162: */
163: public String getUrl() {
164: return url;
165: }
166:
167: /**
168: * Set the JDBC username to use for accessing the DriverManager.
169: *
170: * @see java.sql.DriverManager#getConnection(String, String, String)
171: */
172: public void setUsername(String username) {
173: this .username = username;
174: }
175:
176: /**
177: * Return the JDBC username to use for accessing the DriverManager.
178: */
179: public String getUsername() {
180: return username;
181: }
182:
183: /**
184: * Set the JDBC password to use for accessing the DriverManager.
185: *
186: * @see java.sql.DriverManager#getConnection(String, String, String)
187: */
188: public void setPassword(String password) {
189: this .password = password;
190: }
191:
192: /**
193: * Return the JDBC password to use for accessing the DriverManager.
194: */
195: public String getPassword() {
196: return password;
197: }
198:
199: /**
200: * This implementation delegates to
201: * <code>getConnectionFromDriverManager</code>, using the default
202: * username and password of this DataSource.
203: *
204: * @see #getConnectionFromDriverManager()
205: */
206: public Connection getConnection() throws SQLException {
207: return getConnectionFromDriverManager();
208: }
209:
210: /**
211: * This implementation delegates to
212: * <code>getConnectionFromDriverManager</code>, using the given username
213: * and password.
214: *
215: * @see #getConnectionFromDriverManager(String, String, String)
216: */
217: public Connection getConnection(String username, String password)
218: throws SQLException {
219: return getConnectionFromDriverManager(getUrl(), username,
220: password);
221: }
222:
223: /**
224: * Get a Connection from the DriverManager, using the default username and
225: * password of this DataSource.
226: *
227: * @see #getConnectionFromDriverManager(String, String, String)
228: */
229: protected Connection getConnectionFromDriverManager()
230: throws SQLException {
231: return getConnectionFromDriverManager(getUrl(), getUsername(),
232: getPassword());
233: }
234:
235: /**
236: * Getting a connection using the nasty static from DriverManager is
237: * extracted into a protected method to allow for easy unit testing.
238: *
239: * @see java.sql.DriverManager#getConnection(String, String, String)
240: */
241: protected Connection getConnectionFromDriverManager(String url,
242: String username, String password) throws SQLException {
243:
244: if (log.isDebugEnabled()) {
245: log.debug("Creating new JDBC connection to [" + url + "]");
246: }
247: return DriverManager.getConnection(url, username, password);
248: }
249:
250: }
|