001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.database;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.sql.DataSource;
030: import java.sql.Connection;
031: import java.sql.SQLException;
032:
033: /**
034: * This class adapts a JDBC <code>DataSource</code> to a
035: * {@link IDatabaseConnection}.
036: *
037: * @author Manuel Laflamme
038: * @version $Revision: 554 $
039: * @since Mar 8, 2002
040: */
041: public class DatabaseDataSourceConnection extends
042: AbstractDatabaseConnection implements IDatabaseConnection {
043:
044: /**
045: * Logger for this class
046: */
047: private static final Logger logger = LoggerFactory
048: .getLogger(DatabaseDataSourceConnection.class);
049:
050: private final String _schema;
051: private final DataSource _dataSource;
052: private final String _user;
053: private final String _password;
054: private Connection _connection;
055:
056: public DatabaseDataSourceConnection(InitialContext context,
057: String jndiName, String schema) throws NamingException,
058: SQLException {
059: this ((DataSource) context.lookup(jndiName), schema, null, null);
060: }
061:
062: public DatabaseDataSourceConnection(InitialContext context,
063: String jndiName, String schema, String user, String password)
064: throws NamingException, SQLException {
065: this ((DataSource) context.lookup(jndiName), schema, user,
066: password);
067: }
068:
069: public DatabaseDataSourceConnection(InitialContext context,
070: String jndiName) throws NamingException, SQLException {
071: this (context, jndiName, null);
072: }
073:
074: public DatabaseDataSourceConnection(InitialContext context,
075: String jndiName, String user, String password)
076: throws NamingException, SQLException {
077: this (context, jndiName, null, user, password);
078: }
079:
080: public DatabaseDataSourceConnection(DataSource dataSource)
081: throws SQLException {
082: this (dataSource, null, null, null);
083: }
084:
085: public DatabaseDataSourceConnection(DataSource dataSource,
086: String user, String password) throws SQLException {
087: this (dataSource, null, user, password);
088: }
089:
090: public DatabaseDataSourceConnection(DataSource dataSource,
091: String schema) throws SQLException {
092: this (dataSource, schema, null, null);
093: }
094:
095: public DatabaseDataSourceConnection(DataSource dataSource,
096: String schema, String user, String password)
097: throws SQLException {
098: _dataSource = dataSource;
099: _schema = schema;
100: _user = user;
101: _password = password;
102: }
103:
104: ////////////////////////////////////////////////////////////////////////////
105: // IDatabaseConnection interface
106:
107: public Connection getConnection() throws SQLException {
108: logger.debug("getConnection() - start");
109:
110: if (_connection == null) {
111: if (_user != null) {
112: _connection = _dataSource.getConnection(_user,
113: _password);
114: } else {
115: _connection = _dataSource.getConnection();
116: }
117: }
118: return _connection;
119: }
120:
121: public String getSchema() {
122: logger.debug("getSchema() - start");
123:
124: return _schema;
125: }
126:
127: public void close() throws SQLException {
128: logger.debug("close() - start");
129:
130: if (_connection != null) {
131: _connection.close();
132: _connection = null;
133: }
134: }
135: }
|