SafeJDBC v2.00

com.safejdbc.javax.sql
Class SafeDataSource

java.lang.Object
  extended by com.safejdbc.javax.sql.SafeDataSource
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource

public class SafeDataSource
extends Object
implements DataSource

Implementation of DataSource that allows the wrapping of native "clear" DataSource that references an application database.

Notes :

See Also:
SafeDataSourceFactory

Method Summary
 Connection getConnection()
          Returns a Cipher Connection to the database.
 Connection getConnection(String username, String password)
          Returns a Cipher Connection to the database.
static SafeDataSource getInstance(DataSource dsApplication, DataSource dsSafeJdbcCatalog)
          Creates and configures a SafeDataSource instance that wraps an application DataSource.
 int getLoginTimeout()
          Returns the login timeout (in seconds) for connecting to the database.
 PrintWriter getLogWriter()
          Returns the log writer being used by this data source.
 boolean isWrapperFor(Class<?> iface)
          Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.
 void setLoginTimeout(int loginTimeout)
          Sets the login timeout (in seconds) for connecting to the database.
 void setLogWriter(PrintWriter logWriter)
          Sets the log writer being used by this data source.
<T> T
unwrap(Class<T> iface)
          Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static SafeDataSource getInstance(DataSource dsApplication,
                                         DataSource dsSafeJdbcCatalog)
Creates and configures a SafeDataSource instance that wraps an application DataSource.

This method provides the ability to benefit from connection pooling mechanisms implemented by the wrapped Driver.

getConnection() will return a Cipher Connection that wraps an application Connection extracted from the connection pool.

Example:

InitialContext initCtx = new InitialContext();

// Get a DataSource for connection to the application database
DataSource dsApplication
= (DataSource) initCtx.lookup("java:/comp/env/jdbc/application");

// Get a DataSource for connection to the SafeJdbc Catalog database
DataSource dsSafejdbcCat
= (DataSource) initCtx.lookup("java:/comp/env/jdbc/safejdbc_catalog");

// This will wrap the two previous DataSources
DataSource dsSafe = SafeDataSource.getInstance(dsApplication, dsSafejdbcCat);

// Ok, create a Cipher Connection.
// If native wrapped driver implements connection pooling,
// this will get a wrapped Connection from the pool
Connection conn = dsSafe.getConnection();

if (conn instanceof CipherConnection)
{
    System.out.println("Good! Connection obtained from DataSource is a Cipher Connection!");
}

Parameters:
dsApplication - the DataSource that defines the application database to be wrapped
dsSafeJdbcCatalog - the DataSource that defines the SafeJdbc Catalog database

getConnection

public Connection getConnection()
                         throws SQLException
Returns a Cipher Connection to the database.

Specified by:
getConnection in interface DataSource
Throws:
SQLException - if a database access error occurs

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
Returns a Cipher Connection to the database.

Specified by:
getConnection in interface DataSource
Parameters:
username - Database user on whose behalf the Connection is being made
password - The database user's password
Throws:
SQLException - if a database access error occurs

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Returns the login timeout (in seconds) for connecting to the database.

Specified by:
getLoginTimeout in interface CommonDataSource
Throws:
SQLException - if a database access error occurs

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException
Returns the log writer being used by this data source.

Specified by:
getLogWriter in interface CommonDataSource
Throws:
SQLException - if a database access error occurs

setLoginTimeout

public void setLoginTimeout(int loginTimeout)
                     throws SQLException
Sets the login timeout (in seconds) for connecting to the database.

Specified by:
setLoginTimeout in interface CommonDataSource
Parameters:
loginTimeout - The new login timeout, or zero for no timeout
Throws:
SQLException - if a database access error occurs

setLogWriter

public void setLogWriter(PrintWriter logWriter)
                  throws SQLException
Sets the log writer being used by this data source.

Specified by:
setLogWriter in interface CommonDataSource
Parameters:
logWriter - The new log writer
Throws:
SQLException - if a database access error occurs

unwrap

public <T> T unwrap(Class<T> iface)
         throws SQLException
Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy. If the receiver implements the interface then the result is the receiver or a proxy for the receiver. If the receiver is a wrapper and the wrapped object implements the interface then the result is the wrapped object or a proxy for the wrapped object. Otherwise return the the result of calling unwrap recursively on the wrapped object or a proxy for that result. If the receiver is not a wrapper and does not implement the interface, then an SQLException is thrown.

Specified by:
unwrap in interface Wrapper
Parameters:
iface - A Class defining an interface that the result must implement.
Returns:
an object that implements the interface. May be a proxy for the actual implementing object.
Throws:
SQLException - If no object found that implements the interface
Since:
1.6

isWrapperFor

public boolean isWrapperFor(Class<?> iface)
                     throws SQLException
Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does. Returns false otherwise. If this implements the interface then return true, else if this is a wrapper then return the result of recursively calling isWrapperFor on the wrapped object. If this does not implement the interface and is not a wrapper, return false. This method should be implemented as a low-cost operation compared to unwrap so that callers can use this method to avoid expensive unwrap calls that may fail. If this method returns true then calling unwrap with the same argument should succeed.

Specified by:
isWrapperFor in interface Wrapper
Parameters:
iface - a Class defining an interface.
Returns:
true if this implements the interface or directly or indirectly wraps an object that does.
Throws:
SQLException - if an error occurs while determining whether this is a wrapper for an object with the given interface.
Since:
1.6

SafeJDBC v2.00

Copyright © SafeLogic 2011