1
package org.postgresql.jdbc2.optional;
3
import javax.naming.spi.ObjectFactory;
5
import java.util.Hashtable;
8
* Returns a DataSource-ish thing based on a JNDI reference. In the case of a
9
* SimpleDataSource or ConnectionPool, a new instance is created each time, as
10
* there is no connection state to maintain. In the case of a PoolingDataSource,
11
* the same DataSource will be returned for every invocation within the same
12
* VM/ClassLoader, so that the state of the connections in the pool will be
15
* @author Aaron Mulder (ammulder@chariotsolutions.com)
16
* @version $Revision: 1.3 $
18
public class PGObjectFactory implements ObjectFactory
21
* Dereferences a PostgreSQL DataSource. Other types of references are
24
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
25
Hashtable environment) throws Exception
27
Reference ref = (Reference)obj;
28
if (ref.getClassName().equals(SimpleDataSource.class.getName()))
30
return loadSimpleDataSource(ref);
32
else if (ref.getClassName().equals(ConnectionPool.class.getName()))
34
return loadConnectionPool(ref);
36
else if (ref.getClassName().equals(PoolingDataSource.class.getName()))
38
return loadPoolingDataSource(ref);
46
private Object loadPoolingDataSource(Reference ref)
48
// If DataSource exists, return it
49
String name = getProperty(ref, "dataSourceName");
50
PoolingDataSource pds = PoolingDataSource.getDataSource(name);
55
// Otherwise, create a new one
56
pds = new PoolingDataSource();
57
pds.setDataSourceName(name);
58
loadBaseDataSource(pds, ref);
59
String min = getProperty(ref, "initialConnections");
62
pds.setInitialConnections(Integer.parseInt(min));
64
String max = getProperty(ref, "maxConnections");
67
pds.setMaxConnections(Integer.parseInt(max));
72
private Object loadSimpleDataSource(Reference ref)
74
SimpleDataSource ds = new SimpleDataSource();
75
return loadBaseDataSource(ds, ref);
78
private Object loadConnectionPool(Reference ref)
80
ConnectionPool cp = new ConnectionPool();
81
return loadBaseDataSource(cp, ref);
84
protected Object loadBaseDataSource(BaseDataSource ds, Reference ref)
86
ds.setDatabaseName(getProperty(ref, "databaseName"));
87
ds.setPassword(getProperty(ref, "password"));
88
String port = getProperty(ref, "portNumber");
91
ds.setPortNumber(Integer.parseInt(port));
93
ds.setServerName(getProperty(ref, "serverName"));
94
ds.setUser(getProperty(ref, "user"));
98
protected String getProperty(Reference ref, String s)
100
RefAddr addr = ref.get(s);
105
return (String)addr.getContent();