com.hfg.sql.jdbc.JDBCDataSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.sql.jdbc;
import com.hfg.security.LoginCredentials;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
//------------------------------------------------------------------------------
/**
Represents a JDBC-compatible DataSource.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class JDBCDataSource implements DataSource
{
private String mName;
private PrintWriter mLogWriter;
private Logger mLogger;
private T mSettings;
private JDBCServer mServer;
private LoginCredentials mCredentials;
private String mDatabaseName;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public JDBCDataSource(JDBCServer inServer, String inDatabaseName)
{
mServer = inServer;
mDatabaseName = inDatabaseName;
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public JDBCDataSource setName(String inValue)
{
mName = inValue;
return this;
}
//---------------------------------------------------------------------------
public String name()
{
return mName;
}
//---------------------------------------------------------------------------
public JDBCDataSource setSettings(T inValue)
{
mSettings = inValue;
return this;
}
//---------------------------------------------------------------------------
public T getSettings()
{
if (null == mSettings)
{
mSettings = initSettings();
}
return mSettings;
}
//---------------------------------------------------------------------------
public JDBCServer getServer()
{
return mServer;
}
//---------------------------------------------------------------------------
public String getDatabaseName()
{
return mDatabaseName;
}
//---------------------------------------------------------------------------
public JDBCDataSource setCredentials(LoginCredentials inValue)
{
mCredentials = inValue;
return this;
}
//---------------------------------------------------------------------------
protected LoginCredentials getCredentials()
{
return mCredentials;
}
//---------------------------------------------------------------------------
public JDBCDataSource setParentLogger(Logger inValue)
{
mLogger = inValue;
return this;
}
//---------------------------------------------------------------------------
@Override
public Connection getConnection()
throws SQLException
{
if (null == mCredentials)
{
throw new SQLException("No login credentials have been specified!");
}
return getServer().getConnection(getDatabaseName(), mCredentials, getSettings());
}
//---------------------------------------------------------------------------
@Override
public Connection getConnection(String inUsername, String inPassword)
throws SQLException
{
setCredentials(new LoginCredentials(inUsername, inPassword.toCharArray()));
return getConnection();
}
//---------------------------------------------------------------------------
@Override
public PrintWriter getLogWriter()
throws SQLException
{
return mLogWriter;
}
//---------------------------------------------------------------------------
@Override
public void setLogWriter(PrintWriter inValue)
throws SQLException
{
mLogWriter = inValue;
}
//---------------------------------------------------------------------------
@Override
public void setLoginTimeout(int inValueInSeconds)
throws SQLException
{
getSettings().setConnectTimeoutInSeconds(inValueInSeconds);
}
//---------------------------------------------------------------------------
@Override
public int getLoginTimeout()
throws SQLException
{
return getSettings().getConnectTimeoutInSeconds();
}
//---------------------------------------------------------------------------
@Override
public Logger getParentLogger()
throws SQLFeatureNotSupportedException
{
return mLogger;
}
//---------------------------------------------------------------------------
@Override
public T unwrap(Class inInterface)
throws SQLException
{
try
{
return inInterface.cast(this);
}
catch (ClassCastException e)
{
throw new SQLException("Unable to unwrap the DataSouce to " + inInterface.getName() + "!", e);
}
}
//---------------------------------------------------------------------------
@Override
public boolean isWrapperFor(Class> inInterface)
throws SQLException
{
return inInterface.isInstance(this);
}
//###########################################################################
// PROTECTED METHODS
//###########################################################################
//---------------------------------------------------------------------------
protected T initSettings()
{
return (T) new JDBCConnectionSettings();
}
}