com.foundationdb.sql.jdbc.ds.common.FDBObjectFactory Maven / Gradle / Ivy
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2011, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package com.foundationdb.sql.jdbc.ds.common;
import javax.naming.*;
import javax.naming.spi.ObjectFactory;
import java.util.Hashtable;
import com.foundationdb.sql.jdbc.ds.*;
/**
* Returns a DataSource-ish thing based on a JNDI reference. In the case of a
* SimpleDataSource or ConnectionPool, a new instance is created each time, as
* there is no connection state to maintain. In the case of a PoolingDataSource,
* the same DataSource will be returned for every invocation within the same
* VM/ClassLoader, so that the state of the connections in the pool will be
* consistent.
*
* @author Aaron Mulder ([email protected])
*/
public class FDBObjectFactory implements ObjectFactory
{
/**
* Dereferences a PostgreSQL DataSource. Other types of references are
* ignored.
*/
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable environment) throws Exception
{
Reference ref = (Reference)obj;
String className = ref.getClassName();
if (className.equals("com.foundationdb.sql.jdbc.ds.FDBSimpleDataSource")
|| className.equals("com.foundationdb.sql.jdbc.jdbc2.optional.SimpleDataSource")
|| className.equals("com.foundationdb.sql.jdbc.jdbc3.Jdbc3SimpleDataSource"))
{
return loadSimpleDataSource(ref);
}
else if (className.equals("com.foundationdb.sql.jdbc.ds.FDBConnectionPoolDataSource")
|| className.equals("com.foundationdb.sql.jdbc.jdbc2.optional.ConnectionPool")
|| className.equals("com.foundationdb.sql.jdbc.jdbc3.Jdbc3ConnectionPool"))
{
return loadConnectionPool(ref);
}
else if (className.equals("com.foundationdb.sql.jdbc.ds.FDBPoolingDataSource")
|| className.equals("com.foundationdb.sql.jdbc.jdbc2.optional.PoolingDataSource")
|| className.equals("com.foundationdb.sql.jdbc.jdbc3.Jdbc3PoolingDataSource"))
{
return loadPoolingDataSource(ref);
}
else
{
return null;
}
}
private Object loadPoolingDataSource(Reference ref)
{
// If DataSource exists, return it
String name = getProperty(ref, "dataSourceName");
FDBPoolingDataSource pds = FDBPoolingDataSource.getDataSource(name);
if (pds != null)
{
return pds;
}
// Otherwise, create a new one
pds = new FDBPoolingDataSource();
pds.setDataSourceName(name);
loadBaseDataSource(pds, ref);
String min = getProperty(ref, "initialConnections");
if (min != null)
{
pds.setInitialConnections(Integer.parseInt(min));
}
String max = getProperty(ref, "maxConnections");
if (max != null)
{
pds.setMaxConnections(Integer.parseInt(max));
}
return pds;
}
private Object loadSimpleDataSource(Reference ref)
{
FDBSimpleDataSource ds = new FDBSimpleDataSource();
return loadBaseDataSource(ds, ref);
}
private Object loadConnectionPool(Reference ref)
{
FDBConnectionPoolDataSource cp = new FDBConnectionPoolDataSource();
return loadBaseDataSource(cp, ref);
}
protected Object loadBaseDataSource(BaseDataSource ds, Reference ref)
{
ds.setProtocol(getProperty(ref, "protocol"));
ds.setDatabaseName(getProperty(ref, "databaseName"));
ds.setPassword(getProperty(ref, "password"));
String port = getProperty(ref, "portNumber");
if (port != null)
{
ds.setPortNumber(Integer.parseInt(port));
}
ds.setServerName(getProperty(ref, "serverName"));
ds.setUser(getProperty(ref, "user"));
String prepareThreshold = getProperty(ref, "prepareThreshold");
if (prepareThreshold != null)
ds.setPrepareThreshold(Integer.parseInt(prepareThreshold));
String binaryTransfer = getProperty(ref, "binaryTransfer");
if (binaryTransfer != null)
ds.setBinaryTransfer(Boolean.getBoolean(binaryTransfer));
String binaryTransferEnable = getProperty(ref, "binaryTransferEnable");
if (binaryTransferEnable != null)
ds.setBinaryTransferEnable(binaryTransferEnable);
String binaryTransferDisable = getProperty(ref, "binaryTransferDisable");
if (binaryTransferDisable != null)
ds.setBinaryTransferDisable(binaryTransferDisable);
return ds;
}
protected String getProperty(Reference ref, String s)
{
RefAddr addr = ref.get(s);
if (addr == null)
{
return null;
}
return (String)addr.getContent();
}
}