File: PGObjectFactory.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (130 lines) | stat: -rw-r--r-- 4,439 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/ds/common/PGObjectFactory.java,v 1.6 2008/01/08 06:56:27 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.ds.common;

import javax.naming.*;
import javax.naming.spi.ObjectFactory;
import java.util.Hashtable;

import org.postgresql.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 (ammulder@chariotsolutions.com)
 */
public class PGObjectFactory 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("org.postgresql.ds.PGSimpleDataSource")
                || className.equals("org.postgresql.jdbc2.optional.SimpleDataSource")
                || className.equals("org.postgresql.jdbc3.Jdbc3SimpleDataSource"))
        {
            return loadSimpleDataSource(ref);
        }
        else if (className.equals("org.postgresql.ds.PGConnectionPoolDataSource")
                 || className.equals("org.postgresql.jdbc2.optional.ConnectionPool")
                 || className.equals("org.postgresql.jdbc3.Jdbc3ConnectionPool"))
        {
            return loadConnectionPool(ref);
        }
        else if (className.equals("org.postgresql.ds.PGPoolingDataSource")
                 || className.equals("org.postgresql.jdbc2.optional.PoolingDataSource")
                 || className.equals("org.postgresql.jdbc3.Jdbc3PoolingDataSource"))
        {
            return loadPoolingDataSource(ref);
        }
        else
        {
            return null;
        }
    }

    private Object loadPoolingDataSource(Reference ref)
    {
        // If DataSource exists, return it
        String name = getProperty(ref, "dataSourceName");
        PGPoolingDataSource pds = PGPoolingDataSource.getDataSource(name);
        if (pds != null)
        {
            return pds;
        }
        // Otherwise, create a new one
        pds = new PGPoolingDataSource();
        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)
    {
        PGSimpleDataSource ds = new PGSimpleDataSource();
        return loadBaseDataSource(ds, ref);
    }

    private Object loadConnectionPool(Reference ref)
    {
        PGConnectionPoolDataSource cp = new PGConnectionPoolDataSource();
        return loadBaseDataSource(cp, ref);
    }

    protected Object loadBaseDataSource(BaseDataSource ds, Reference ref)
    {
        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));

        return ds;
    }

    protected String getProperty(Reference ref, String s)
    {
        RefAddr addr = ref.get(s);
        if (addr == null)
        {
            return null;
        }
        return (String)addr.getContent();
    }

}