KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > DatabaseDataSourceConnection


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 javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.sql.DataSource JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 /**
31  * This class adapts a JDBC <code>DataSource</code> to a
32  * {@link IDatabaseConnection}.
33  *
34  * @author Manuel Laflamme
35  * @version $Revision: 1.10 $
36  * @since Mar 8, 2002
37  */

38 public class DatabaseDataSourceConnection extends AbstractDatabaseConnection
39         implements IDatabaseConnection
40 {
41     private final String JavaDoc _schema;
42     private final DataSource JavaDoc _dataSource;
43     private final String JavaDoc _user;
44     private final String JavaDoc _password;
45     private Connection JavaDoc _connection;
46
47     public DatabaseDataSourceConnection(InitialContext JavaDoc context, String JavaDoc jndiName,
48             String JavaDoc schema) throws NamingException JavaDoc, SQLException JavaDoc
49     {
50         this((DataSource JavaDoc)context.lookup(jndiName), schema, null, null);
51     }
52
53     public DatabaseDataSourceConnection(InitialContext JavaDoc context, String JavaDoc jndiName,
54             String JavaDoc schema, String JavaDoc user, String JavaDoc password)
55             throws NamingException JavaDoc, SQLException JavaDoc
56     {
57         this((DataSource JavaDoc)context.lookup(jndiName), schema, user, password);
58     }
59
60     public DatabaseDataSourceConnection(InitialContext JavaDoc context, String JavaDoc jndiName)
61             throws NamingException JavaDoc, SQLException JavaDoc
62     {
63         this(context, jndiName, null);
64     }
65
66     public DatabaseDataSourceConnection(InitialContext JavaDoc context, String JavaDoc jndiName,
67             String JavaDoc user, String JavaDoc password) throws NamingException JavaDoc, SQLException JavaDoc
68     {
69         this(context, jndiName, null, user, password);
70     }
71
72     public DatabaseDataSourceConnection(DataSource JavaDoc dataSource)
73             throws SQLException JavaDoc
74     {
75         this(dataSource, null, null, null);
76     }
77
78     public DatabaseDataSourceConnection(DataSource JavaDoc dataSource, String JavaDoc user,
79             String JavaDoc password) throws SQLException JavaDoc
80     {
81         this(dataSource, null, user, password);
82     }
83
84     public DatabaseDataSourceConnection(DataSource JavaDoc dataSource, String JavaDoc schema)
85             throws SQLException JavaDoc
86     {
87         this(dataSource, schema, null, null);
88     }
89
90     public DatabaseDataSourceConnection(DataSource JavaDoc dataSource, String JavaDoc schema,
91             String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc
92     {
93         _dataSource = dataSource;
94         _schema = schema;
95         _user = user;
96         _password = password;
97     }
98
99     ////////////////////////////////////////////////////////////////////////////
100
// IDatabaseConnection interface
101

102     public Connection JavaDoc getConnection() throws SQLException JavaDoc
103     {
104         if (_connection == null)
105         {
106             if (_user != null)
107             {
108                 _connection = _dataSource.getConnection(_user, _password);
109             }
110             else
111             {
112                 _connection = _dataSource.getConnection();
113             }
114         }
115         return _connection;
116     }
117
118     public String JavaDoc getSchema()
119     {
120         return _schema;
121     }
122
123     public void close() throws SQLException JavaDoc
124     {
125         if (_connection != null)
126         {
127             _connection.close();
128             _connection = null;
129         }
130     }
131 }
132
133
134
135
136
137
138
139
Popular Tags