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; either
009: * version 2.1 of the License, or (at your option) any later version.
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.oracle;
017:
018: import java.sql.SQLException;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import javax.sql.DataSource;
023:
024: import oracle.jdbc.pool.OracleConnectionPoolDataSource;
025:
026: import org.geotools.data.jdbc.ConnectionPool;
027: import org.geotools.data.jdbc.ConnectionPoolManager;
028: import org.geotools.data.jdbc.datasource.DataSourceFinder;
029: import org.geotools.data.jdbc.datasource.DataSourceUtil;
030:
031: /**
032: * Provides javax.sql.DataSource wrapper around an OracleConnection object.
033: *
034: * @author Sean Geoghegan, Defence Science and Technology Organisation
035: * @author $Author: seangeo $
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/oracle-spatial/src/main/java/org/geotools/data/oracle/OracleConnectionFactory.java $
037: * @version $Id: OracleConnectionFactory.java 26148 2007-07-04 17:07:11Z aaime $
038: * @deprecated Use {@link DataSource}, {@link DataSourceUtil} and {@link DataSourceFinder} instead
039: */
040: public class OracleConnectionFactory {
041: /** The prefix of an Oracle JDBC url */
042: private static final String JDBC_PATH = "jdbc:oracle:thin:@";
043:
044: /** The prefix of an Oracle OCI JDBC url */
045: private static final String OCI_PATH = "jdbc:oracle:oci:@";
046:
047: /** Map that contains Connection Pool Data Sources */
048: private static Map dataSources = new HashMap();
049:
050: /** The url to the DB */
051: private String dbUrl;
052:
053: /** The username to login with */
054: private String username = "";
055:
056: /** The password to login with */
057: private String passwd = "";
058:
059: /**
060: * BDT ADDED 2004/08/02 This constructor sets up a connection factor for
061: * OCI connection. The alias refers to values defined by Oracle Net
062: * Configuration assitant and stored in
063: * $ORACLE_HOME/network/admin/tnsnames.ora OCI connection require you to
064: * install the oracle client software. The classes12.jar MUST be taken
065: * from $ORACLE_HOME/jdbc/lib Creates a new OracleConnection object that
066: * wraps a oracle.jdbc.driver.OracleConnection.
067: *
068: * @param alias The host to connect to.
069: */
070: public OracleConnectionFactory(String alias) {
071: dbUrl = OCI_PATH + alias;
072: }
073:
074: /**
075: * Creates a new OracleConnection object that wraps a
076: * oracle.jdbc.driver.OracleConnection.
077: *
078: * @param host The host to connect to.
079: * @param port The port number on the host
080: * @param instance The instance name on the host
081: */
082: public OracleConnectionFactory(String host, String port,
083: String instance) {
084: if (instance.startsWith("("))
085: dbUrl = JDBC_PATH + instance;
086: else if (instance.startsWith("/"))
087: dbUrl = JDBC_PATH + "//" + host + ":" + port + instance;
088: else
089: dbUrl = JDBC_PATH + host + ":" + port + ":" + instance;
090: }
091:
092: /**
093: * Creates the real OracleConnection. Logs in to the Oracle Database and
094: * creates the OracleConnection object.
095: *
096: * @param user The user name.
097: * @param pass The password
098: *
099: * @return The real OracleConnection object.
100: *
101: * @throws SQLException If an error occurs connecting to the DB.
102: */
103: public ConnectionPool getConnectionPool(String user, String pass)
104: throws SQLException {
105: String poolKey = dbUrl + user + pass;
106: OracleConnectionPoolDataSource poolDataSource = (OracleConnectionPoolDataSource) dataSources
107: .get(poolKey);
108:
109: if (poolDataSource == null) {
110: poolDataSource = new OracleConnectionPoolDataSource();
111:
112: poolDataSource.setURL(dbUrl);
113: poolDataSource.setUser(user);
114: poolDataSource.setPassword(pass);
115:
116: dataSources.put(poolKey, poolDataSource);
117: }
118:
119: ConnectionPoolManager manager = ConnectionPoolManager
120: .getInstance();
121: ConnectionPool connectionPool = manager
122: .getConnectionPool(poolDataSource);
123:
124: return connectionPool;
125: }
126:
127: /**
128: * Creates the real OracleConnection. Logs into the database using the
129: * credentials provided by setLogin
130: *
131: * @return The oracle connection to the data base.
132: *
133: * @throws SQLException If an error occurs connecting to the DB.
134: */
135: public ConnectionPool getConnectionPool() throws SQLException {
136: return getConnectionPool(username, passwd);
137: }
138:
139: /**
140: * Sets the login credentials.
141: *
142: * @param user The username
143: * @param pass The password
144: */
145: public void setLogin(String user, String pass) {
146: this.username = user;
147:
148: this.passwd = pass;
149: }
150: }
|