KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > jdbc > jdbcDataSource


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.jdbc;
33
34 import java.io.PrintWriter JavaDoc;
35 import java.io.Serializable JavaDoc;
36 import java.sql.Connection JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 import javax.naming.NamingException JavaDoc;
41 import javax.naming.Reference JavaDoc;
42 import javax.naming.Referenceable JavaDoc;
43 import javax.naming.StringRefAddr JavaDoc;
44 import javax.sql.DataSource JavaDoc;
45
46 import org.hsqldb.jdbcDriver;
47
48 // boucherb@users 20040411 - doc 1.7.2 - javadoc updates toward 1.7.2 final
49

50 /**
51  * <p>A factory for connections to the physical data source that this
52  * <code>DataSource</code> object represents. An alternative to the
53  * <code>DriverManager</code> facility, a <code>DataSource</code> object
54  * is the preferred means of getting a connection. An object that implements
55  * the <code>DataSource</code> interface will typically be
56  * registered with a naming service based on the
57  * Java<sup><font size=-2>TM</font></sup> Naming and Directory (JNDI) API.
58  * <P>
59  * The <code>DataSource</code> interface is implemented by a driver vendor.
60  * There are three types of implementations:
61  * <OL>
62  * <LI>Basic implementation -- produces a standard <code>Connection</code>
63  * object
64  * <LI>Connection pooling implementation -- produces a <code>Connection</code>
65  * object that will automatically participate in connection pooling. This
66  * implementation works with a middle-tier connection pooling manager.
67  * <LI>Distributed transaction implementation -- produces a
68  * <code>Connection</code> object that may be used for distributed
69  * transactions and almost always participates in connection pooling.
70  * This implementation works with a middle-tier
71  * transaction manager and almost always with a connection
72  * pooling manager.
73  * </OL>
74  * <P>
75  * A <code>DataSource</code> object has properties that can be modified
76  * when necessary. For example, if the data source is moved to a different
77  * server, the property for the server can be changed. The benefit is that
78  * because the data source's properties can be changed, any code accessing
79  * that data source does not need to be changed.
80  * <P>
81  * A driver that is accessed via a <code>DataSource</code> object does not
82  * register itself with the <code>DriverManager</code>. Rather, a
83  * <code>DataSource</code> object is retrieved though a lookup operation
84  * and then used to create a <code>Connection</code> object. With a basic
85  * implementation, the connection obtained through a <code>DataSource</code>
86  * object is identical to a connection obtained through the
87  * <code>DriverManager</code> facility.
88  *
89  * @since JDK 1.4
90
91  * @author deforest@users
92  * @version 1.7.2
93  */

94 public class jdbcDataSource
95 implements Serializable JavaDoc, Referenceable JavaDoc, DataSource JavaDoc {
96
97     /**
98      * Login timeout
99      */

100     private int loginTimeout = 0;
101
102     /**
103      * Log writer
104      */

105     private transient PrintWriter JavaDoc logWriter;
106
107     /**
108      * Default password to use for connections
109      */

110     private String JavaDoc password = "";
111
112     /**
113      * Default user to use for connections
114      */

115     private String JavaDoc user = "";
116
117     /**
118      * Database location
119      */

120     private String JavaDoc database = "";
121
122     /**
123      * Constructor
124      */

125     public jdbcDataSource() {}
126
127     /**
128      * <p>Attempts to establish a connection with the data source that
129      * this <code>DataSource</code> object represents.
130      *
131      * @return a connection to the data source
132      * @exception SQLException if a database access error occurs
133      */

134     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
135         return getConnection(user, password);
136     }
137
138     /**
139      * <p>Attempts to establish a connection with the data source that
140      * this <code>DataSource</code> object represents.
141      *
142      * @param username the database user on whose behalf the connection is
143      * being made
144      * @param password the user's password
145      * @return a connection to the data source
146      * @exception SQLException if a database access error occurs
147      */

148     public Connection JavaDoc getConnection(String JavaDoc username,
149                                     String JavaDoc password) throws SQLException JavaDoc {
150
151         Properties JavaDoc props = new Properties JavaDoc();
152
153         if (username != null) {
154             props.put("user", username);
155         }
156
157         if (password != null) {
158             props.put("password", password);
159         }
160
161         return jdbcDriver.getConnection(database, props);
162     }
163
164     /**
165      * Retrieves the jdbc database connection url attribute. <p>
166      *
167      * @return the jdbc database connection url attribute
168      */

169     public String JavaDoc getDatabase() {
170         return database;
171     }
172
173     /**
174      * Gets the maximum time in seconds that this data source can wait
175      * while attempting to connect to a database. A value of zero
176      * means that the timeout is the default system timeout
177      * if there is one; otherwise, it means that there is no timeout.
178      * When a <code>DataSource</code> object is created, the login timeout is
179      * initially zero.
180      *
181      * @return the data source login time limit
182      * @exception SQLException if a database access error occurs.
183      * @see #setLoginTimeout
184      */

185     public int getLoginTimeout() throws SQLException JavaDoc {
186         return 0;
187     }
188
189     /**
190      * <p>Retrieves the log writer for this <code>DataSource</code>
191      * object.
192      *
193      * <p>The log writer is a character output stream to which all logging
194      * and tracing messages for this data source will be
195      * printed. This includes messages printed by the methods of this
196      * object, messages printed by methods of other objects manufactured
197      * by this object, and so on. Messages printed to a data source
198      * specific log writer are not printed to the log writer associated
199      * with the <code>java.sql.Drivermanager</code> class. When a
200      * <code>DataSource</code> object is
201      * created, the log writer is initially null; in other words, the
202      * default is for logging to be disabled.
203      *
204      * @return the log writer for this data source or null if
205      * logging is disabled
206      * @exception SQLException if a database access error occurs
207      * @see #setLogWriter
208      */

209     public java.io.PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
210         return logWriter;
211     }
212
213     // javadoc to be copied from javax.naming.Referenceable.getReference()
214
public Reference JavaDoc getReference() throws NamingException JavaDoc {
215
216         String JavaDoc cname = "org.hsqldb.jdbc.jdbcDataSourceFactory";
217         Reference JavaDoc ref = new Reference JavaDoc(getClass().getName(), cname, null);
218
219         ref.add(new StringRefAddr JavaDoc("database", getDatabase()));
220         ref.add(new StringRefAddr JavaDoc("user", getUser()));
221         ref.add(new StringRefAddr JavaDoc("password", password));
222
223         return ref;
224     }
225
226     /**
227      * Retrieves the user ID for the connection. <p>
228      *
229      * @return the user ID for the connection
230      */

231     public String JavaDoc getUser() {
232         return user;
233     }
234
235     /**
236      * Assigns the value of this object's jdbc database connection
237      * url attribute. <p>
238      *
239      * @param database the new value of this object's jdbc database connection
240      * url attribute
241      */

242     public void setDatabase(String JavaDoc database) {
243         this.database = database;
244     }
245
246     /**
247      * <p>Sets the maximum time in seconds that this data source will wait
248      * while attempting to connect to a database. A value of zero
249      * specifies that the timeout is the default system timeout
250      * if there is one; otherwise, it specifies that there is no timeout.
251      * When a <code>DataSource</code> object is created, the login timeout is
252      * initially zero.
253      *
254      * @param seconds the data source login time limit
255      * @exception SQLException if a database access error occurs.
256      * @see #getLoginTimeout
257      */

258     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
259         loginTimeout = 0;
260     }
261
262     /**
263      * <p>Sets the log writer for this <code>DataSource</code>
264      * object to the given <code>java.io.PrintWriter</code> object.
265      *
266      * <p>The log writer is a character output stream to which all logging
267      * and tracing messages for this data source will be
268      * printed. This includes messages printed by the methods of this
269      * object, messages printed by methods of other objects manufactured
270      * by this object, and so on. Messages printed to a data source-
271      * specific log writer are not printed to the log writer associated
272      * with the <code>java.sql.Drivermanager</code> class. When a
273      * <code>DataSource</code> object is created the log writer is
274      * initially null; in other words, the default is for logging to be
275      * disabled.
276      *
277      * @param logWriter the new log writer; to disable logging, set to null
278      * @exception SQLException if a database access error occurs
279      * @see #getLogWriter
280      */

281     public void setLogWriter(PrintWriter JavaDoc logWriter) throws SQLException JavaDoc {
282         this.logWriter = logWriter;
283     }
284
285     /**
286      * Sets the password to use for connecting to the database
287      * @param password the password
288      */

289     public void setPassword(String JavaDoc password) {
290         this.password = password;
291     }
292
293     /**
294      * Sets the userid
295      * @param user the user id
296      */

297     public void setUser(String JavaDoc user) {
298         this.user = user;
299     }
300 }
301
Popular Tags