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: import org.logicalcobwebs.proxool.resources.ResourceNamesIF;
011:
012: import java.sql.Connection;
013: import java.sql.Driver;
014: import java.sql.DriverManager;
015: import java.sql.DriverPropertyInfo;
016: import java.sql.SQLException;
017: import java.util.Properties;
018: import java.util.ResourceBundle;
019:
020: /**
021: * This is the Proxool implementation of the java.sql.Driver interface.
022: * @version $Revision: 1.28 $, $Date: 2006/01/18 14:40:01 $
023: * @author billhorsman
024: * @author $Author: billhorsman $ (current maintainer)
025: */
026: public class ProxoolDriver implements Driver {
027:
028: private static final Log LOG = LogFactory
029: .getLog(ProxoolDriver.class);
030:
031: static {
032: try {
033: DriverManager.registerDriver(new ProxoolDriver());
034: } catch (SQLException e) {
035: System.out.println(e.toString());
036: }
037: }
038:
039: private static final ResourceBundle ATTRIBUTE_DESCRIPTIONS_RESOURCE = createAttributeDescriptionsResource();
040:
041: private static ResourceBundle createAttributeDescriptionsResource() {
042: try {
043: return ResourceBundle
044: .getBundle(ResourceNamesIF.ATTRIBUTE_DESCRIPTIONS);
045: } catch (Exception e) {
046: LOG.error("Could not find resource "
047: + ResourceNamesIF.ATTRIBUTE_DESCRIPTIONS, e);
048: }
049: return null;
050: }
051:
052: /**
053: * The url should be of the form:
054: * <pre>
055: * proxool:delegate-class:delegate-url
056: * </pre>
057: * or,
058: * <pre>
059: * proxool.name:delegate-class:delegate-url
060: * </pre>
061: * where <pre>delegate-class</pre> is the actual Driver that will be used and
062: * <pre>delegate-url</pre> is the url that will be based to that Driver
063: *
064: * By defining <pre>name</pre> you are able to define multiple connection pools
065: * even if the delegate url is the same. The entire url (including the proxool.name) is
066: * used to uniquely identify this pool.
067: *
068: */
069: public Connection connect(String url, Properties info)
070: throws SQLException {
071: if (!url.startsWith("proxool")) {
072: return null;
073: }
074:
075: ConnectionPool cp = null;
076: try {
077: String alias = ProxoolFacade.getAlias(url);
078:
079: if (!ConnectionPoolManager.getInstance()
080: .isPoolExists(alias)) {
081: ProxoolFacade.registerConnectionPool(url, info, false);
082: cp = ConnectionPoolManager.getInstance()
083: .getConnectionPool(alias);
084: } else if (info != null && info.size() > 0) {
085: // Perhaps we should be redefining the definition?
086: cp = ConnectionPoolManager.getInstance()
087: .getConnectionPool(alias);
088: ConnectionPoolDefinition cpd = cp.getDefinition();
089: if (!cpd.isEqual(url, info)) {
090: cpd.redefine(url, info);
091: }
092: } else {
093: cp = ConnectionPoolManager.getInstance()
094: .getConnectionPool(alias);
095: }
096: return cp.getConnection();
097:
098: } catch (SQLException e) {
099: // We don't log exceptions. Leave that up to the client.
100: // LOG.error("Problem", e);
101: // Check to see if it's fatal. We might need to wrap it up.
102: try {
103: String alias = ProxoolFacade.getAlias(url);
104: cp = ConnectionPoolManager.getInstance()
105: .getConnectionPool(alias);
106: if (FatalSqlExceptionHelper.testException(cp
107: .getDefinition(), e)) {
108: FatalSqlExceptionHelper.throwFatalSQLException(cp
109: .getDefinition()
110: .getFatalSqlExceptionWrapper(), e);
111: }
112: // This bit isn't reached if throwFatalSQLException() above throws another exception
113: throw e;
114: } catch (ProxoolException e1) {
115: LOG.error("Problem", e);
116: throw new SQLException(e.toString());
117: }
118: } catch (ProxoolException e) {
119: LOG.error("Problem", e);
120: throw new SQLException(e.toString());
121: }
122:
123: }
124:
125: /**
126: * @see Driver#acceptsURL
127: */
128: public boolean acceptsURL(String url) throws SQLException {
129: return (url.startsWith("proxool"));
130: }
131:
132: /**
133: * @see Driver#getPropertyInfo
134: */
135: public DriverPropertyInfo[] getPropertyInfo(String url,
136: Properties info) throws SQLException {
137:
138: DriverPropertyInfo[] dpi = new DriverPropertyInfo[18];
139: ConnectionPool cp = null;
140: try {
141: cp = ConnectionPoolManager.getInstance().getConnectionPool(
142: url);
143: } catch (ProxoolException e) {
144: throw new SQLException(e.toString());
145: }
146:
147: ConnectionPoolDefinitionIF cpd = cp.getDefinition();
148:
149: dpi[0] = buildDriverPropertyInfo(
150: ProxoolConstants.DELEGATE_DRIVER_PROPERTY, String
151: .valueOf(cpd.getDriver()));
152:
153: dpi[1] = buildDriverPropertyInfo(
154: ProxoolConstants.DELEGATE_URL_PROPERTY, String
155: .valueOf(cpd.getUrl()));
156:
157: dpi[2] = buildDriverPropertyInfo(
158: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
159: String.valueOf(cpd.getMinimumConnectionCount()));
160:
161: dpi[3] = buildDriverPropertyInfo(
162: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
163: String.valueOf(cpd.getMaximumConnectionCount()));
164:
165: dpi[4] = buildDriverPropertyInfo(
166: ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME_PROPERTY,
167: String.valueOf(cpd.getMaximumConnectionLifetime()));
168:
169: dpi[5] = buildDriverPropertyInfo(
170: ProxoolConstants.MAXIMUM_NEW_CONNECTIONS_PROPERTY,
171: String.valueOf(cpd.getMaximumNewConnections()));
172:
173: dpi[6] = buildDriverPropertyInfo(
174: ProxoolConstants.PROTOTYPE_COUNT_PROPERTY, String
175: .valueOf(cpd.getPrototypeCount()));
176:
177: dpi[7] = buildDriverPropertyInfo(
178: ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY,
179: String.valueOf(cpd.getHouseKeepingSleepTime()));
180:
181: dpi[8] = buildDriverPropertyInfo(
182: ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, cpd
183: .getHouseKeepingTestSql());
184:
185: dpi[9] = buildDriverPropertyInfo(
186: ProxoolConstants.RECENTLY_STARTED_THRESHOLD_PROPERTY,
187: String.valueOf(cpd.getRecentlyStartedThreshold()));
188:
189: dpi[10] = buildDriverPropertyInfo(
190: ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY,
191: String.valueOf(cpd.getOverloadWithoutRefusalLifetime()));
192:
193: dpi[11] = buildDriverPropertyInfo(
194: ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, String
195: .valueOf(cpd.getMaximumActiveTime()));
196:
197: dpi[12] = buildDriverPropertyInfo(
198: ProxoolConstants.VERBOSE_PROPERTY, String.valueOf(cpd
199: .isVerbose()));
200:
201: dpi[13] = buildDriverPropertyInfo(
202: ProxoolConstants.TRACE_PROPERTY, String.valueOf(cpd
203: .isTrace()));
204:
205: dpi[14] = buildDriverPropertyInfo(
206: ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, String
207: .valueOf(cpd.getFatalSqlExceptions()));
208:
209: dpi[15] = buildDriverPropertyInfo(
210: ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, String
211: .valueOf(cpd.getFatalSqlExceptions()));
212:
213: dpi[16] = buildDriverPropertyInfo(
214: ProxoolConstants.STATISTICS_PROPERTY, String
215: .valueOf(cpd.getStatistics()));
216:
217: dpi[17] = buildDriverPropertyInfo(
218: ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY, String
219: .valueOf(cpd.getStatisticsLogLevel()));
220:
221: return dpi;
222: }
223:
224: private DriverPropertyInfo buildDriverPropertyInfo(
225: String propertyName, String value) {
226: DriverPropertyInfo dpi = new DriverPropertyInfo(propertyName,
227: ATTRIBUTE_DESCRIPTIONS_RESOURCE.getString(propertyName));
228: if (value != null) {
229: dpi.value = value;
230: }
231: return dpi;
232: }
233:
234: /**
235: * @see Driver#getMajorVersion
236: */
237: public int getMajorVersion() {
238: return 1;
239: }
240:
241: /**
242: * @see Driver#getMinorVersion
243: */
244: public int getMinorVersion() {
245: return 0;
246: }
247:
248: /**
249: * @see Driver#jdbcCompliant
250: */
251: public boolean jdbcCompliant() {
252: return true;
253: }
254:
255: }
256:
257: /*
258: Revision history:
259: $Log: ProxoolDriver.java,v $
260: Revision 1.28 2006/01/18 14:40:01 billhorsman
261: Unbundled Jakarta's Commons Logging.
262:
263: Revision 1.27 2004/06/02 20:41:13 billhorsman
264: Don't log SQLExceptions. Leave that up to the client.
265:
266: Revision 1.26 2003/10/16 18:53:21 billhorsman
267: When registering a new pool on the fly, indicate that it is implicit (for exception message handling)
268:
269: Revision 1.25 2003/09/30 18:39:08 billhorsman
270: New test-before-use, test-after-use and fatal-sql-exception-wrapper-class properties.
271:
272: Revision 1.24 2003/09/05 16:59:42 billhorsman
273: Added wrap-fatal-sql-exceptions property
274:
275: Revision 1.23 2003/08/15 10:13:24 billhorsman
276: remove finalize() method
277:
278: Revision 1.22 2003/04/19 12:58:40 billhorsman
279: fixed bug where ConfigurationListener's
280: definitionUpdated was getting called too
281: frequently
282:
283: Revision 1.21 2003/03/10 23:43:12 billhorsman
284: reapplied checkstyle that i'd inadvertently let
285: IntelliJ change...
286:
287: Revision 1.20 2003/03/10 15:26:48 billhorsman
288: refactoringn of concurrency stuff (and some import
289: optimisation)
290:
291: Revision 1.19 2003/03/03 11:11:58 billhorsman
292: fixed licence
293:
294: Revision 1.18 2003/02/26 23:59:37 billhorsman
295: accept now accepts just proxool not proxool:
296:
297: Revision 1.17 2003/02/26 16:05:52 billhorsman
298: widespread changes caused by refactoring the way we
299: update and redefine pool definitions.
300:
301: Revision 1.16 2003/02/07 10:12:59 billhorsman
302: changed updatePoolByDriver sig.
303:
304: Revision 1.15 2003/02/06 17:41:04 billhorsman
305: now uses imported logging
306:
307: Revision 1.14 2003/02/06 15:41:16 billhorsman
308: add statistics-log-level
309:
310: Revision 1.13 2003/01/31 00:28:38 billhorsman
311: updated doc for statistics
312:
313: Revision 1.12 2003/01/30 17:22:01 billhorsman
314: new statistics property
315:
316: Revision 1.11 2003/01/18 15:13:11 billhorsman
317: Signature changes (new ProxoolException
318: thrown) on the ProxoolFacade API.
319:
320: Revision 1.10 2003/01/17 00:38:12 billhorsman
321: wide ranging changes to clarify use of alias and url -
322: this has led to some signature changes (new exceptions
323: thrown) on the ProxoolFacade API.
324:
325: Revision 1.9 2002/12/11 01:48:41 billhorsman
326: added default values for property info documentation
327:
328: Revision 1.8 2002/12/04 13:19:43 billhorsman
329: draft ConfigurationListenerIF stuff for persistent configuration
330:
331: Revision 1.7 2002/12/03 00:41:56 billhorsman
332: fixed getPropertyInfo() for TRACE property and better explanation of FATAL_SQL_EXCEPTION
333:
334: Revision 1.6 2002/11/09 15:55:42 billhorsman
335: added propertyInfo for verbose
336:
337: Revision 1.5 2002/10/27 13:29:38 billhorsman
338: deprecated debug-level in favour of verbose
339:
340: Revision 1.4 2002/10/23 21:04:36 billhorsman
341: checkstyle fixes (reduced max line width and lenient naming convention
342:
343: Revision 1.3 2002/10/17 15:25:37 billhorsman
344: use the url when updating, not the name
345:
346: Revision 1.2 2002/09/18 13:48:56 billhorsman
347: checkstyle and doc
348:
349: Revision 1.1.1.1 2002/09/13 08:13:09 billhorsman
350: new
351:
352: Revision 1.13 2002/07/10 16:14:47 billhorsman
353: widespread layout changes and move constants into ProxoolConstants
354:
355: Revision 1.12 2002/07/04 09:10:48 billhorsman
356: update definition changes
357:
358: Revision 1.11 2002/07/03 10:16:20 billhorsman
359: autoFlush and configuration for logging
360:
361: Revision 1.10 2002/07/02 11:19:08 billhorsman
362: layout code and imports
363:
364: Revision 1.9 2002/07/02 11:14:26 billhorsman
365: added test (andbug fixes) for FileLogger
366:
367: Revision 1.8 2002/07/02 09:05:25 billhorsman
368: All properties now start with "proxool." to avoid possible confusion with delegate driver's properties
369:
370: Revision 1.7 2002/07/02 08:50:33 billhorsman
371: Responsibility for configuring pool is now simplifed and moved to here
372:
373: Revision 1.6 2002/06/28 11:19:47 billhorsman
374: improved doc
375:
376: */
|