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.apache.lucene.store.jdbc.datasource;
018:
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.SQLException;
022:
023: /**
024: * Simple implementation of the standard JDBC DataSource interface, configuring
025: * a plain old JDBC Driver via bean properties, and returning a new Connection
026: * for every <code>getConnection</code> call.
027: * <p/>
028: * Useful for test or standalone environments outside of a J2EE container.
029: * Pool-assuming <code>Connection.close()</code> calls will simply close the
030: * Connection, so any DataSource-aware persistence code should work.
031: * <p/>
032: * In a J2EE container, it is recommended to use a JNDI DataSource provided by
033: * the container.
034: * <p/>
035: * If you need a "real" connection pool outside of a J2EE container, consider <a
036: * href="http://jakarta.apache.org/commons/dbcp">Apache's Jakarta Commons DBCP</a>.
037: * Its BasicDataSource is a full connection pool bean, supporting the same basic
038: * properties as this class plus specific settings.
039: * <p/>
040: * Note, autoCommit property defaults to <code>false<code>.
041: *
042: * @author kimchy
043: */
044: public class DriverManagerDataSource extends AbstractDataSource {
045:
046: private String driverClassName;
047:
048: private String url;
049:
050: private String username;
051:
052: private String password;
053:
054: private boolean autoCommit;
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 the JDBC driver class name
067: * @param url the JDBC URL to use for accessing the DriverManager
068: * @param username the JDBC username to use for accessing the DriverManager
069: * @param password the JDBC password to use for accessing the DriverManager
070: * @param autoCommit the default autoCommit value that will be set to created connections
071: * @see java.sql.DriverManager#getConnection(String, String, String)
072: */
073: public DriverManagerDataSource(String driverClassName, String url,
074: String username, String password, boolean autoCommit) {
075: setDriverClassName(driverClassName);
076: setUrl(url);
077: setUsername(username);
078: setPassword(password);
079: setAutoCommit(autoCommit);
080: }
081:
082: /**
083: * Create a new DriverManagerDataSource with the given standard
084: * DriverManager parameters.
085: * <p/>
086: * Note, the autoCommit will default to <code>false</code>.
087: *
088: * @param url the JDBC URL to use for accessing the DriverManager
089: * @param username the JDBC username to use for accessing the DriverManager
090: * @param password the JDBC password to use for accessing the DriverManager
091: * @see java.sql.DriverManager#getConnection(String, String, String)
092: */
093: public DriverManagerDataSource(String url, String username,
094: String password) {
095: setUrl(url);
096: setUsername(username);
097: setPassword(password);
098: }
099:
100: /**
101: * Create a new DriverManagerDataSource with the given JDBC URL, not
102: * specifying a username or password for JDBC access.
103: *
104: * @param url the JDBC URL to use for accessing the DriverManager
105: * @see java.sql.DriverManager#getConnection(String)
106: */
107: public DriverManagerDataSource(String url) {
108: setUrl(url);
109: }
110:
111: /**
112: * Set the JDBC driver class name. This driver will get initialized on
113: * startup, registering itself with the JDK's DriverManager.
114: * <p/>
115: * Alternatively, consider initializing the JDBC driver yourself before
116: * instantiating this DataSource.
117: *
118: * @see Class#forName(String)
119: * @see java.sql.DriverManager#registerDriver(java.sql.Driver)
120: */
121: public void setDriverClassName(String driverClassName) {
122: this .driverClassName = driverClassName;
123: try {
124: Class.forName(this .driverClassName, true, Thread
125: .currentThread().getContextClassLoader());
126: } catch (ClassNotFoundException ex) {
127: throw new IllegalArgumentException(
128: "Could not load JDBC driver class ["
129: + this .driverClassName + "]");
130: }
131: }
132:
133: /**
134: * Return the JDBC driver class name, if any.
135: */
136: public String getDriverClassName() {
137: return driverClassName;
138: }
139:
140: /**
141: * Set the JDBC URL to use for accessing the DriverManager.
142: *
143: * @see java.sql.DriverManager#getConnection(String, String, String)
144: */
145: public void setUrl(String url) {
146: this .url = url;
147: }
148:
149: /**
150: * Return the JDBC URL to use for accessing the DriverManager.
151: */
152: public String getUrl() {
153: return url;
154: }
155:
156: /**
157: * Set the JDBC username to use for accessing the DriverManager.
158: *
159: * @see java.sql.DriverManager#getConnection(String, String, String)
160: */
161: public void setUsername(String username) {
162: this .username = username;
163: }
164:
165: /**
166: * Return the JDBC username to use for accessing the DriverManager.
167: */
168: public String getUsername() {
169: return username;
170: }
171:
172: /**
173: * Set the JDBC password to use for accessing the DriverManager.
174: *
175: * @see java.sql.DriverManager#getConnection(String, String, String)
176: */
177: public void setPassword(String password) {
178: this .password = password;
179: }
180:
181: /**
182: * Return the JDBC password to use for accessing the DriverManager.
183: */
184: public String getPassword() {
185: return password;
186: }
187:
188: /**
189: * Returns the auto commit setting that a connection will be created
190: */
191: public boolean getAutoCommit() {
192: return this .autoCommit;
193: }
194:
195: /**
196: * Sets the auto commit setting that a connection will be created
197: */
198: public void setAutoCommit(boolean autoCommit) {
199: this .autoCommit = autoCommit;
200: }
201:
202: /**
203: * This implementation delegates to
204: * <code>getConnectionFromDriverManager</code>, using the default
205: * username and password of this DataSource.
206: *
207: * @see #getConnectionFromDriverManager()
208: */
209: public Connection getConnection() throws SQLException {
210: return getConnectionFromDriverManager();
211: }
212:
213: /**
214: * This implementation delegates to
215: * <code>getConnectionFromDriverManager</code>, using the given username
216: * and password.
217: *
218: * @see #getConnectionFromDriverManager(String, String, String)
219: */
220: public Connection getConnection(String username, String password)
221: throws SQLException {
222: return getConnectionFromDriverManager(getUrl(), username,
223: password);
224: }
225:
226: /**
227: * Get a Connection from the DriverManager, using the default username and
228: * password of this DataSource.
229: *
230: * @see #getConnectionFromDriverManager(String, String, String)
231: */
232: protected Connection getConnectionFromDriverManager()
233: throws SQLException {
234: return getConnectionFromDriverManager(getUrl(), getUsername(),
235: getPassword());
236: }
237:
238: /**
239: * Getting a connection using the nasty static from DriverManager is
240: * extracted into a protected method to allow for easy unit testing.
241: * <p/>
242: * Note, that it sets the auto commit to false
243: *
244: * @see java.sql.DriverManager#getConnection(String, String, String)
245: */
246: protected Connection getConnectionFromDriverManager(String url,
247: String username, String password) throws SQLException {
248:
249: Connection conn = DriverManager.getConnection(url, username,
250: password);
251: if (conn.getAutoCommit() != autoCommit) {
252: conn.setAutoCommit(autoCommit);
253: }
254: return conn;
255: }
256:
257: }
|