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