View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.database;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import javax.naming.InitialContext;
28  import javax.naming.NamingException;
29  import javax.sql.DataSource;
30  import java.sql.Connection;
31  import java.sql.SQLException;
32  
33  /**
34   * This class adapts a JDBC <code>DataSource</code> to a
35   * {@link IDatabaseConnection}.
36   *
37   * @author Manuel Laflamme
38   * @version $Revision$
39   * @since Mar 8, 2002
40   */
41  public class DatabaseDataSourceConnection extends AbstractDatabaseConnection
42          implements IDatabaseConnection
43  {
44  
45      /**
46       * Logger for this class
47       */
48      private static final Logger logger = LoggerFactory.getLogger(DatabaseDataSourceConnection.class);
49  
50      private final String _schema;
51      private final DataSource _dataSource;
52      private final String _user;
53      private final String _password;
54      private Connection _connection;
55  
56      public DatabaseDataSourceConnection(InitialContext context, String jndiName,
57              String schema) throws NamingException, SQLException
58      {
59          this((DataSource)context.lookup(jndiName), schema, null, null);
60      }
61  
62      public DatabaseDataSourceConnection(InitialContext context, String jndiName,
63              String schema, String user, String password)
64              throws NamingException, SQLException
65      {
66          this((DataSource)context.lookup(jndiName), schema, user, password);
67      }
68  
69      public DatabaseDataSourceConnection(InitialContext context, String jndiName)
70              throws NamingException, SQLException
71      {
72          this(context, jndiName, null);
73      }
74  
75      public DatabaseDataSourceConnection(InitialContext context, String jndiName,
76              String user, String password) throws NamingException, SQLException
77      {
78          this(context, jndiName, null, user, password);
79      }
80  
81      public DatabaseDataSourceConnection(DataSource dataSource)
82              throws SQLException
83      {
84          this(dataSource, null, null, null);
85      }
86  
87      public DatabaseDataSourceConnection(DataSource dataSource, String user,
88              String password) throws SQLException
89      {
90          this(dataSource, null, user, password);
91      }
92  
93      public DatabaseDataSourceConnection(DataSource dataSource, String schema)
94              throws SQLException
95      {
96          this(dataSource, schema, null, null);
97      }
98  
99      public DatabaseDataSourceConnection(DataSource dataSource, String schema,
100             String user, String password) throws SQLException
101     {
102         _dataSource = dataSource;
103         _schema = schema;
104         _user = user;
105         _password = password;
106     }
107 
108     ////////////////////////////////////////////////////////////////////////////
109     // IDatabaseConnection interface
110 
111     public Connection getConnection() throws SQLException
112     {
113         logger.debug("getConnection() - start");
114 
115         if (_connection == null)
116         {
117             try
118             {
119                 if (_user != null) {
120                     _connection = _dataSource.getConnection(_user, _password);
121                 } else {
122                     _connection = _dataSource.getConnection();
123                 }
124             }
125             catch (SQLException e)
126             {
127                 logger.error("getConnection(): ", e);
128                 throw e;
129             }
130         }
131         return _connection;
132     }
133 
134     public String getSchema()
135     {
136         return _schema;
137     }
138 
139     public void close() throws SQLException
140     {
141         logger.debug("close() - start");
142 
143         if (_connection != null)
144         {
145             _connection.close();
146             _connection = null;
147         }
148     }
149 }