001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.jdbc;
032:
033: import java.io.PrintWriter;
034: import java.io.Serializable;
035: import java.sql.Connection;
036: import java.sql.SQLException;
037: import java.util.Properties;
038:
039: import javax.naming.NamingException;
040: import javax.naming.Reference;
041: import javax.naming.Referenceable;
042: import javax.naming.StringRefAddr;
043: import javax.sql.DataSource;
044:
045: import org.hsqldb.jdbcDriver;
046:
047: // boucherb@users 20040411 - doc 1.7.2 - javadoc updates toward 1.7.2 final
048:
049: /**
050: * <p>A factory for connections to the physical data source that this
051: * <code>DataSource</code> object represents. An alternative to the
052: * <code>DriverManager</code> facility, a <code>DataSource</code> object
053: * is the preferred means of getting a connection. An object that implements
054: * the <code>DataSource</code> interface will typically be
055: * registered with a naming service based on the
056: * Java<sup><font size=-2>TM</font></sup> Naming and Directory (JNDI) API.
057: * <P>
058: * The <code>DataSource</code> interface is implemented by a driver vendor.
059: * There are three types of implementations:
060: * <OL>
061: * <LI>Basic implementation -- produces a standard <code>Connection</code>
062: * object
063: * <LI>Connection pooling implementation -- produces a <code>Connection</code>
064: * object that will automatically participate in connection pooling. This
065: * implementation works with a middle-tier connection pooling manager.
066: * <LI>Distributed transaction implementation -- produces a
067: * <code>Connection</code> object that may be used for distributed
068: * transactions and almost always participates in connection pooling.
069: * This implementation works with a middle-tier
070: * transaction manager and almost always with a connection
071: * pooling manager.
072: * </OL>
073: * <P>
074: * A <code>DataSource</code> object has properties that can be modified
075: * when necessary. For example, if the data source is moved to a different
076: * server, the property for the server can be changed. The benefit is that
077: * because the data source's properties can be changed, any code accessing
078: * that data source does not need to be changed.
079: * <P>
080: * A driver that is accessed via a <code>DataSource</code> object does not
081: * register itself with the <code>DriverManager</code>. Rather, a
082: * <code>DataSource</code> object is retrieved though a lookup operation
083: * and then used to create a <code>Connection</code> object. With a basic
084: * implementation, the connection obtained through a <code>DataSource</code>
085: * object is identical to a connection obtained through the
086: * <code>DriverManager</code> facility.
087: *
088: * @since JDK 1.4
089:
090: * @author deforest@users
091: * @version 1.7.2
092: */
093: public class jdbcDataSource implements Serializable, Referenceable,
094: DataSource {
095:
096: /**
097: * Login timeout
098: */
099: private int loginTimeout = 0;
100:
101: /**
102: * Log writer
103: */
104: private transient PrintWriter logWriter;
105:
106: /**
107: * Default password to use for connections
108: */
109: private String password = "";
110:
111: /**
112: * Default user to use for connections
113: */
114: private String user = "";
115:
116: /**
117: * Database location
118: */
119: private String database = "";
120:
121: /**
122: * Constructor
123: */
124: public jdbcDataSource() {
125: }
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 getConnection() throws SQLException {
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 getConnection(String username, String password)
149: throws SQLException {
150:
151: Properties props = new Properties();
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 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 {
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 getLogWriter() throws SQLException {
210: return logWriter;
211: }
212:
213: // javadoc to be copied from javax.naming.Referenceable.getReference()
214: public Reference getReference() throws NamingException {
215:
216: String cname = "org.hsqldb.jdbc.jdbcDataSourceFactory";
217: Reference ref = new Reference(getClass().getName(), cname, null);
218:
219: ref.add(new StringRefAddr("database", getDatabase()));
220: ref.add(new StringRefAddr("user", getUser()));
221: ref.add(new StringRefAddr("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 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 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 {
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 logWriter) throws SQLException {
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 password) {
290: this .password = password;
291: }
292:
293: /**
294: * Sets the userid
295: * @param user the user id
296: */
297: public void setUser(String user) {
298: this.user = user;
299: }
300: }
|