001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) Copyright IBM Corporation, 2005. All rights reserved.
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: */
017: package org.geotools.data.db2;
018:
019: import com.ibm.db2.jcc.DB2ConnectionPoolDataSource;
020: import org.geotools.data.jdbc.ConnectionPool;
021: import org.geotools.data.jdbc.ConnectionPoolManager;
022: import org.geotools.data.jdbc.datasource.DataSourceFinder;
023: import org.geotools.data.jdbc.datasource.DataSourceUtil;
024:
025: import java.sql.SQLException;
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.logging.Logger;
029: import javax.sql.ConnectionPoolDataSource;
030: import javax.sql.DataSource;
031:
032: /**
033: * A factory to create a DB2 Connection based on the needed parameters.
034: *
035: * @author David Adler - IBM Corporation
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/db2/src/main/java/org/geotools/data/db2/DB2ConnectionFactory.java $
037: * @deprecated Use {@link DataSource}, {@link DataSourceUtil} and {@link DataSourceFinder} instead
038: */
039: public class DB2ConnectionFactory {
040: private static final Logger LOGGER = org.geotools.util.logging.Logging
041: .getLogger("org.geotools.data.db2");
042: private static final String DB2_URL_PREFIX = "jdbc:db2://";
043: private static Map DATA_SOURCES = new HashMap();
044: private String dbURL = null;
045: private String dbname = null;
046: private String user = null;
047: private String pw = null;
048: private String host = null;
049: private String portnum = null;
050:
051: /**
052: * Creates a new DB2ConnectionFactory from a host name, port number, and
053: * database name.
054:
055: * If the port number is zero we will try to use the JDBC type 2 driver
056: * and if the port number is non-zer, we will try to use the JDBC type 4
057: * driver
058: *
059: * @param host the DB2 host name
060: * @param portnum the TCP/IP port number for the DB2 client connection
061: * @param dbname the DB2 database name
062: */
063: public DB2ConnectionFactory(String host, String portnum,
064: String dbname) {
065: super ();
066: this .dbURL = DB2_URL_PREFIX + host + ":" + portnum + "/"
067: + dbname;
068: this .dbname = dbname;
069: this .portnum = portnum;
070: this .host = host;
071: }
072:
073: /**
074: * Sets database login information.
075: *
076: * @param user the database user name
077: * @param pw the user's database password
078: */
079: public void setLogin(String user, String pw) {
080: this .user = user;
081: this .pw = pw;
082: }
083:
084: /**
085: * Returns the database URL string for this connection.
086: *
087: * @return dbURL
088: */
089: public String getDbURL() {
090: return this .dbURL;
091: }
092:
093: /**
094: * Returns a ConnectionPool. Assumes that the required connection
095: * information: database name, user name and password have already been
096: * set.
097: *
098: * @return a ConnectionPool object
099: *
100: * @throws SQLException if we fail to get a database connection
101: */
102: public ConnectionPool getConnectionPool() throws SQLException {
103: String key = this .dbURL + this .user + this .pw;
104: DB2ConnectionPoolDataSource poolDataSource = (DB2ConnectionPoolDataSource) DATA_SOURCES
105: .get(key);
106:
107: // Create a new pool data source if one doesn't already exist.
108: if (poolDataSource == null) {
109: poolDataSource = new DB2ConnectionPoolDataSource();
110: int portValue = Integer.valueOf(portnum).intValue();
111: int driverType = 4;
112: poolDataSource = new DB2ConnectionPoolDataSource();
113: if (portValue == 0) { // If there is no port number, use type 2 driver
114: driverType = 2;
115: }
116: poolDataSource.setDriverType(driverType);
117: poolDataSource.setPortNumber(portValue);
118: poolDataSource.setServerName(host);
119: poolDataSource.setDatabaseName(this .dbname);
120: poolDataSource.setUser(this .user);
121: poolDataSource.setPassword(this .pw);
122: DATA_SOURCES.put(key, poolDataSource);
123: LOGGER
124: .info("Created new DB2ConnectionPoolDataSource for dbUrl "
125: + this .dbURL);
126: }
127:
128: ConnectionPoolManager manager = ConnectionPoolManager
129: .getInstance();
130: ConnectionPool connectionPool = manager
131: .getConnectionPool((ConnectionPoolDataSource) poolDataSource);
132:
133: LOGGER.info("Successfully obtained DB2 ConnectionPool");
134:
135: return connectionPool;
136: }
137: }
|