001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.jdbc;
020:
021: import java.sql.*;
022: import java.util.Properties;
023:
024: /**
025: * A mandarax jdbc driver. Only SELECT statements are supported, this driver
026: * is not jdbc compliant!
027: * <p>
028: * Loading this class with Class.forName will try to locate the server and the client delegates,
029: * and load them if this succeeds.
030: * This class is usually not instanciated!
031: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
032: * @version 3.3.2 <29 December 2004>
033: * @since 3.0
034: */
035:
036: public class DriverImpl extends AbstractDriverImpl {
037: // delegate driver names
038: public static final String CLIENT_DRIVER = "org.mandarax.jdbc.client.ClientDriverImpl";
039: public static final String SERVER_DRIVER = "org.mandarax.jdbc.server.ServerDriverImpl";
040: private static Driver clientDriver = null;
041: private static Driver serverDriver = null;
042:
043: // try to load the client driver
044: static Driver loadClientDriver() {
045: try {
046: clientDriver = (Driver) Class.forName(CLIENT_DRIVER)
047: .newInstance();
048: } catch (Exception x) {
049: LOG_JDBC
050: .info(
051: "Cannot load client side mandarax jdbc driver (this is ok if this is a server application)",
052: x);
053: }
054: return null;
055: }
056:
057: // try to load the server driver
058: static Driver loadServerDriver() {
059: try {
060: serverDriver = (Driver) Class.forName(SERVER_DRIVER)
061: .newInstance();
062: } catch (Exception x) {
063: LOG_JDBC
064: .info(
065: "Cannot load server side mandarax jdbc driver (this is ok if this is a client application)",
066: x);
067: }
068: return null;
069: }
070:
071: /**
072: * Instanciate (and load) the driver in static initializer.
073: */
074: static {
075: loadClientDriver();
076: loadServerDriver();
077: }
078:
079: /**
080: * Constructor.
081: * usually, this class is not in instanciated!
082: */
083: public DriverImpl() {
084: super ();
085: }
086:
087: /**
088: * Get a connection.
089: * If driver is loaded using Class.forName(), this method will never be called!
090: * @param url the url
091: * @param info additional connect info
092: */
093: public Connection connect(String url, Properties info)
094: throws SQLException {
095: if (clientDriver != null && clientDriver.acceptsURL(url))
096: return clientDriver.connect(url, info);
097: if (serverDriver != null && serverDriver.acceptsURL(url))
098: return serverDriver.connect(url, info);
099:
100: // this is necessary since the driver manager does not call acceptsURL when selecting a driver !!
101: LOG_JDBC.warn("Unexpected URL " + url + ", cannot connect");
102: return null;
103:
104: }
105:
106: /**
107: * Indicates whether the driver accepts the url.
108: * If driver is loaded using Class.forName(), this method will never be called!
109: * @param url the url to be tested
110: * @return true or false
111: */
112: public boolean acceptsURL(String url) throws SQLException {
113: return (clientDriver != null && clientDriver.acceptsURL(url))
114: || (serverDriver != null && serverDriver
115: .acceptsURL(url));
116: }
117:
118: }
|