001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.util.HashMap;
012: import java.util.HashSet;
013: import java.util.Iterator;
014: import java.util.Map;
015: import java.util.Set;
016:
017: /**
018: *
019: * @version $Revision: 1.16 $, $Date: 2006/01/18 14:40:01 $
020: * @author billhorsman
021: * @author $Author: billhorsman $ (current maintainer)
022: */
023: class ConnectionPoolManager {
024: private static final Object LOCK = new Object();
025:
026: private Map connectionPoolMap = new HashMap();
027:
028: private Set connectionPools = new HashSet();
029:
030: private static ConnectionPoolManager connectionPoolManager = null;
031:
032: private static final Log LOG = LogFactory
033: .getLog(ProxoolFacade.class);
034:
035: public static ConnectionPoolManager getInstance() {
036: if (connectionPoolManager == null) {
037: synchronized (LOCK) {
038: if (connectionPoolManager == null) {
039: connectionPoolManager = new ConnectionPoolManager();
040: }
041: }
042: }
043: return connectionPoolManager;
044: }
045:
046: private ConnectionPoolManager() {
047: }
048:
049: /**
050: * Get the pool by the alias
051: * @param alias identifies the pool
052: * @return the pool
053: * @throws ProxoolException if it couldn't be found
054: */
055: protected ConnectionPool getConnectionPool(String alias)
056: throws ProxoolException {
057: ConnectionPool cp = (ConnectionPool) connectionPoolMap
058: .get(alias);
059: if (cp == null) {
060: throw new ProxoolException(getKnownPools(alias));
061: }
062: return cp;
063: }
064:
065: /**
066: * Convenient method for outputing a message explaining that a pool couldn't
067: * be found and listing the ones that could be found.
068: * @param alias identifies the pool
069: * @return a description of the wht the pool couldn't be found
070: */
071: protected String getKnownPools(String alias) {
072: StringBuffer message = new StringBuffer(
073: "Couldn't find a pool called '" + alias
074: + "'. Known pools are: ");
075: Iterator i = connectionPoolMap.keySet().iterator();
076: while (i.hasNext()) {
077: message.append((String) i.next());
078: message.append(i.hasNext() ? ", " : ".");
079: }
080: return message.toString();
081: }
082:
083: /**
084: * Whether the pool is already registered
085: * @param alias how we identify the pool
086: * @return true if it already exists, else false
087: */
088: protected boolean isPoolExists(String alias) {
089: return connectionPoolMap.containsKey(alias);
090: }
091:
092: /** @return an array of the connection pools */
093: protected ConnectionPool[] getConnectionPools() {
094: return (ConnectionPool[]) connectionPools
095: .toArray(new ConnectionPool[connectionPools.size()]);
096: }
097:
098: protected ConnectionPool createConnectionPool(
099: ConnectionPoolDefinition connectionPoolDefinition)
100: throws ProxoolException {
101: ConnectionPool connectionPool = new ConnectionPool(
102: connectionPoolDefinition);
103: connectionPools.add(connectionPool);
104: connectionPoolMap.put(connectionPoolDefinition.getAlias(),
105: connectionPool);
106: return connectionPool;
107: }
108:
109: protected void removeConnectionPool(String name) {
110: ConnectionPool cp = (ConnectionPool) connectionPoolMap
111: .get(name);
112: if (cp != null) {
113: connectionPoolMap.remove(cp.getDefinition().getAlias());
114: connectionPools.remove(cp);
115: } else {
116: LOG
117: .info("Ignored attempt to remove either non-existent or already removed connection pool "
118: + name);
119: }
120: }
121:
122: public String[] getConnectionPoolNames() {
123: return (String[]) connectionPoolMap.keySet().toArray(
124: new String[connectionPoolMap.size()]);
125: }
126: }
127:
128: /*
129: Revision history:
130: $Log: ConnectionPoolManager.java,v $
131: Revision 1.16 2006/01/18 14:40:01 billhorsman
132: Unbundled Jakarta's Commons Logging.
133:
134: Revision 1.15 2003/03/11 14:51:51 billhorsman
135: more concurrency fixes relating to snapshots
136:
137: Revision 1.14 2003/03/10 23:43:09 billhorsman
138: reapplied checkstyle that i'd inadvertently let
139: IntelliJ change...
140:
141: Revision 1.13 2003/03/10 15:26:45 billhorsman
142: refactoringn of concurrency stuff (and some import
143: optimisation)
144:
145: Revision 1.12 2003/03/03 11:11:57 billhorsman
146: fixed licence
147:
148: Revision 1.11 2003/03/01 15:27:24 billhorsman
149: checkstyle
150:
151: Revision 1.10 2003/02/28 10:42:59 billhorsman
152: ConnectionPoolManager now passes ProxoolFacade an
153: array of ConnectionPools rather than a Collection
154: to avoid a ConcurrentModificationException during
155: shutdown.
156:
157: Revision 1.9 2003/02/07 10:27:47 billhorsman
158: change in shutdown procedure to allow re-registration
159:
160: Revision 1.8 2003/02/06 17:41:04 billhorsman
161: now uses imported logging
162:
163: Revision 1.7 2003/01/27 18:26:36 billhorsman
164: refactoring of ProxyConnection and ProxyStatement to
165: make it easier to write JDK 1.2 patch
166:
167: Revision 1.6 2003/01/23 11:08:26 billhorsman
168: new setConfiguratorListener method (and remove from optional
169: parameter when registering pool)
170:
171: Revision 1.5 2003/01/17 00:38:12 billhorsman
172: wide ranging changes to clarify use of alias and url -
173: this has led to some signature changes (new exceptions
174: thrown) on the ProxoolFacade API.
175:
176: Revision 1.4 2002/12/15 19:21:42 chr32
177: Changed @linkplain to @link (to preserve JavaDoc for 1.2/1.3 users).
178:
179: Revision 1.3 2002/11/09 15:49:36 billhorsman
180: add method to get the name of every pool
181:
182: Revision 1.2 2002/10/13 13:39:03 billhorsman
183: fix when removing pools (credit to Dan Milstein)
184:
185: Revision 1.1.1.1 2002/09/13 08:13:04 billhorsman
186: new
187:
188: Revision 1.8 2002/07/10 16:14:47 billhorsman
189: widespread layout changes and move constants into ProxoolConstants
190:
191: Revision 1.7 2002/07/04 09:05:36 billhorsman
192: Fixes
193:
194: Revision 1.6 2002/07/02 11:19:08 billhorsman
195: layout code and imports
196:
197: Revision 1.5 2002/07/02 08:47:31 billhorsman
198: you can now access a pool by alias or full url
199:
200: Revision 1.4 2002/06/28 11:19:47 billhorsman
201: improved doc
202:
203: */
|