001: /*
002: * Geotools2 - 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: */
017: package org.geotools.arcsde.pool;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.logging.Logger;
023:
024: import org.geotools.data.DataSourceException;
025:
026: /**
027: * Singleton factory that maintains a single
028: * {@link ArcSDEConnectionPool connection pool} per set of
029: * {@link ArcSDEConnectionConfig connection parameters}.
030: *
031: * @author Gabriel Roldan
032: * @source $URL:
033: * http://svn.geotools.org/geotools/trunk/gt/modules/unsupported/arcsde/datastore/src/main/java/org/geotools/arcsde/pool/ArcSDEConnectionPoolFactory.java $
034: * @version $Id: ArcSDEConnectionPoolFactory.java 24660 2007-03-02 16:10:44Z
035: * saul.farber $
036: */
037: public class ArcSDEConnectionPoolFactory {
038: /** package logger */
039: private static Logger LOGGER = org.geotools.util.logging.Logging
040: .getLogger(ArcSDEConnectionPoolFactory.class.getPackage()
041: .getName());
042:
043: /** singleton pool factory */
044: private static final ArcSDEConnectionPoolFactory singleton = new ArcSDEConnectionPoolFactory();
045:
046: /**
047: * Map{ArcSDEConnectionConfig,ArcSDEConnectionPool} with per config
048: * connection pool
049: */
050: private final Map currentPools = new HashMap();
051:
052: /**
053: * Creates a new SdeConnectionPoolFactory object.
054: */
055: private ArcSDEConnectionPoolFactory() {
056: // intentionally blank
057: }
058:
059: /**
060: * Returns a connection pool factory instance
061: *
062: * @return the connection pool factory singleton
063: */
064: public synchronized static ArcSDEConnectionPoolFactory getInstance() {
065: return singleton;
066: }
067:
068: /**
069: * Creates a connection pool factory for the given connection parameters, or
070: * returns the existing one if there already exists one for that set of
071: * connection params.
072: *
073: * @param config
074: * contains the connection parameters and pool preferences
075: *
076: * @return a pool for the given connection parameters, wether it already
077: * existed or had to be created.
078: *
079: * @throws DataSourceException
080: * if the pool needs but can't be created
081: */
082: public synchronized ArcSDEConnectionPool createPool(
083: ArcSDEConnectionConfig config) throws DataSourceException {
084: ArcSDEConnectionPool pool = (ArcSDEConnectionPool) this .currentPools
085: .get(config);
086:
087: if (pool == null) {
088: // the new pool will be populated with config.minConnections
089: // connections
090: pool = new ArcSDEConnectionPool(config);
091: this .currentPools.put(config, pool);
092: }
093:
094: return pool;
095: }
096:
097: /**
098: * Closes and removes all the existing connection pools
099: */
100: public void clear() {
101: closeAll();
102: this .currentPools.clear();
103: LOGGER.fine("sde connection pools creared");
104: }
105:
106: /**
107: * loses all the available connection pools
108: */
109: private void closeAll() {
110: for (Iterator it = this .currentPools.values().iterator(); it
111: .hasNext();) {
112: ((ArcSDEConnectionPool) it.next()).close();
113: }
114: }
115:
116: /**
117: * Ensures proper closure of connection pools at this object's finalization
118: * stage.
119: */
120: public void finalize() {
121: closeAll();
122: }
123: }
|