001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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: */
017: package org.geotools.data.geometryless;
018:
019: import java.sql.SQLException;
020: import java.util.logging.Logger;
021:
022: import javax.sql.ConnectionPoolDataSource;
023: import javax.sql.PooledConnection;
024:
025: /**
026: * Creates ConnectionPool objects for a certain JDBC database instance.
027: *
028: * @author Rob Atkinson rob@socialchange.net.NOSPAM.au
029: * @author Gary Sheppard garysheppard@psu.edu
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geometryless/src/main/java/org/geotools/data/geometryless/ConnectionPoolFacade.java $
031: */
032: public class ConnectionPoolFacade implements ConnectionPoolDataSource {
033:
034: /** Standard logging instance */
035: private static final Logger LOGGER = org.geotools.util.logging.Logging
036: .getLogger("org.geotools.data.geometryless");
037:
038: private ConnectionPoolDataSource _nativePool;
039:
040: /** Creates configuration-driven JDBC driver class. */
041:
042: private String _dbURL;
043:
044: private String _username = "";
045:
046: private String _password = "";
047:
048: /**
049: * Creates a new ConnectionPool object from a specified database URL. This
050: * is normally of the following format: <br/><br/>jdbc:mysql:// <host>:
051: * <port>/ <instance>
052: *
053: * @param url
054: * the JDBC database URL
055: */
056: public ConnectionPoolFacade(String poolKey, String driver)
057: throws SQLException {
058:
059: try {
060: _nativePool = (ConnectionPoolDataSource) (Class
061: .forName(driver).newInstance());
062: // LOGGER.fine("Obtained ConnectionPoolDataSource " + _nativePool
063: // + " from driver " + driver);
064:
065: } catch (Exception e) {
066: throw new SQLException(
067: "Failed to instantiate connection pool using "
068: + driver + "(" + e + ")");
069: }
070: }
071:
072: /**
073: * Sets the JDBC database login credentials.
074: *
075: * @param username
076: * the username
077: *
078: */
079: public void setUser(String username) {
080: _username = username;
081:
082: }
083:
084: /**
085: * Sets the JDBC database login credentials.
086: *
087: * @param username
088: * the username
089: * @param password
090: * the password
091: */
092: public void setURL(String dbURL) throws SQLException {
093:
094: _dbURL = dbURL;
095:
096: try {
097:
098: _nativePool.getClass().getMethod("setURL",
099: new Class[] { String.class }).invoke(_nativePool,
100: new Object[] { _dbURL });
101: } catch (Exception e) {
102:
103: LOGGER
104: .severe("Failed to instantiate connection pool trying setURL method with value "
105: + _dbURL);
106: LOGGER
107: .severe("Either the info.xml urlprefix is incorrect, or you need to use an org.geotools.data.geometryless.wrapper class,");
108: LOGGER
109: .severe("or you need to wrapper your driver to provide a setURL method");
110:
111: throw new SQLException(
112: "Failed to instantiate connection pool using "
113: + _dbURL + "(" + e + ")");
114: }
115:
116: LOGGER.fine("Instantiated connection pool using " + _dbURL);
117: }
118:
119: /**
120: * Sets the JDBC database login credentials.
121: *
122: * @param username
123: * the username
124: * @param password
125: * the password
126: */
127: public void setPassword(String password) {
128:
129: _password = password;
130: }
131:
132: public PooledConnection getPooledConnection() throws SQLException {
133:
134: return getPooledConnection(_username, _password);
135: }
136:
137: /* these are all the methods needed for the interface... */
138:
139: public PooledConnection getPooledConnection(String user,
140: String password) throws SQLException {
141:
142: return _nativePool.getPooledConnection(user, password);
143: }
144:
145: public int getLoginTimeout() throws SQLException {
146: return _nativePool.getLoginTimeout();
147: }
148:
149: public java.io.PrintWriter getLogWriter() throws SQLException {
150: return _nativePool.getLogWriter();
151: }
152:
153: public void setLogWriter(java.io.PrintWriter out)
154: throws SQLException {
155: _nativePool.setLogWriter(out);
156: }
157:
158: public void setLoginTimeout(int seconds) throws SQLException {
159: _nativePool.setLoginTimeout(seconds);
160: }
161: }
|