package com.mchange.v2.c3p0;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.sql.SqlUtils;
import com.mchange.v2.log.MLevel;
import com.mchange.v2.log.MLog;
import com.mchange.v2.log.MLogger;
import com.mchange.v2.c3p0.cfg.C3P0Config;
import com.mchange.v2.c3p0.impl.DriverManagerDataSourceBase;
public final class DriverManagerDataSource extends DriverManagerDataSourceBase implements DataSource
{
final static MLogger logger;
static
{
logger = MLog.getLogger( DriverManagerDataSource.class );
try { Class.forName( "java.sql.DriverManager" ); }
catch ( Exception e )
{
String msg = "Could not load the DriverManager class?!?";
if ( logger.isLoggable( MLevel.SEVERE ) )
logger.log( MLevel.SEVERE, msg );
throw new InternalError( msg );
}
}
Driver driver;
boolean driver_class_loaded = false;
public DriverManagerDataSource()
{ this( true ); }
public DriverManagerDataSource(boolean autoregister)
{
super( autoregister );
setUpPropertyListeners();
String user = C3P0Config.initializeStringPropertyVar("user", null);
String password = C3P0Config.initializeStringPropertyVar("password", null);
if (user != null)
this.setUser( user );
if (password != null)
this.setPassword( password );
}
private void setUpPropertyListeners()
{
PropertyChangeListener driverClassListener = new PropertyChangeListener()
{
public void propertyChange( PropertyChangeEvent evt )
{
if ( "driverClass".equals( evt.getPropertyName() ) )
{
synchronized (DriverManagerDataSource.this)
{
setDriverClassLoaded( false );
if ( driverClass != null && driverClass.trim().length() == 0 )
driverClass = null;
}
}
}
};
this.addPropertyChangeListener( driverClassListener );
}
private synchronized boolean isDriverClassLoaded()
{ return driver_class_loaded; }
private synchronized void setDriverClassLoaded(boolean dcl)
{
this.driver_class_loaded = dcl;
if (! driver_class_loaded) clearDriver();
}
private synchronized void ensureDriverLoaded() throws SQLException
{
try
{
if (! isDriverClassLoaded())
{
if (driverClass != null)
Class.forName( driverClass );
setDriverClassLoaded( true );
}
}
catch (ClassNotFoundException e)
{
if (logger.isLoggable(MLevel.WARNING))
logger.log(MLevel.WARNING, "Could not load driverClass " + driverClass, e);
}
}
public Connection getConnection() throws SQLException
{
ensureDriverLoaded();
Connection out = driver().connect( jdbcUrl, properties );
if (out == null)
throw new SQLException("Apparently, jdbc URL '" + jdbcUrl + "' is not valid for the underlying " +
"driver [" + driver() + "].");
return out;
}
public Connection getConnection(String username, String password) throws SQLException
{
ensureDriverLoaded();
Connection out = driver().connect( jdbcUrl, overrideProps(username, password) );
if (out == null)
throw new SQLException("Apparently, jdbc URL '" + jdbcUrl + "' is not valid for the underlying " +
"driver [" + driver() + "].");
return out;
}
public PrintWriter getLogWriter() throws SQLException
{ return DriverManager.getLogWriter(); }
public void setLogWriter(PrintWriter out) throws SQLException
{ DriverManager.setLogWriter( out ); }
public int getLoginTimeout() throws SQLException
{ return DriverManager.getLoginTimeout(); }
public void setLoginTimeout(int seconds) throws SQLException
{ DriverManager.setLoginTimeout( seconds ); }
public synchronized void setJdbcUrl(String jdbcUrl)
{
super.setJdbcUrl( jdbcUrl );
clearDriver();
}
public synchronized void setUser(String user)
{
String oldUser = this.getUser();
if (! eqOrBothNull( user, oldUser ))
{
if (user != null)
properties.put( SqlUtils.DRIVER_MANAGER_USER_PROPERTY, user );
else
properties.remove( SqlUtils.DRIVER_MANAGER_USER_PROPERTY );
pcs.firePropertyChange("user", oldUser, user);
}
}
public synchronized String getUser()
{
return properties.getProperty( SqlUtils.DRIVER_MANAGER_USER_PROPERTY );
}
public synchronized void setPassword(String password)
{
String oldPass = this.getPassword();
if (! eqOrBothNull( password, oldPass ))
{
if (password != null)
properties.put( SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY, password );
else
properties.remove( SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY );
pcs.firePropertyChange("password", oldPass, password);
}
}
public synchronized String getPassword()
{ return properties.getProperty( SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY ); }
private final Properties overrideProps(String user, String password)
{
Properties overriding = (Properties) properties.clone();
if (user != null)
overriding.put(SqlUtils.DRIVER_MANAGER_USER_PROPERTY, user);
else
overriding.remove(SqlUtils.DRIVER_MANAGER_USER_PROPERTY);
if (password != null)
overriding.put(SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY, password);
else
overriding.remove(SqlUtils.DRIVER_MANAGER_PASSWORD_PROPERTY);
return overriding;
}
private synchronized Driver driver() throws SQLException
{
if (driver == null)
{
if (driverClass != null && forceUseNamedDriverClass)
{
if ( Debug.DEBUG && logger.isLoggable( MLevel.FINER ) )
logger.finer( "Circumventing DriverManager and instantiating driver class '" + driverClass +
"' directly. (forceUseNamedDriverClass = " + forceUseNamedDriverClass + ")" );
try
{
driver = (Driver) Class.forName( driverClass ).newInstance();
this.setDriverClassLoaded( true );
}
catch (Exception e)
{ SqlUtils.toSQLException("Cannot instantiate specified JDBC driver. Exception while initializing named, forced-to-use driver class'" + driverClass +"'", e); }
}
else
driver = DriverManager.getDriver( jdbcUrl );
}
return driver;
}
private synchronized void clearDriver()
{ driver = null; }
private static boolean eqOrBothNull( Object a, Object b )
{ return (a == b || (a != null && a.equals(b))); }
private static final long serialVersionUID = 1;
private static final short VERSION = 0x0001;
private void writeObject( ObjectOutputStream oos ) throws IOException
{
oos.writeShort( VERSION );
}
private void readObject( ObjectInputStream ois ) throws IOException, ClassNotFoundException
{
short version = ois.readShort();
switch (version)
{
case VERSION:
setUpPropertyListeners();
break;
default:
throw new IOException("Unsupported Serialized Version: " + version);
}
}
private boolean isWrapperForThis(Class<?> iface)
{ return iface.isAssignableFrom( this.getClass() ); }
public boolean isWrapperFor(Class<?> iface) throws SQLException
{
return isWrapperForThis( iface );
}
public <T> T unwrap(Class<T> iface) throws SQLException
{
if ( this.isWrapperForThis( iface ) )
return (T) this;
else
throw new SQLException(this + " is not a wrapper for or implementation of " + iface.getName());
}
}