KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > jdbc > pool > JdbcConnectionPoolDriver


1 package com.protomatter.jdbc.pool;
2 import com.protomatter.syslog.Channel;
3
4 import com.protomatter.util.Debug;
5
6 /**
7  * {{{ The Protomatter Software License, Version 1.0
8  * derived from The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer.
18  *
19  * 2. Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in
21  * the documentation and/or other materials provided with the
22  * distribution.
23  *
24  * 3. The end-user documentation included with the redistribution,
25  * if any, must include the following acknowledgment:
26  * "This product includes software developed for the
27  * Protomatter Software Project
28  * (http://protomatter.sourceforge.net/)."
29  * Alternately, this acknowledgment may appear in the software itself,
30  * if and wherever such third-party acknowledgments normally appear.
31  *
32  * 4. The names "Protomatter" and "Protomatter Software Project" must
33  * not be used to endorse or promote products derived from this
34  * software without prior written permission. For written
35  * permission, please contact support@protomatter.com.
36  *
37  * 5. Products derived from this software may not be called "Protomatter",
38  * nor may "Protomatter" appear in their name, without prior written
39  * permission of the Protomatter Software Project
40  * (support@protomatter.com).
41  *
42  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
43  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
46  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
49  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE. }}}
54  */

55
56 import java.sql.*;
57 import java.text.*;
58 import java.util.*;
59 import com.protomatter.syslog.Channel;
60 import com.protomatter.util.Debug;
61
62 /**
63  * The driver for use with JDBC connection pools. When this class is loaded, it
64  * registers itself with the DriverManager. This driver accepts JDBC connection
65  * URLs of the form:<P>
66  *
67  *
68  * <ul><tt>jdbc:protomatter:pool:<i>PoolName</i> </tt>
69  * </ul>
70  * <P>
71  *
72  * This class also keeps a static reference to the list of all known connection
73  * pools, which is updated by creating a new JdbcConnectionPool object. Pools
74  * can be un-registered by calling <tt>unRegisterPool()</tt> on the
75  * JdbcConnectionPool object.
76  *
77  *@author nate
78  *@created October 19, 2002
79  *@see java.sql.DriverManager
80  *@see JdbcConnectionPool
81  *@see JdbcConnectionPoolConnection
82  */

83 public class JdbcConnectionPoolDriver
84 implements Driver
85 {
86     private static Map pools = new HashMap();
87     private static int MAJOR_VERSION = 1;
88     private static int MINOR_VERSION = 1;
89
90     protected static Debug DEBUG = Debug.forPackage(JdbcConnectionPoolDriver.class);
91     protected static Channel log = Channel.forPackage(JdbcConnectionPoolDriver.class);
92
93
94     /**
95      * Default constructor.
96      */

97     public JdbcConnectionPoolDriver()
98     {
99         super();
100     }
101
102     /**
103      * Static initializer. When this class is loaded, it registers itself with
104      * the JDBC driver manager.
105      */

106     static
107     {
108         // register ourselves on load with the driver manager.
109
try
110         {
111             if (DEBUG.debug())
112             {
113                 log.debug(JdbcConnectionPoolDriver.class, "Registering JDBC driver");
114             }
115             Driver driver = new JdbcConnectionPoolDriver();
116             DriverManager.registerDriver(driver);
117         }
118         catch (SQLException x)
119         {
120             log.error(JdbcConnectionPoolDriver.class, "Cannot register JDBC driver", x);
121         }
122     }
123
124
125     static int countRegisteredPools()
126     {
127         return pools.size();
128     }
129
130
131     static void registerPool(JdbcConnectionPool pool)
132     {
133         if (DEBUG.debug())
134             log.debug(JdbcConnectionPoolDriver.class, "Registering pool: " + pool);
135         pools.put(pool.getName(), pool);
136     }
137
138
139     static void unRegisterPool(JdbcConnectionPool pool)
140     {
141         if (DEBUG.debug())
142             log.debug(JdbcConnectionPoolDriver.class, "Un-registering pool: " + pool);
143         pools.remove(pool.getName());
144     }
145
146
147     /**
148      * Get one of the currently registered JDBC Connection Pools.
149      *
150      *@param poolName Description of the Parameter
151      *@return The pool value
152      */

153     public static JdbcConnectionPool getPool(String JavaDoc poolName)
154     {
155         return (JdbcConnectionPool)pools.get(poolName);
156     }
157
158
159     /**
160      * Get the list of registered pool names. The returned enumeration contains
161      * strings.
162      *
163      *@return The poolNames value
164      */

165     public static Iterator getPoolNames()
166     {
167         return pools.keySet().iterator();
168     }
169
170
171     /**
172      * Shuts down all connections for all JDBC connection pools. This should
173      * only be used as part of a system shutdown of some kind. All pools are
174      * un-registered when this method is run.
175      *
176      *@see JdbcConnectionPool#closeAllConnections
177      */

178     public static void shutdownAllConnections()
179     {
180         if (DEBUG.debug())
181             log.debug(JdbcConnectionPoolDriver.class, "Shutting down all connections");
182         // lock everyone out first.
183
Map thePools = pools;
184         pools = new HashMap();
185
186         Iterator i = thePools.keySet().iterator();
187         while (i.hasNext())
188         {
189             String JavaDoc poolName = (String JavaDoc)i.next();
190             if (DEBUG.debug())
191                 log.debug(JdbcConnectionPoolDriver.class, "Closing connections on pool " + poolName);
192             JdbcConnectionPool pool = (JdbcConnectionPool)thePools.get(poolName);
193             pool.closeAllConnections();
194         }
195     }
196
197
198     /**
199      * The JDBC URL prefix for making connections. The poolname should be
200      * appended to this string when asking for connections from the
201      * DriverManager.
202      */

203     public static String JavaDoc URL_PREFIX = "jdbc:protomatter:pool:";
204
205
206     /**
207      * Check a connection out of the pool specified in the URL.
208      *
209      *@param url Description of the Parameter
210      *@param props Description of the Parameter
211      *@return Description of the Return Value
212      *@exception SQLException If there is a problem checking a connection out
213      * of the pool.
214      *@see java.sql.Driver
215      */

216     public Connection connect(String JavaDoc url, Properties props)
217         throws SQLException
218     {
219         if (DEBUG.debug())
220             log.debug(this, "connect(" + url + ", " + props + ")");
221
222         if (!url.startsWith(URL_PREFIX))
223             return null;
224
225         String JavaDoc poolName = url.substring(URL_PREFIX.length());
226
227         JdbcConnectionPool pool = (JdbcConnectionPool)pools.get(poolName);
228         if (pool == null)
229         {
230             throw new SQLException(MessageFormat.format(
231                     PoolResources.getResourceString(MessageConstants.UNKNOWN_POOL_MESSAGE),
232                     new Object JavaDoc[]{poolName}));
233         }
234
235         // checkout a connection
236
try
237         {
238             JdbcConnectionPoolConnection c = (JdbcConnectionPoolConnection)pool.checkout();
239             c.setCheckoutStackTrace(new JdbcCheckoutExceptionTrace());
240             c.resetLastTimeUsed();
241             return new ConnectionWrapper(c, pool);
242         }
243         catch (Exception JavaDoc x)
244         {
245             throw new PoolSQLException(MessageFormat.format(
246                     PoolResources.getResourceString(MessageConstants.CANNOT_CHECKOUT_MESSAGE),
247                     new Object JavaDoc[]{pool.getName(), x.toString()}), x);
248         }
249     }
250
251
252     /**
253      *@param url Description of the Parameter
254      *@return Description of the Return Value
255      *@exception SQLException Because java.sql.Driver throws one here.
256      *@see java.sql.Driver
257      */

258     public boolean acceptsURL(String JavaDoc url)
259         throws SQLException
260     {
261         return url.startsWith(URL_PREFIX);
262     }
263
264
265     /**
266      *@param url Description of the Parameter
267      *@param props Description of the Parameter
268      *@return The propertyInfo value
269      *@exception SQLException Because java.sql.Driver throws one here.
270      *@see java.sql.Driver
271      */

272     public DriverPropertyInfo[] getPropertyInfo(String JavaDoc url, Properties props)
273         throws SQLException
274     {
275         return null;
276     }
277
278
279     /**
280      *@return The majorVersion value
281      *@see java.sql.Driver
282      */

283     public int getMajorVersion()
284     {
285         return MAJOR_VERSION;
286     }
287
288
289     /**
290      *@return The minorVersion value
291      *@see java.sql.Driver
292      */

293     public int getMinorVersion()
294     {
295         return MINOR_VERSION;
296     }
297
298
299     /**
300      *@return Description of the Return Value
301      *@see java.sql.Driver
302      */

303     public boolean jdbcCompliant()
304     {
305         return false;
306     }
307 }
308
309
Popular Tags