1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 23 class ConnectionPoolManager { 24 private static final Object LOCK = new Object (); 25 26 private Map connectionPoolMap = new HashMap (); 27 28 private Set connectionPools = new HashSet (); 29 30 private static ConnectionPoolManager connectionPoolManager = null; 31 32 private static final Log LOG = LogFactory.getLog(ProxoolFacade.class); 33 34 public static ConnectionPoolManager getInstance() { 35 if (connectionPoolManager == null) { 36 synchronized (LOCK) { 37 if (connectionPoolManager == null) { 38 connectionPoolManager = new ConnectionPoolManager(); 39 } 40 } 41 } 42 return connectionPoolManager; 43 } 44 45 private ConnectionPoolManager() { 46 } 47 48 54 protected ConnectionPool getConnectionPool(String alias) throws ProxoolException { 55 ConnectionPool cp = (ConnectionPool) connectionPoolMap.get(alias); 56 if (cp == null) { 57 throw new ProxoolException(getKnownPools(alias)); 58 } 59 return cp; 60 } 61 62 68 protected String getKnownPools(String alias) { 69 StringBuffer message = new StringBuffer ("Couldn't find a pool called '" + alias + "'. Known pools are: "); 70 Iterator i = connectionPoolMap.keySet().iterator(); 71 while (i.hasNext()) { 72 message.append((String ) i.next()); 73 message.append(i.hasNext() ? ", " : "."); 74 } 75 return message.toString(); 76 } 77 78 83 protected boolean isPoolExists(String alias) { 84 return connectionPoolMap.containsKey(alias); 85 } 86 87 88 protected ConnectionPool[] getConnectionPools() { 89 return (ConnectionPool[]) connectionPools.toArray(new ConnectionPool[connectionPools.size()]); 90 } 91 92 protected ConnectionPool createConnectionPool(ConnectionPoolDefinition connectionPoolDefinition) throws ProxoolException { 93 ConnectionPool connectionPool = new ConnectionPool(connectionPoolDefinition); 94 connectionPools.add(connectionPool); 95 connectionPoolMap.put(connectionPoolDefinition.getAlias(), connectionPool); 96 return connectionPool; 97 } 98 99 protected void removeConnectionPool(String name) { 100 ConnectionPool cp = (ConnectionPool) connectionPoolMap.get(name); 101 if (cp != null) { 102 connectionPoolMap.remove(cp.getDefinition().getAlias()); 103 connectionPools.remove(cp); 104 } else { 105 LOG.info("Ignored attempt to remove either non-existent or already removed connection pool " + name); 106 } 107 } 108 109 public String [] getConnectionPoolNames() { 110 return (String []) connectionPoolMap.keySet().toArray(new String [connectionPoolMap.size()]); 111 } 112 } 113 114 190 | Popular Tags |