001: /*
002: * GeoLBS - OpenSource Location Based Servces toolkit
003: * (C) 2004, Julian J. Ray, All Rights Reserved
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: */
016:
017: package org.geotools.data.geomedia;
018:
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022: import java.sql.SQLException;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.logging.Logger;
026:
027: import javax.sql.ConnectionPoolDataSource;
028:
029: import org.geotools.data.jdbc.ConnectionPool;
030: import org.geotools.data.jdbc.ConnectionPoolManager;
031:
032: /**
033: * <p>
034: * Title: GeoTools2 Development
035: * </p>
036: *
037: * <p>
038: * Description:
039: * </p>
040: *
041: * <p>
042: * Copyright: Copyright (c) 2003
043: * </p>
044: *
045: * <p>
046: * Company:
047: * </p>
048: * Provides javax.sql.DataSource wrapper around a JDBC object. User passes a JDBC driver string as part of the
049: * properties. Driver is loaded using Class.forname() from the class path. User also passes a connection string URL as
050: * part of the properties.
051: *
052: * @author Julian J. Ray
053: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geomedia/src/main/java/org/geotools/data/geomedia/GeoMediaConnectionFactory.java $
054: * @version 1.0
055: */
056: public class GeoMediaConnectionFactory {
057: /** DOCUMENT ME! */
058: private static final Logger LOGGER = org.geotools.util.logging.Logging
059: .getLogger("org.geotools.data.geomedia");
060:
061: /** Map that contains Connection Pool Data Sources */
062: private static Map mDataSources = new HashMap();
063:
064: // The class name for the JDBC driver
065:
066: /** DOCUMENT ME! */
067: private String mDriverClassName;
068:
069: // The url to the DB
070:
071: /** DOCUMENT ME! */
072: private String mPoolKey;
073:
074: // Connection Parameters
075:
076: /** DOCUMENT ME! */
077: private GeoMediaConnectionParam[] mParams;
078:
079: // The username to login with
080:
081: /** DOCUMENT ME! */
082: private String mUsername = "";
083:
084: /// The password to login with
085:
086: /** DOCUMENT ME! */
087: private String mPasswd = "";
088:
089: /**
090: * Creates a new Connection object.
091: *
092: * @param classname The host name of IP address to connect to.
093: * @param poolKey The port number on the host. Usually 1433
094: * @param params The database name on the host
095: */
096: public GeoMediaConnectionFactory(String classname, String poolKey,
097: GeoMediaConnectionParam[] params) {
098: mDriverClassName = classname;
099: mPoolKey = poolKey;
100: mParams = params;
101: }
102:
103: /**
104: * Creates the real Server Connection. Logs in to the Database and creates the Connection object. If the connection
105: * pool is not established, a new one is created.
106: *
107: * @param user The user name.
108: * @param pass The password
109: *
110: * @return The ConnectionPool object.
111: *
112: * @throws SQLException If an error occurs connecting to the DB.
113: */
114: public ConnectionPool getConnectionPool(String user, String pass)
115: throws SQLException {
116: String poolKey = mPoolKey + ":" + user + ":" + pass;
117: ConnectionPoolDataSource poolDataSource = (ConnectionPoolDataSource) mDataSources
118: .get(poolKey);
119:
120: if (poolDataSource == null) {
121: try {
122: // Attempt to load the driver
123: Class driver = Class.forName(mDriverClassName);
124:
125: // Get the constructor
126: Constructor constr = driver
127: .getConstructor(new Class[] {});
128: poolDataSource = (ConnectionPoolDataSource) constr
129: .newInstance(new Object[] {});
130:
131: // Process the connection parameters
132: for (int i = 0; i < mParams.length; i++) {
133: Method method = driver.getMethod(mParams[i]
134: .getMethodName(), new Class[] { mParams[i]
135: .getClassType() });
136: Object ret = method.invoke(poolDataSource,
137: new Object[] { mParams[i].getParam() });
138: }
139:
140: // Cache the data source
141: mDataSources.put(poolKey, poolDataSource);
142: } catch (ClassNotFoundException e) {
143: throw new SQLException("Driver " + mDriverClassName
144: + " not found!");
145: } catch (NoSuchMethodException e) {
146: throw new SQLException("Driver does not support method"
147: + e.getMessage());
148: } catch (InstantiationException e) {
149: throw new SQLException("Cannot create instance of "
150: + mDriverClassName);
151: } catch (IllegalAccessException e) {
152: throw new SQLException(
153: "IllegalAccessException while instantiating connection pool for driver "
154: + mDriverClassName);
155: } catch (InvocationTargetException e) {
156: throw new SQLException(
157: "IllegalAccessException while instantiating connection pool for driver "
158: + mDriverClassName);
159: }
160: }
161:
162: ConnectionPoolManager manager = ConnectionPoolManager
163: .getInstance();
164: ConnectionPool connectionPool = manager
165: .getConnectionPool(poolDataSource);
166:
167: return connectionPool;
168: }
169:
170: /**
171: * Returns a connection from the connection pool.
172: *
173: * @return The connection to the data base.
174: *
175: * @throws SQLException If an error occurs.
176: */
177: public ConnectionPool getConnectionPool() throws SQLException {
178: return getConnectionPool(mUsername, mPasswd);
179: }
180:
181: /**
182: * Sets the login credentials.
183: *
184: * @param user The username
185: * @param pass The password
186: */
187: public void setLogin(String user, String pass) {
188: this.mUsername = user;
189: this.mPasswd = pass;
190: }
191: }
|