KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > ConnectionPoolManager


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

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 JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 /**
18  *
19  * @version $Revision: 1.16 $, $Date: 2006/01/18 14:40:01 $
20  * @author billhorsman
21  * @author $Author: billhorsman $ (current maintainer)
22  */

23 class ConnectionPoolManager {
24     private static final Object JavaDoc LOCK = new Object JavaDoc();
25
26     private Map JavaDoc connectionPoolMap = new HashMap JavaDoc();
27
28     private Set JavaDoc connectionPools = new HashSet JavaDoc();
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     /**
49      * Get the pool by the alias
50      * @param alias identifies the pool
51      * @return the pool
52      * @throws ProxoolException if it couldn't be found
53      */

54     protected ConnectionPool getConnectionPool(String JavaDoc 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     /**
63      * Convenient method for outputing a message explaining that a pool couldn't
64      * be found and listing the ones that could be found.
65      * @param alias identifies the pool
66      * @return a description of the wht the pool couldn't be found
67      */

68     protected String JavaDoc getKnownPools(String JavaDoc alias) {
69         StringBuffer JavaDoc message = new StringBuffer JavaDoc("Couldn't find a pool called '" + alias + "'. Known pools are: ");
70         Iterator JavaDoc i = connectionPoolMap.keySet().iterator();
71         while (i.hasNext()) {
72             message.append((String JavaDoc) i.next());
73             message.append(i.hasNext() ? ", " : ".");
74         }
75         return message.toString();
76     }
77
78     /**
79      * Whether the pool is already registered
80      * @param alias how we identify the pool
81      * @return true if it already exists, else false
82      */

83     protected boolean isPoolExists(String JavaDoc alias) {
84         return connectionPoolMap.containsKey(alias);
85     }
86
87     /** @return an array of the connection pools */
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 JavaDoc 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 JavaDoc[] getConnectionPoolNames() {
110         return (String JavaDoc[]) connectionPoolMap.keySet().toArray(new String JavaDoc[connectionPoolMap.size()]);
111     }
112 }
113
114 /*
115  Revision history:
116  $Log: ConnectionPoolManager.java,v $
117  Revision 1.16 2006/01/18 14:40:01 billhorsman
118  Unbundled Jakarta's Commons Logging.
119
120  Revision 1.15 2003/03/11 14:51:51 billhorsman
121  more concurrency fixes relating to snapshots
122
123  Revision 1.14 2003/03/10 23:43:09 billhorsman
124  reapplied checkstyle that i'd inadvertently let
125  IntelliJ change...
126
127  Revision 1.13 2003/03/10 15:26:45 billhorsman
128  refactoringn of concurrency stuff (and some import
129  optimisation)
130
131  Revision 1.12 2003/03/03 11:11:57 billhorsman
132  fixed licence
133
134  Revision 1.11 2003/03/01 15:27:24 billhorsman
135  checkstyle
136
137  Revision 1.10 2003/02/28 10:42:59 billhorsman
138  ConnectionPoolManager now passes ProxoolFacade an
139  array of ConnectionPools rather than a Collection
140  to avoid a ConcurrentModificationException during
141  shutdown.
142
143  Revision 1.9 2003/02/07 10:27:47 billhorsman
144  change in shutdown procedure to allow re-registration
145
146  Revision 1.8 2003/02/06 17:41:04 billhorsman
147  now uses imported logging
148
149  Revision 1.7 2003/01/27 18:26:36 billhorsman
150  refactoring of ProxyConnection and ProxyStatement to
151  make it easier to write JDK 1.2 patch
152
153  Revision 1.6 2003/01/23 11:08:26 billhorsman
154  new setConfiguratorListener method (and remove from optional
155  parameter when registering pool)
156
157  Revision 1.5 2003/01/17 00:38:12 billhorsman
158  wide ranging changes to clarify use of alias and url -
159  this has led to some signature changes (new exceptions
160  thrown) on the ProxoolFacade API.
161
162  Revision 1.4 2002/12/15 19:21:42 chr32
163  Changed @linkplain to @link (to preserve JavaDoc for 1.2/1.3 users).
164
165  Revision 1.3 2002/11/09 15:49:36 billhorsman
166  add method to get the name of every pool
167
168  Revision 1.2 2002/10/13 13:39:03 billhorsman
169  fix when removing pools (credit to Dan Milstein)
170
171  Revision 1.1.1.1 2002/09/13 08:13:04 billhorsman
172  new
173
174  Revision 1.8 2002/07/10 16:14:47 billhorsman
175  widespread layout changes and move constants into ProxoolConstants
176
177  Revision 1.7 2002/07/04 09:05:36 billhorsman
178  Fixes
179
180  Revision 1.6 2002/07/02 11:19:08 billhorsman
181  layout code and imports
182
183  Revision 1.5 2002/07/02 08:47:31 billhorsman
184  you can now access a pool by alias or full url
185
186  Revision 1.4 2002/06/28 11:19:47 billhorsman
187  improved doc
188
189 */

190
Popular Tags