001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-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: * Created on 15/08/2003
017: */
018: package org.geotools.data.jdbc;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import javax.sql.ConnectionPoolDataSource;
025: import javax.sql.DataSource;
026:
027: import org.geotools.data.jdbc.datasource.DataSourceFinder;
028: import org.geotools.data.jdbc.datasource.DataSourceUtil;
029:
030: /** Provides a Singleton manager of connection pools.
031: *
032: * @author Sean Geoghegan, Defence Science and Technology Organisation
033: * @author $Author: jive $
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/ConnectionPoolManager.java $
035: * @version $Id: ConnectionPoolManager.java 26148 2007-07-04 17:07:11Z aaime $
036: * Last Modified: $Date: 2003/11/21 18:51:20 $
037: * @deprecated Use {@link DataSource}, {@link DataSourceUtil} and {@link DataSourceFinder} instead
038: */
039: public class ConnectionPoolManager {
040: /** The singleton instance of the ConnectionPoolManager. */
041: private static ConnectionPoolManager instance;
042: /** Map containing the connection pools. */
043: private Map connectionPools = new HashMap();
044:
045: /** Private constructor to enforce Singleton
046: *
047: */
048: private ConnectionPoolManager() {
049: }
050:
051: /** Gets the instance of the ConnectionPoolManager.
052: *
053: * @return The one and only instance of ConnectionPoolManager.
054: */
055: public static synchronized ConnectionPoolManager getInstance() {
056: if (instance == null) {
057: instance = new ConnectionPoolManager();
058: }
059:
060: return instance;
061: }
062:
063: /** Gets a ConnectionPool for a ConnectionPoolDataSource.
064: *
065: * <p>This method will return a connection pool that contains
066: * the ConnectionPoolDataSource. If a ConnectionPool exists that
067: * already contains the ConnectionPoolDataSource, it will be returned,
068: * otherwise a ConnectionPool will be created for the ConnectionPoolDataSource.
069: *
070: * @param cpds The ConnectionPoolDataSource to get a ConnectionPool for.
071: * @return The ConnectionPool.
072: */
073: public synchronized ConnectionPool getConnectionPool(
074: final ConnectionPoolDataSource cpds) {
075: ConnectionPool connectionPool = (ConnectionPool) connectionPools
076: .get(cpds);
077:
078: if (connectionPool == null) {
079: connectionPool = new ConnectionPool(cpds);
080: connectionPools.put(cpds, connectionPool);
081: }
082:
083: return connectionPool;
084: }
085:
086: public synchronized void free(ConnectionPool pool) {
087: if (!pool.isClosed()) {
088: pool.close();
089: }
090: connectionPools.values().remove(pool);
091: }
092:
093: public synchronized void closeAll() {
094: for (Iterator iter = connectionPools.values().iterator(); iter
095: .hasNext();) {
096: ConnectionPool pool = (ConnectionPool) iter.next();
097: iter.remove();
098: pool.close();
099: }
100: }
101: }
|