org.sape.carbon.services.sql.connection
Class NamedDataSourceConnectionFactory

java.lang.Object
  |
  +--org.sape.carbon.services.sql.connection.NamedDataSourceConnectionFactory
All Implemented Interfaces:
Configurable, ConnectionFactory, javax.sql.DataSource, DataSourceConnectionFactory, FunctionalInterface

public class NamedDataSourceConnectionFactory
extends Object
implements DataSourceConnectionFactory, Configurable

Decorator Component for a JNDI DataSource. This component should be used as a javax.sql.DataSource. On configuration, obtains a JDBC DataSource from JNDI and delegates requests for connections to it. Note that the DataSource instance is discarded and re-fetched each time this component is configured. Copyright 2002 Sapient

Since:
carbon 1.0
Version:
$Revision: 1.12 $($Author: dvoet $ / $Date: 2003/05/05 21:21:36 $)
Author:
Chris Herron, March 2002

Field Summary
protected  javax.sql.DataSource dataSource
          Stores a reference to the data source retrieve from JNDI.
protected  String dataSourcePassword
          Stores the (optional) password used to create the connection from the managed DataSource object.
protected  String dataSourceUserId
          Stores the (optional) userid used to create the connection from the managed DataSource object.
private  org.apache.commons.logging.Log log
          Provides a handle to Apache-commons logger
 
Constructor Summary
NamedDataSourceConnectionFactory()
           
 
Method Summary
 void configure(ComponentConfiguration configuration)
          Obtains the DataSource and necessary connection properties.
protected  Context fetchInitialContext(String initialContextFactory, String providerUrl, String jndiUserId, String jndiPassword)
          Helper method that creates the InitialContext onto the JNDI tree based upon the configured properties.
 Connection getConnection()
          Delegates to the underlying DataSource to obtain a JDBC connection.
 Connection getConnection(String overridingDataSourceUserId, String overridingDataSourcePassword)
          Delegates to the underlying DataSource to obtain a JDBC connection.
 int getLoginTimeout()
          Delegates to DataSource.getLoginTimeout().
 PrintWriter getLogWriter()
          Delegates to DataSource.getLogWriter().
 void setLoginTimeout(int param)
          Delegates to DataSource.setLoginTimeout(int).
 void setLogWriter(PrintWriter printWriter)
          Delegates to DataSource.setLogWriter(java.io.PrintWriter).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dataSource

protected javax.sql.DataSource dataSource

Stores a reference to the data source retrieve from JNDI. Prevents further calls to JNDI, thus improving performance on acquiring connections.


dataSourceUserId

protected String dataSourceUserId

Stores the (optional) userid used to create the connection from the managed DataSource object.


dataSourcePassword

protected String dataSourcePassword

Stores the (optional) password used to create the connection from the managed DataSource object.


log

private org.apache.commons.logging.Log log
Provides a handle to Apache-commons logger

Constructor Detail

NamedDataSourceConnectionFactory

public NamedDataSourceConnectionFactory()
Method Detail

getConnection

public Connection getConnection()
                         throws SQLException
Delegates to the underlying DataSource to obtain a JDBC connection. If there is a configured dataSourceUserId, uses this.getConnection(dataSourceUserId, dataSourcePassword) to to obtain a JDBC connection. If no dataSourceUserId is configured, uses DataSource.getConnection().

Specified by:
getConnection in interface ConnectionFactory
Returns:
Connection A JDBC Connection
Throws:
SQLException - When obtaining connection fails

getConnection

public Connection getConnection(String overridingDataSourceUserId,
                                String overridingDataSourcePassword)
                         throws SQLException
Delegates to the underlying DataSource to obtain a JDBC connection. Overrides the dataSourceUserId and dataSourcePassword, if configured.

Specified by:
getConnection in interface javax.sql.DataSource
Parameters:
overridingDataSourceUserId - The overriding dataSourceUserId
overridingDataSourcePassword - The overriding dataSourcePassword
Returns:
Connection A JDBC Connection
Throws:
SQLException - When obtaining connection fails

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException
Delegates to DataSource.getLogWriter().

Specified by:
getLogWriter in interface javax.sql.DataSource
SQLException
See Also:
DataSource.getLogWriter()

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Delegates to DataSource.getLoginTimeout().

Specified by:
getLoginTimeout in interface javax.sql.DataSource
SQLException
See Also:
DataSource.getLoginTimeout()

setLogWriter

public void setLogWriter(PrintWriter printWriter)
                  throws SQLException
Delegates to DataSource.setLogWriter(java.io.PrintWriter).

Specified by:
setLogWriter in interface javax.sql.DataSource
SQLException
See Also:
DataSource.setLogWriter(java.io.PrintWriter)

setLoginTimeout

public void setLoginTimeout(int param)
                     throws SQLException
Delegates to DataSource.setLoginTimeout(int).

Specified by:
setLoginTimeout in interface javax.sql.DataSource
SQLException
See Also:
DataSource.setLoginTimeout(int)

configure

public void configure(ComponentConfiguration configuration)
Obtains the DataSource and necessary connection properties. Note that the DataSource is replaced each time the component is configured.

Specified by:
configure in interface Configurable
Parameters:
configuration - An instance of NamedDataSourceConnectionFactoryConfiguration

fetchInitialContext

protected Context fetchInitialContext(String initialContextFactory,
                                      String providerUrl,
                                      String jndiUserId,
                                      String jndiPassword)
                               throws NamingException

Helper method that creates the InitialContext onto the JNDI tree based upon the configured properties.

Parameters:
initialContextFactory - String representation of the class name to be used as the InitialContextFactory; this parameter is analagous to Context.INITIAL_CONTEXT_FACTORY
providerUrl - The JNDI provider URL as a Stringl this parameter is analagous to Context.PROVIDER_URL
jndiUserId - The security principal as a String; this parameter is analagous to Context.SECURITY_PRINCIPAL
jndiPassword - The credentials associated with the security principal as a String; this parameter is analagous to the Context.SECURITY_CREDENTIALS
Returns:
a new Context based on the given environment.
Throws:
NamingException - indicates an error building the Context based on the given environment.


Copyright 1999-2003 Sapient Corporation. All Rights Reserved.