forked from geodmms/xdgnjobs

ulysseskao
2013-12-06 d617e9455852252d061b88968649090d6903e9f2
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * JtsWrapper.java
 *
 * Allows transparent usage of JTS Geometry classes via PostgreSQL JDBC driver
 * connected to a PostGIS enabled PostgreSQL server.
 *
 * (C) 2005 Markus Schaber, markus.schaber@logix-tt.com
 *
 * 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.
 *
 * 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 or visit the web at
 * http://www.gnu.org.
 *
 * $Id$
 */
 
package com.ximple.eofms.util.postjts;
 
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import org.postgresql.Driver;
import org.postgresql.PGConnection;
 
/**
 * DriverWrapper
 * <p/>
 * Wraps the PostGreSQL Driver to add the JTS/PostGIS Object Classes.
 * <p/>
 * This method currently works with J2EE DataSource implementations, and with
 * DriverManager framework.
 * <p/>
 * Simply replace the "jdbc:postgresql:" with a "jdbc:postgres_jts:" in the jdbc
 * URL.
 * <p/>
 * When using the drivermanager, you need to initialize JtsWrapper instead of
 * (or in addition to) org.postgresql.Driver. When using a J2EE DataSource
 * implementation, set the driver class property in the datasource config, the
 * following works for jboss:
 * <p/>
 * &lt;driver-class&gt;org.postgis.jts.PostGisWrapper&lt;/driver-class&gt;
 *
 * @author markus.schaber@logix-tt.com
 */
public class JtsWrapper extends Driver {
 
    protected static final Logger logger = Logger.getLogger("org.postgis.DriverWrapper");
 
    private static final String POSTGRES_PROTOCOL = "jdbc:postgresql:";
    private static final String POSTGIS_PROTOCOL = "jdbc:postgres_jts:";
    public static final String REVISION = "$Revision: 2570 $";
 
    public JtsWrapper() {
        super();
    }
 
    static {
        try {
            // Try to register ourself to the DriverManager
            java.sql.DriverManager.registerDriver(new JtsWrapper());
        } catch (SQLException e) {
            logger.log(Level.WARNING, "Error registering PostgreSQL Jts Wrapper Driver", e);
        }
    }
 
    /**
     * Creates a postgresql connection, and then adds the JTS GIS data types to
     * it calling addpgtypes()
     *
     * @param url  the URL of the database to connect to
     * @param info a list of arbitrary tag/value pairs as connection arguments
     * @return a connection to the URL or null if it isnt us
     * @throws SQLException if a database access error occurs
     * @see java.sql.Driver#connect
     * @see org.postgresql.Driver
     */
    public java.sql.Connection connect(String url, Properties info) throws SQLException {
        url = mangleURL(url);
        Connection result = super.connect(url, info);
        addGISTypes((PGConnection) result);
        return result;
    }
 
    /**
     * adds the JTS/PostGIS Data types to a PG Connection.
     *
     * @param pgconn postgres connection
     * @throws SQLException error
     */
    public static void addGISTypes(PGConnection pgconn) throws SQLException {
        pgconn.addDataType("geometry", JtsGeometry.class);
    }
 
    /**
     * Mangles the PostGIS URL to return the original PostGreSQL URL
     *
     * @param url url
     * @return string
     * @throws java.sql.SQLException erroe
     */
    public static String mangleURL(String url) throws SQLException {
        if (url.startsWith(POSTGIS_PROTOCOL)) {
            return POSTGRES_PROTOCOL + url.substring(POSTGIS_PROTOCOL.length());
        } else {
            throw new SQLException("Unknown protocol or subprotocol in url " + url);
        }
    }
 
    /**
     * Check whether the driver thinks he can handle the given URL.
     *
     * @param url the URL of the driver
     * @return true if this driver accepts the given URL
     * @throws SQLException Passed through from the underlying PostgreSQL
     *                      driver, should not happen.
     * @see java.sql.Driver#acceptsURL
     */
    public boolean acceptsURL(String url) throws SQLException {
        try {
            url = mangleURL(url);
        } catch (SQLException e) {
            return false;
        }
        return super.acceptsURL(url);
    }
 
    /**
     * Gets the underlying drivers major version number
     *
     * @return the drivers major version number
     */
 
    public int getMajorVersion() {
        return super.getMajorVersion();
    }
 
    /**
     * Get the underlying drivers minor version number
     *
     * @return the drivers minor version number
     */
    public int getMinorVersion() {
        return super.getMinorVersion();
    }
 
    /**
     * Returns our own CVS version plus postgres Version
     *
     * @return version
     */
    public static String getVersion() {
        return "JtsGisWrapper " + REVISION + ", wrapping " + Driver.getVersion();
    }
 
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}