What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/* Copyright (c) 2001-2004, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hsqldb.jdbc;
import java.io.Serializable;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.Properties;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;
import javax.sql.DataSource;
import org.hsqldb.jdbcDriver;
// boucherb@users 20040411 - doc 1.7.2 - javadoc updates toward 1.7.2 final
/**
* A factory for connections to the physical data source that this
* DataSource object represents. An alternative to the
* DriverManager facility, a DataSource object
* is the preferred means of getting a connection. An object that implements
* the DataSource interface will typically be
* registered with a naming service based on the
* JavaTM Naming and Directory (JNDI) API.
*
* The DataSource interface is implemented by a driver vendor.
* There are three types of implementations:
*
* - Basic implementation -- produces a standard
Connection
* object
* - Connection pooling implementation -- produces a
Connection
* object that will automatically participate in connection pooling. This
* implementation works with a middle-tier connection pooling manager.
* - Distributed transaction implementation -- produces a
*
Connection object that may be used for distributed
* transactions and almost always participates in connection pooling.
* This implementation works with a middle-tier
* transaction manager and almost always with a connection
* pooling manager.
*
*
* A DataSource object has properties that can be modified
* when necessary. For example, if the data source is moved to a different
* server, the property for the server can be changed. The benefit is that
* because the data source's properties can be changed, any code accessing
* that data source does not need to be changed.
*
* A driver that is accessed via a DataSource object does not
* register itself with the DriverManager. Rather, a
* DataSource object is retrieved though a lookup operation
* and then used to create a Connection object. With a basic
* implementation, the connection obtained through a DataSource
* object is identical to a connection obtained through the
* DriverManager facility.
*
* @since JDK 1.4
* @author deforest@users
* @version 1.7.2
*/
public class jdbcDataSource
implements Serializable, Referenceable, DataSource {
/**
* Login timeout
*/
private int loginTimeout = 0;
/**
* Log writer
*/
private transient PrintWriter logWriter;
/**
* Default password to use for connections
*/
private String password = "";
/**
* Default user to use for connections
*/
private String user = "";
/**
* Database location
*/
private String database = "";
/**
* Constructor
*/
public jdbcDataSource() {}
/**
* Attempts to establish a connection with the data source that
* this DataSource object represents.
*
* @return a connection to the data source
* @exception SQLException if a database access error occurs
*/
public Connection getConnection() throws java.sql.SQLException {
return getConnection(user, password);
}
/**
* Attempts to establish a connection with the data source that
* this DataSource object represents.
*
* @param username the database user on whose behalf the connection is
* being made
* @param password the user's password
* @return a connection to the data source
* @exception SQLException if a database access error occurs
*/
public Connection getConnection(String username,
String password) throws SQLException {
Properties props = new Properties();
if (username != null) {
props.put("user", username);
}
if (password != null) {
props.put("password", password);
}
return jdbcDriver.getConnection(database, props);
}
/**
* Retrieves the jdbc database connection url attribute.
*
* @return the jdbc database connection url attribute
*/
public String getDatabase() {
return database;
}
/**
* Gets the maximum time in seconds that this data source can wait
* while attempting to connect to a database. A value of zero
* means that the timeout is the default system timeout
* if there is one; otherwise, it means that there is no timeout.
* When a DataSource object is created, the login timeout is
* initially zero.
*
* @return the data source login time limit
* @exception SQLException if a database access error occurs.
* @see #setLoginTimeout
*/
public int getLoginTimeout() throws java.sql.SQLException {
return 0;
}
/**
* Retrieves the log writer for this DataSource
* object.
*
* The log writer is a character output stream to which all logging
* and tracing messages for this data source will be
* printed. This includes messages printed by the methods of this
* object, messages printed by methods of other objects manufactured
* by this object, and so on. Messages printed to a data source
* specific log writer are not printed to the log writer associated
* with the java.sql.Drivermanager class. When a
* DataSource object is
* created, the log writer is initially null; in other words, the
* default is for logging to be disabled.
*
* @return the log writer for this data source or null if
* logging is disabled
* @exception SQLException if a database access error occurs
* @see #setLogWriter
*/
public java.io.PrintWriter getLogWriter() throws java.sql.SQLException {
return logWriter;
}
// javadoc to be copied from javax.naming.Referenceable.getReference()
public Reference getReference() throws NamingException {
String cname = "org.hsqldb.jdbc.jdbcDataSourceFactory";
Reference ref = new Reference(getClass().getName(), cname, null);
ref.add(new StringRefAddr("database", getDatabase()));
ref.add(new StringRefAddr("user", getUser()));
ref.add(new StringRefAddr("password", password));
return ref;
}
/**
* Retrieves the user ID for the connection.
*
* @return the user ID for the connection
*/
public String getUser() {
return user;
}
/**
* Assigns the value of this object's jdbc database connection
* url attribute.
*
* @param database the new value of this object's jdbc database connection
* url attribute
*/
public void setDatabase(String database) {
this.database = database;
}
/**
* Sets the maximum time in seconds that this data source will wait
* while attempting to connect to a database. A value of zero
* specifies that the timeout is the default system timeout
* if there is one; otherwise, it specifies that there is no timeout.
* When a DataSource object is created, the login timeout is
* initially zero.
*
* @param seconds the data source login time limit
* @exception SQLException if a database access error occurs.
* @see #getLoginTimeout
*/
public void setLoginTimeout(int seconds) throws java.sql.SQLException {
loginTimeout = 0;
}
/**
* Sets the log writer for this DataSource
* object to the given java.io.PrintWriter object.
*
* The log writer is a character output stream to which all logging
* and tracing messages for this data source will be
* printed. This includes messages printed by the methods of this
* object, messages printed by methods of other objects manufactured
* by this object, and so on. Messages printed to a data source-
* specific log writer are not printed to the log writer associated
* with the java.sql.Drivermanager class. When a
* DataSource object is created the log writer is
* initially null; in other words, the default is for logging to be
* disabled.
*
* @param logWriter the new log writer; to disable logging, set to null
* @exception SQLException if a database access error occurs
* @see #getLogWriter
*/
public void setLogWriter(PrintWriter logWriter)
throws java.sql.SQLException {
this.logWriter = logWriter;
}
/**
* Sets the password to use for connecting to the database
* @param password the password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Sets the userid
* @param user the user id
*/
public void setUser(String user) {
this.user = user;
}
}
|