KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.logicalcobwebs.proxool.resources.ResourceNamesIF;
11
12 import java.sql.Connection JavaDoc;
13 import java.sql.Driver JavaDoc;
14 import java.sql.DriverManager JavaDoc;
15 import java.sql.DriverPropertyInfo JavaDoc;
16 import java.sql.SQLException JavaDoc;
17 import java.util.Properties JavaDoc;
18 import java.util.ResourceBundle JavaDoc;
19
20 /**
21  * This is the Proxool implementation of the java.sql.Driver interface.
22  * @version $Revision: 1.28 $, $Date: 2006/01/18 14:40:01 $
23  * @author billhorsman
24  * @author $Author: billhorsman $ (current maintainer)
25  */

26 public class ProxoolDriver implements Driver JavaDoc {
27
28     private static final Log LOG = LogFactory.getLog(ProxoolDriver.class);
29
30     static {
31         try {
32             DriverManager.registerDriver(new ProxoolDriver());
33         } catch (SQLException JavaDoc e) {
34             System.out.println(e.toString());
35         }
36     }
37
38     private static final ResourceBundle JavaDoc ATTRIBUTE_DESCRIPTIONS_RESOURCE = createAttributeDescriptionsResource ();
39
40     private static ResourceBundle JavaDoc createAttributeDescriptionsResource () {
41         try {
42             return ResourceBundle.getBundle (ResourceNamesIF.ATTRIBUTE_DESCRIPTIONS);
43         } catch (Exception JavaDoc e) {
44             LOG.error ("Could not find resource " + ResourceNamesIF.ATTRIBUTE_DESCRIPTIONS, e);
45         }
46         return null;
47     }
48
49     /**
50      * The url should be of the form:
51      * <pre>
52      * proxool:delegate-class:delegate-url
53      * </pre>
54      * or,
55      * <pre>
56      * proxool.name:delegate-class:delegate-url
57      * </pre>
58      * where <pre>delegate-class</pre> is the actual Driver that will be used and
59      * <pre>delegate-url</pre> is the url that will be based to that Driver
60      *
61      * By defining <pre>name</pre> you are able to define multiple connection pools
62      * even if the delegate url is the same. The entire url (including the proxool.name) is
63      * used to uniquely identify this pool.
64      *
65      */

66     public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc info)
67             throws SQLException JavaDoc {
68         if (!url.startsWith("proxool")) {
69             return null;
70         }
71
72         ConnectionPool cp = null;
73         try {
74             String JavaDoc alias = ProxoolFacade.getAlias(url);
75
76             if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) {
77                 ProxoolFacade.registerConnectionPool(url, info, false);
78                 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias);
79             } else if (info != null && info.size() > 0) {
80                 // Perhaps we should be redefining the definition?
81
cp = ConnectionPoolManager.getInstance().getConnectionPool(alias);
82                 ConnectionPoolDefinition cpd = cp.getDefinition();
83                 if (!cpd.isEqual(url, info)) {
84                     cpd.redefine(url, info);
85                 }
86             } else {
87                 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias);
88             }
89             return cp.getConnection();
90
91         } catch (SQLException JavaDoc e) {
92             // We don't log exceptions. Leave that up to the client.
93
// LOG.error("Problem", e);
94
// Check to see if it's fatal. We might need to wrap it up.
95
try {
96                 String JavaDoc alias = ProxoolFacade.getAlias(url);
97                 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias);
98                 if (FatalSqlExceptionHelper.testException(cp.getDefinition(), e)) {
99                     FatalSqlExceptionHelper.throwFatalSQLException(cp.getDefinition().getFatalSqlExceptionWrapper(), e);
100                 }
101                 // This bit isn't reached if throwFatalSQLException() above throws another exception
102
throw e;
103             } catch (ProxoolException e1) {
104                 LOG.error("Problem", e);
105                 throw new SQLException JavaDoc(e.toString());
106             }
107         } catch (ProxoolException e) {
108             LOG.error("Problem", e);
109             throw new SQLException JavaDoc(e.toString());
110         }
111
112     }
113
114     /**
115      * @see Driver#acceptsURL
116      */

117     public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc {
118         return (url.startsWith("proxool"));
119     }
120
121     /**
122      * @see Driver#getPropertyInfo
123      */

124     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info)
125             throws SQLException JavaDoc {
126
127         DriverPropertyInfo JavaDoc[] dpi = new DriverPropertyInfo JavaDoc[18];
128         ConnectionPool cp = null;
129         try {
130             cp = ConnectionPoolManager.getInstance().getConnectionPool(url);
131         } catch (ProxoolException e) {
132             throw new SQLException JavaDoc(e.toString());
133         }
134
135         ConnectionPoolDefinitionIF cpd = cp.getDefinition();
136
137         dpi[0] = buildDriverPropertyInfo(ProxoolConstants.DELEGATE_DRIVER_PROPERTY,
138                 String.valueOf(cpd.getDriver()));
139
140         dpi[1] = buildDriverPropertyInfo(ProxoolConstants.DELEGATE_URL_PROPERTY,
141                 String.valueOf(cpd.getUrl()));
142
143         dpi[2] = buildDriverPropertyInfo(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
144                 String.valueOf(cpd.getMinimumConnectionCount()));
145
146         dpi[3] = buildDriverPropertyInfo(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
147                 String.valueOf(cpd.getMaximumConnectionCount()));
148
149         dpi[4] = buildDriverPropertyInfo(ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME_PROPERTY,
150                 String.valueOf(cpd.getMaximumConnectionLifetime()));
151
152         dpi[5] = buildDriverPropertyInfo(ProxoolConstants.MAXIMUM_NEW_CONNECTIONS_PROPERTY,
153                 String.valueOf(cpd.getMaximumNewConnections()));
154
155         dpi[6] = buildDriverPropertyInfo(ProxoolConstants.PROTOTYPE_COUNT_PROPERTY,
156                 String.valueOf(cpd.getPrototypeCount()));
157
158         dpi[7] = buildDriverPropertyInfo(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY,
159                 String.valueOf(cpd.getHouseKeepingSleepTime()));
160
161         dpi[8] = buildDriverPropertyInfo(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY,
162                 cpd.getHouseKeepingTestSql());
163
164         dpi[9] = buildDriverPropertyInfo(ProxoolConstants.RECENTLY_STARTED_THRESHOLD_PROPERTY,
165                 String.valueOf(cpd.getRecentlyStartedThreshold()));
166
167         dpi[10] = buildDriverPropertyInfo(ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY,
168                 String.valueOf(cpd.getOverloadWithoutRefusalLifetime()));
169
170         dpi[11] = buildDriverPropertyInfo(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY,
171                 String.valueOf(cpd.getMaximumActiveTime()));
172
173         dpi[12] = buildDriverPropertyInfo(ProxoolConstants.VERBOSE_PROPERTY,
174                 String.valueOf(cpd.isVerbose()));
175
176         dpi[13] = buildDriverPropertyInfo(ProxoolConstants.TRACE_PROPERTY,
177                 String.valueOf(cpd.isTrace()));
178
179         dpi[14] = buildDriverPropertyInfo(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY,
180                 String.valueOf(cpd.getFatalSqlExceptions()));
181
182         dpi[15] = buildDriverPropertyInfo(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY,
183                 String.valueOf(cpd.getFatalSqlExceptions()));
184
185         dpi[16] = buildDriverPropertyInfo(ProxoolConstants.STATISTICS_PROPERTY,
186                 String.valueOf(cpd.getStatistics()));
187
188         dpi[17] = buildDriverPropertyInfo(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY,
189                 String.valueOf(cpd.getStatisticsLogLevel()));
190
191         return dpi;
192     }
193
194     private DriverPropertyInfo JavaDoc buildDriverPropertyInfo(String JavaDoc propertyName, String JavaDoc value) {
195         DriverPropertyInfo JavaDoc dpi = new DriverPropertyInfo JavaDoc(propertyName,
196                 ATTRIBUTE_DESCRIPTIONS_RESOURCE.getString (propertyName));
197         if (value != null) {
198             dpi.value = value;
199         }
200         return dpi;
201     }
202
203     /**
204      * @see Driver#getMajorVersion
205      */

206     public int getMajorVersion() {
207         return 1;
208     }
209
210     /**
211      * @see Driver#getMinorVersion
212      */

213     public int getMinorVersion() {
214         return 0;
215     }
216
217     /**
218      * @see Driver#jdbcCompliant
219      */

220     public boolean jdbcCompliant() {
221         return true;
222     }
223
224 }
225
226 /*
227  Revision history:
228  $Log: ProxoolDriver.java,v $
229  Revision 1.28 2006/01/18 14:40:01 billhorsman
230  Unbundled Jakarta's Commons Logging.
231
232  Revision 1.27 2004/06/02 20:41:13 billhorsman
233  Don't log SQLExceptions. Leave that up to the client.
234
235  Revision 1.26 2003/10/16 18:53:21 billhorsman
236  When registering a new pool on the fly, indicate that it is implicit (for exception message handling)
237
238  Revision 1.25 2003/09/30 18:39:08 billhorsman
239  New test-before-use, test-after-use and fatal-sql-exception-wrapper-class properties.
240
241  Revision 1.24 2003/09/05 16:59:42 billhorsman
242  Added wrap-fatal-sql-exceptions property
243
244  Revision 1.23 2003/08/15 10:13:24 billhorsman
245  remove finalize() method
246
247  Revision 1.22 2003/04/19 12:58:40 billhorsman
248  fixed bug where ConfigurationListener's
249  definitionUpdated was getting called too
250  frequently
251
252  Revision 1.21 2003/03/10 23:43:12 billhorsman
253  reapplied checkstyle that i'd inadvertently let
254  IntelliJ change...
255
256  Revision 1.20 2003/03/10 15:26:48 billhorsman
257  refactoringn of concurrency stuff (and some import
258  optimisation)
259
260  Revision 1.19 2003/03/03 11:11:58 billhorsman
261  fixed licence
262
263  Revision 1.18 2003/02/26 23:59:37 billhorsman
264  accept now accepts just proxool not proxool:
265
266  Revision 1.17 2003/02/26 16:05:52 billhorsman
267  widespread changes caused by refactoring the way we
268  update and redefine pool definitions.
269
270  Revision 1.16 2003/02/07 10:12:59 billhorsman
271  changed updatePoolByDriver sig.
272
273  Revision 1.15 2003/02/06 17:41:04 billhorsman
274  now uses imported logging
275
276  Revision 1.14 2003/02/06 15:41:16 billhorsman
277  add statistics-log-level
278
279  Revision 1.13 2003/01/31 00:28:38 billhorsman
280  updated doc for statistics
281
282  Revision 1.12 2003/01/30 17:22:01 billhorsman
283  new statistics property
284
285  Revision 1.11 2003/01/18 15:13:11 billhorsman
286  Signature changes (new ProxoolException
287  thrown) on the ProxoolFacade API.
288
289  Revision 1.10 2003/01/17 00:38:12 billhorsman
290  wide ranging changes to clarify use of alias and url -
291  this has led to some signature changes (new exceptions
292  thrown) on the ProxoolFacade API.
293
294  Revision 1.9 2002/12/11 01:48:41 billhorsman
295  added default values for property info documentation
296
297  Revision 1.8 2002/12/04 13:19:43 billhorsman
298  draft ConfigurationListenerIF stuff for persistent configuration
299
300  Revision 1.7 2002/12/03 00:41:56 billhorsman
301  fixed getPropertyInfo() for TRACE property and better explanation of FATAL_SQL_EXCEPTION
302
303  Revision 1.6 2002/11/09 15:55:42 billhorsman
304  added propertyInfo for verbose
305
306  Revision 1.5 2002/10/27 13:29:38 billhorsman
307  deprecated debug-level in favour of verbose
308
309  Revision 1.4 2002/10/23 21:04:36 billhorsman
310  checkstyle fixes (reduced max line width and lenient naming convention
311
312  Revision 1.3 2002/10/17 15:25:37 billhorsman
313  use the url when updating, not the name
314
315  Revision 1.2 2002/09/18 13:48:56 billhorsman
316  checkstyle and doc
317
318  Revision 1.1.1.1 2002/09/13 08:13:09 billhorsman
319  new
320
321  Revision 1.13 2002/07/10 16:14:47 billhorsman
322  widespread layout changes and move constants into ProxoolConstants
323
324  Revision 1.12 2002/07/04 09:10:48 billhorsman
325  update definition changes
326
327  Revision 1.11 2002/07/03 10:16:20 billhorsman
328  autoFlush and configuration for logging
329
330  Revision 1.10 2002/07/02 11:19:08 billhorsman
331  layout code and imports
332
333  Revision 1.9 2002/07/02 11:14:26 billhorsman
334  added test (andbug fixes) for FileLogger
335
336  Revision 1.8 2002/07/02 09:05:25 billhorsman
337  All properties now start with "proxool." to avoid possible confusion with delegate driver's properties
338
339  Revision 1.7 2002/07/02 08:50:33 billhorsman
340  Responsibility for configuring pool is now simplifed and moved to here
341
342  Revision 1.6 2002/06/28 11:19:47 billhorsman
343  improved doc
344
345 */

346
Popular Tags