001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.postgis;
017:
018: import java.sql.Connection;
019: import java.sql.DriverManager;
020: import java.sql.SQLException;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.logging.Logger;
025:
026: import javax.sql.DataSource;
027:
028: import org.geotools.data.jdbc.ConnectionPool;
029: import org.geotools.data.jdbc.ConnectionPoolManager;
030: import org.geotools.data.jdbc.datasource.DataSourceFinder;
031: import org.geotools.data.jdbc.datasource.DataSourceUtil;
032:
033: /**
034: * Shell for JDBC transactions of all types. This creates connections for the
035: * postgis datasource to make its transactions. To create a
036: * PostgisDataSource, create a PostgisConnectionFactory, then call
037: * getConnection(), and pass that connection to the PostgisDataSource
038: * constructor.
039: *
040: * @author Rob Hranac, Vision for New York
041: * @author Chris Holmes, TOPP
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/postgis/src/main/java/org/geotools/data/postgis/PostgisConnectionFactory.java $
043: * @version $Id: PostgisConnectionFactory.java 27862 2007-11-12 19:51:19Z desruisseaux $
044: * @deprecated Use {@link DataSource}, {@link DataSourceUtil} and {@link DataSourceFinder} instead
045: * *
046: * @task REVISIT: connection pooling, implementing java.sql.Datasource. I
047: * removed the implementing because that class should be provided by the
048: * vendor, not a hack on top of DriverManager. The only problem is that
049: * we can't use the postgres one directly, because it doesn't allow you
050: * to set the charset, which some of our users need.
051: */
052: public class PostgisConnectionFactory {
053: /** Standard logging instance */
054: private static final Logger LOGGER = org.geotools.util.logging.Logging
055: .getLogger("org.geotools.defaultcore");
056:
057: /** Creates PostGIS-specific JDBC driver class. */
058: private static final String DRIVER_CLASS = "org.postgresql.Driver";
059:
060: /** Creates PostGIS-specific JDBC driver path. */
061: private static final String DRIVER_PATH = "jdbc:postgresql";
062:
063: /** The full path used to connect to the database */
064: private String connPath;
065:
066: /** The host to connect to */
067: private String host;
068:
069: /** The database to connect to.*/
070: private String dbName;
071:
072: /** The port to connect on.*/
073: private int port = -1;
074:
075: /** The user to connect with */
076: private String user = "test";
077:
078: /** The password of the user */
079: private String password = "test";
080:
081: /** Map that contains Connection Pool Data Sources */
082: private static Map dataSources = new HashMap();
083:
084: /**
085: * Constructor with all internal database driver classes, driver paths and
086: * database types.
087: *
088: * @param host The name or ip address of the computer where the postgis
089: * database is installed.
090: * @param port The port to connect on; 5432 is generally the postgres
091: * default.
092: * @param dbName The name of the pgsql database that holds the tables of
093: * the feature types that will be used by PostgisDataSource.
094: */
095: public PostgisConnectionFactory(String host, String port,
096: String dbName) {
097: this .host = host;
098: this .dbName = dbName;
099: if (port != null) {
100: try {
101: this .port = Integer.parseInt(port);
102: } catch (NumberFormatException nfe) {
103: throw new IllegalArgumentException(
104: "could not parse port " + "to an int value");
105: }
106: }
107: connPath = DRIVER_PATH + "://" + host + ":" + port + "/"
108: + dbName;
109: }
110:
111: /**
112: * Constructor with all internal database driver classes, driver paths and
113: * database types.
114: *
115: * @param host The name or ip address of the computer where the postgis
116: * database is installed.
117: * @param port The port to connect on; 5432 is generally the postgres
118: * default.
119: * @param dbName The name of the pgsql database that holds the tables of
120: * the feature types that will be used by PostgisDataSource.
121: */
122: public PostgisConnectionFactory(String host, int port, String dbName) {
123: connPath = DRIVER_PATH + "://" + host + ":" + port + "/"
124: + dbName;
125: this .host = host;
126: this .dbName = dbName;
127: this .port = port;
128: }
129:
130: /**
131: * Constructor with all internal database driver classes, driver paths and
132: * database types.
133: *
134: * @param connPath The driver class; should be passed from the
135: * database-specific subclass.
136: */
137: //public PostgisConnectionFactory(String connPath) {
138: // connPath = DRIVER_PATH + "://" + connPath;
139: //}
140: /**
141: * Creates a database connection method to initialize a given database for
142: * feature extraction.
143: *
144: * @param user the name of the user connect to connect to the pgsql db.
145: * @param password the password for the user.
146: */
147: public void setLogin(String user, String password) {
148: this .user = user;
149: this .password = password;
150: }
151:
152: /**
153: * Creates a database connection method to initialize a given database for
154: * feature extraction using the set username and password.
155: *
156: * @return the sql Connection object to the database.
157: *
158: * @throws SQLException if the postgres driver could not be found
159: */
160: public Connection getConnection() throws SQLException {
161: return getConnection(user, password);
162: }
163:
164: /**
165: * Creates a database connection method to initialize a given database for
166: * feature extraction with the user and password params.
167: *
168: * @param user the name of the user connect to connect to the pgsql db.
169: * @param password the password for the user.
170: *
171: * @return the sql Connection object to the database.
172: *
173: * @throws SQLException if the postgis sql driver could not be found
174: */
175: public Connection getConnection(String user, String password)
176: throws SQLException {
177: Properties props = new Properties();
178: props.put("user", user);
179: props.put("password", password);
180:
181: return getConnection(props);
182: }
183:
184: public ConnectionPool getConnectionPool(String user, String pass)
185: throws SQLException {
186: org.postgresql.jdbc2.optional.ConnectionPool poolDataSource = null;
187: String dbUrl = connPath;
188: String poolKey = dbUrl + user + pass;
189: LOGGER.fine("looking up pool key " + poolKey);
190: Object poolDS = dataSources.get(poolKey);
191: poolDataSource = (org.postgresql.jdbc2.optional.ConnectionPool) poolDS;
192: LOGGER.fine("pool is " + poolDataSource);
193: if (poolDataSource == null) {
194: poolDataSource = new org.postgresql.jdbc2.optional.ConnectionPool();
195: //source.setDataSourceName("Geotools Postgis");
196: poolDataSource.setServerName(host);
197: poolDataSource.setDatabaseName(dbName);
198: poolDataSource.setPortNumber(port);
199: poolDataSource.setUser(user);
200: poolDataSource.setPassword(pass);
201: //source.setMaxConnections(10);
202: //the source looks like this defaults to false, but we have
203: //assumed true (as that's how it was before pooling)
204: poolDataSource.setDefaultAutoCommit(true);
205: dataSources.put(poolKey, poolDataSource);
206:
207: }
208:
209: ConnectionPoolManager manager = ConnectionPoolManager
210: .getInstance();
211: ConnectionPool connectionPool = manager
212: .getConnectionPool(poolDataSource);
213:
214: return connectionPool;
215: }
216:
217: public ConnectionPool getConnectionPool() throws SQLException {
218: return getConnectionPool(user, password);
219: }
220:
221: public void free(ConnectionPool connectionPool) {
222: if (connectionPool == null)
223: return;
224: if (!connectionPool.isClosed()) {
225: connectionPool.close();
226: }
227: ConnectionPoolManager.getInstance().free(connectionPool);
228: }
229:
230: /**
231: * Creates a database connection method to initialize a given database for
232: * feature extraction with the given Properties.
233: *
234: * @param props Should contain at a minimum the user and password.
235: * Additional properties, such as charSet, can also be added.
236: *
237: * @return the sql Connection object to the database.
238: *
239: * @throws SQLException if the postgis sql driver could not be found
240: */
241: public Connection getConnection(Properties props)
242: throws SQLException {
243: // makes a new feature type bean to deal with incoming
244: Connection dbConnection = null;
245:
246: // Instantiate the driver classes
247: try {
248: Class.forName(DRIVER_CLASS);
249: LOGGER.finest("getting connection at " + connPath
250: + "with props: " + props);
251: dbConnection = DriverManager.getConnection(connPath, props);
252: } catch (ClassNotFoundException cnfe) {
253: throw new SQLException("Postgis driver was not found.");
254: }
255:
256: return dbConnection;
257: }
258: }
|