~ubuntu-branches/ubuntu/wily/libpgjava/wily

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/jdbc2/optional/PGObjectFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2005-04-21 14:25:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050421142511-wibh5vc31fkrorx7
Tags: 7.4.7-3
Built with sources...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.jdbc2.optional;
 
2
 
 
3
import javax.naming.spi.ObjectFactory;
 
4
import javax.naming.*;
 
5
import java.util.Hashtable;
 
6
 
 
7
/**
 
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
 
13
 * consistent.
 
14
 *
 
15
 * @author Aaron Mulder (ammulder@chariotsolutions.com)
 
16
 * @version $Revision: 1.3 $
 
17
 */
 
18
public class PGObjectFactory implements ObjectFactory
 
19
{
 
20
        /**
 
21
         * Dereferences a PostgreSQL DataSource.  Other types of references are
 
22
         * ignored.
 
23
         */
 
24
        public Object getObjectInstance(Object obj, Name name, Context nameCtx,
 
25
                                                                        Hashtable environment) throws Exception
 
26
        {
 
27
                Reference ref = (Reference)obj;
 
28
                if (ref.getClassName().equals(SimpleDataSource.class.getName()))
 
29
                {
 
30
                        return loadSimpleDataSource(ref);
 
31
                }
 
32
                else if (ref.getClassName().equals(ConnectionPool.class.getName()))
 
33
                {
 
34
                        return loadConnectionPool(ref);
 
35
                }
 
36
                else if (ref.getClassName().equals(PoolingDataSource.class.getName()))
 
37
                {
 
38
                        return loadPoolingDataSource(ref);
 
39
                }
 
40
                else
 
41
                {
 
42
                        return null;
 
43
                }
 
44
        }
 
45
 
 
46
        private Object loadPoolingDataSource(Reference ref)
 
47
        {
 
48
                // If DataSource exists, return it
 
49
                String name = getProperty(ref, "dataSourceName");
 
50
                PoolingDataSource pds = PoolingDataSource.getDataSource(name);
 
51
                if (pds != null)
 
52
                {
 
53
                        return pds;
 
54
                }
 
55
                // Otherwise, create a new one
 
56
                pds = new PoolingDataSource();
 
57
                pds.setDataSourceName(name);
 
58
                loadBaseDataSource(pds, ref);
 
59
                String min = getProperty(ref, "initialConnections");
 
60
                if (min != null)
 
61
                {
 
62
                        pds.setInitialConnections(Integer.parseInt(min));
 
63
                }
 
64
                String max = getProperty(ref, "maxConnections");
 
65
                if (max != null)
 
66
                {
 
67
                        pds.setMaxConnections(Integer.parseInt(max));
 
68
                }
 
69
                return pds;
 
70
        }
 
71
 
 
72
        private Object loadSimpleDataSource(Reference ref)
 
73
        {
 
74
                SimpleDataSource ds = new SimpleDataSource();
 
75
                return loadBaseDataSource(ds, ref);
 
76
        }
 
77
 
 
78
        private Object loadConnectionPool(Reference ref)
 
79
        {
 
80
                ConnectionPool cp = new ConnectionPool();
 
81
                return loadBaseDataSource(cp, ref);
 
82
        }
 
83
 
 
84
        protected Object loadBaseDataSource(BaseDataSource ds, Reference ref)
 
85
        {
 
86
                ds.setDatabaseName(getProperty(ref, "databaseName"));
 
87
                ds.setPassword(getProperty(ref, "password"));
 
88
                String port = getProperty(ref, "portNumber");
 
89
                if (port != null)
 
90
                {
 
91
                        ds.setPortNumber(Integer.parseInt(port));
 
92
                }
 
93
                ds.setServerName(getProperty(ref, "serverName"));
 
94
                ds.setUser(getProperty(ref, "user"));
 
95
                return ds;
 
96
        }
 
97
 
 
98
    protected String getProperty(Reference ref, String s)
 
99
        {
 
100
                RefAddr addr = ref.get(s);
 
101
                if (addr == null)
 
102
                {
 
103
                        return null;
 
104
                }
 
105
                return (String)addr.getContent();
 
106
        }
 
107
 
 
108
}