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.Connection;
020: import java.sql.DriverManager;
021: import java.sql.SQLException;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.logging.Logger;
026:
027: import org.geotools.data.jdbc.ConnectionPool;
028: import org.geotools.data.jdbc.ConnectionPoolManager;
029:
030: /**
031: * Creates ConnectionPool objects for a certain JDBC database instance.
032: * @author Rob Atkinson rob@socialchange.net.NOSPAM.au
033: * @author Gary Sheppard garysheppard@psu.edu
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geometryless/src/main/java/org/geotools/data/geometryless/JDBCConnectionFactory.java $
035: */
036: public class JDBCConnectionFactory {
037:
038: /** Standard logging instance */
039: private static final Logger LOGGER = org.geotools.util.logging.Logging
040: .getLogger("org.geotools.data.geometryless");
041:
042: /** Creates configuration-driven JDBC driver class. */
043: private String _driver = "";
044: private static Map _dataSources = new HashMap();
045: private String _dbURL;
046: private String _username = "";
047: private String _password = "";
048: /** An alternate character set to use. */
049: private String charSet;
050:
051: /**
052: * Creates a new JDBCConnectionFactory object from a specified database URL.
053: This is the only constructor supported since there is significant variability in implementation syntax
054: <br/>
055: * <br/>
056: * jdbc:mysql://<host>:<port>/<instance>
057: * @param url the JDBC database URL
058: */
059: public JDBCConnectionFactory(String url, String driver) {
060: _driver = driver;
061: _dbURL = url;
062: }
063:
064: /**
065: * Creates and returns a JDBC ConnectionPool, or gets an existing ConnectionPool
066: * if one exists, based upon the username and password parameters passed to this
067: * method. This is shorthand for the following two calls:<br>
068: * <br>
069: * connPool.setLogin(username, password);<br>
070: * connPool.getConnectionPool();<br>
071: * @param username the JDBC username
072: * @param password the password corresponding to <code>username</code>
073: * @return a JDBC ConnectionPool object
074: * @throws SQLException if an error occurs connecting to the JDBC database
075: */
076: public ConnectionPool getConnectionPool(String username,
077: String password) throws SQLException {
078: setLogin(username, password);
079: return getConnectionPool();
080: }
081:
082: /**
083: * Creates a database connection method to initialize a given database for
084: * feature extraction with the user and password params.
085: *
086: * @param user the name of the user connect to connect to the db.
087: * @param password the password for the user.
088: *
089: * @return the sql Connection object to the database.
090: *
091: * @throws SQLException if the configured sql driver could not be found
092: */
093: public Connection getConnection(String user, String password)
094: throws SQLException {
095: Properties props = new Properties();
096: props.put("user", user);
097: props.put("password", password);
098:
099: if (charSet != null) {
100: props.put("charSet", charSet);
101: }
102:
103: return getConnection(props);
104: }
105:
106: /**
107: * Creates a database connection method to initialize a given database for
108: * feature extraction with the given Properties.
109: *
110: * @param props Should contain at a minimum the user and password.
111: * Additional properties, such as charSet, can also be added.
112: *
113: * @return the sql Connection object to the database.
114: *
115: * @throws SQLException if the postgis sql driver could not be found
116: */
117: public Connection getConnection(Properties props)
118: throws SQLException {
119: // makes a new feature type bean to deal with incoming
120: Connection dbConnection = null;
121:
122: // dbConnection = getConnectionPool().getConnection();
123:
124: // Instantiate the driver classes
125: try {
126: Class.forName("com.mysql.jdbc.Driver");
127: LOGGER.fine("getting connection at " + _dbURL + " using "
128: + _driver + " with props: " + props);
129: dbConnection = DriverManager.getConnection(_dbURL, props);
130: } catch (ClassNotFoundException cnfe) {
131: throw new SQLException("JDBC driver (" + _driver
132: + " was not found.");
133: }
134:
135: return dbConnection;
136: }
137:
138: /**
139: * Creates and returns a ConnectionPool, or gets an existing ConnectionPool
140: * if one exists, based upon the username and password set in this JDBCConnectionFactory
141: * object. Please call setLogin before calling this method, or use getConnectionPool(String, String)
142: * instead.
143: * @return a ConnectionPool object
144: * @throws SQLException if an error occurs connecting to the DB
145: */
146: public ConnectionPool getConnectionPool() throws SQLException {
147: String poolKey = _dbURL + _username + _password;
148:
149: ConnectionPoolFacade poolDataSource = (ConnectionPoolFacade) _dataSources
150: .get(poolKey);
151: if (poolDataSource == null) {
152:
153: poolDataSource = new ConnectionPoolFacade(poolKey, _driver);
154:
155: poolDataSource.setURL(_dbURL);
156: poolDataSource.setUser(_username);
157: poolDataSource.setPassword(_password);
158:
159: _dataSources.put(poolKey, poolDataSource);
160:
161: }
162:
163: ConnectionPoolManager manager = ConnectionPoolManager
164: .getInstance();
165: ConnectionPool connectionPool = manager
166: .getConnectionPool(poolDataSource);
167:
168: return connectionPool;
169: }
170:
171: /**
172: * Sets the JDBC database login credentials.
173: * @param username the username
174: * @param password the password
175: */
176: public void setLogin(String username, String password) {
177: _username = username;
178: _password = password;
179: }
180:
181: public void free(ConnectionPool connectionPool) {
182: if (!connectionPool.isClosed()) {
183: connectionPool.close();
184: }
185: ConnectionPoolManager.getInstance().free(connectionPool);
186: }
187:
188: /**
189: * Sets a different character set for the postgis driver to use.
190: *
191: * @param charSet the string of a valid charset name.
192: */
193: public void setCharSet(String charSet) {
194: this.charSet = charSet;
195: }
196:
197: }
|