java/jdbc-dk-driver/src/info/globalcode/sql/dk/jdbc/Driver.java
branchv_0
changeset 239 39e6c2ad3571
parent 238 4a1864c3e867
child 240 09c72e23c895
     1.1 --- a/java/jdbc-dk-driver/src/info/globalcode/sql/dk/jdbc/Driver.java	Mon Mar 04 20:15:24 2019 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,157 +0,0 @@
     1.4 -/**
     1.5 - * SQL-DK
     1.6 - * Copyright © 2015 František Kučera (frantovo.cz)
     1.7 - *
     1.8 - * This program is free software: you can redistribute it and/or modify
     1.9 - * it under the terms of the GNU General Public License as published by
    1.10 - * the Free Software Foundation, either version 3 of the License, or
    1.11 - * (at your option) any later version.
    1.12 - *
    1.13 - * This program is distributed in the hope that it will be useful,
    1.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 - * GNU General Public License for more details.
    1.17 - *
    1.18 - * You should have received a copy of the GNU General Public License
    1.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 - */
    1.21 -package info.globalcode.sql.dk.jdbc;
    1.22 -
    1.23 -import info.globalcode.sql.dk.Constants;
    1.24 -import info.globalcode.sql.dk.configuration.Configuration;
    1.25 -import info.globalcode.sql.dk.configuration.ConfigurationException;
    1.26 -import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    1.27 -import info.globalcode.sql.dk.configuration.Loader;
    1.28 -import info.globalcode.sql.dk.configuration.Properties;
    1.29 -import info.globalcode.sql.dk.configuration.Property;
    1.30 -import java.sql.Connection;
    1.31 -import java.sql.DriverManager;
    1.32 -import java.sql.DriverPropertyInfo;
    1.33 -import java.sql.SQLException;
    1.34 -import java.sql.SQLFeatureNotSupportedException;
    1.35 -import java.util.logging.Level;
    1.36 -import java.util.logging.Logger;
    1.37 -
    1.38 -/**
    1.39 - * <p>
    1.40 - * Meta JDBC driver that works as redirector/router. It loads SQL-DK's configuration file and
    1.41 - * instantiates a real JDBC connection (PostgreSQL, MariDB etc.) of given name.
    1.42 - * </p>
    1.43 - *
    1.44 - * <p>
    1.45 - * Raison d'être: user can define his/her database connections just once (in SQL-DK's configuration)
    1.46 - * and use them in many other programs – there is no need to define the connection (hostname,
    1.47 - * username, password, properties…) in each application again and again. User will simply connect to
    1.48 - * e.g. <code>jdbc:sql-dk://my-connection</code> and this driver reads parameters of
    1.49 - * <code>my-connection</code> from SQL-DK's configuration and returns real driver implementation.
    1.50 - * </p>
    1.51 - *
    1.52 - * <p>
    1.53 - * Of course, the real JDBC driver for given database is still needed on the class path.
    1.54 - * </p>
    1.55 - *
    1.56 - * <p>
    1.57 - * TODO: current version is quite heavy-weight, because it includes whole SQL-DK source tree. Some
    1.58 - * refactoring and separation is desired to provide more light-weight JDBC driver. However the
    1.59 - * public interface and behavior of this driver should remain unchanged.
    1.60 - * </p>
    1.61 - *
    1.62 - * @author Ing. František Kučera (frantovo.cz)
    1.63 - */
    1.64 -public class Driver implements java.sql.Driver {
    1.65 -
    1.66 -	private static final Logger log = Logger.getLogger(Driver.class.getName());
    1.67 -
    1.68 -	private final Loader loader = new Loader();
    1.69 -
    1.70 -	static {
    1.71 -		try {
    1.72 -			DriverManager.registerDriver(new Driver());
    1.73 -		} catch (SQLException e) {
    1.74 -			log.log(Level.SEVERE, "Unable to register JDBC driver", e);
    1.75 -		}
    1.76 -	}
    1.77 -
    1.78 -	@Override
    1.79 -	public Connection connect(String url, java.util.Properties info) throws SQLException {
    1.80 -		if (acceptsURL(url)) {
    1.81 -			log.log(Level.FINER, "Loading SQL-DK configuration for URL: {0}", url);
    1.82 -			String name = extractDatabaseName(url);
    1.83 -			log.log(Level.FINE, "Loading SQL-DK configuration for name: {0}", name);
    1.84 -
    1.85 -			try {
    1.86 -				return getConnection(name, info);
    1.87 -			} catch (ConfigurationException e) {
    1.88 -				log.log(Level.SEVERE, "Unable to load SQL-DK configuration for name: {0}. Is it defined in {1}?", new Object[]{name, Constants.CONFIG_FILE});
    1.89 -				throw new SQLException("Unable to load SQL-DK configuration for name: " + name, e);
    1.90 -			}
    1.91 -		} else {
    1.92 -			// The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL.
    1.93 -			return null;
    1.94 -		}
    1.95 -	}
    1.96 -
    1.97 -	private Connection getConnection(String connectionName, java.util.Properties info) throws SQLException, ConfigurationException {
    1.98 -		Configuration c = loader.loadConfiguration();
    1.99 -		DatabaseDefinition dd = c.getDatabase(connectionName);
   1.100 -
   1.101 -		if (acceptsURL(dd.getUrl())) {
   1.102 -			log.log(Level.SEVERE, "SQL-DK meta JDBC driver loops to itself: {0} → {1} Please check {2}", new Object[]{connectionName, dd.getUrl(), Constants.CONFIG_FILE});
   1.103 -			throw new ConfigurationException("SQL-DK meta JDBC driver loops to itself.");
   1.104 -		} else {
   1.105 -			return Loader.jdbcConnect(dd, translate(info));
   1.106 -		}
   1.107 -	}
   1.108 -
   1.109 -	private String extractDatabaseName(String url) {
   1.110 -		return url.split("//", 2)[1];
   1.111 -	}
   1.112 -
   1.113 -	/**
   1.114 -	 * TODO: refactor/move, reuse
   1.115 -	 *
   1.116 -	 * @param info
   1.117 -	 * @return
   1.118 -	 */
   1.119 -	private Properties translate(java.util.Properties info) {
   1.120 -		Properties properties = new Properties();
   1.121 -
   1.122 -		for (String name : info.stringPropertyNames()) {
   1.123 -			String value = info.getProperty(name);
   1.124 -			properties.add(new Property(name, value));
   1.125 -		}
   1.126 -
   1.127 -		return properties;
   1.128 -	}
   1.129 -
   1.130 -	@Override
   1.131 -	public boolean acceptsURL(String url) throws SQLException {
   1.132 -		return url != null && url.startsWith("jdbc:sql-dk://");
   1.133 -	}
   1.134 -
   1.135 -	@Override
   1.136 -	public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException {
   1.137 -		return new DriverPropertyInfo[0];
   1.138 -	}
   1.139 -
   1.140 -	@Override
   1.141 -	public int getMajorVersion() {
   1.142 -		return 0;
   1.143 -	}
   1.144 -
   1.145 -	@Override
   1.146 -	public int getMinorVersion() {
   1.147 -		return 1;
   1.148 -	}
   1.149 -
   1.150 -	@Override
   1.151 -	public boolean jdbcCompliant() {
   1.152 -		return false;
   1.153 -	}
   1.154 -
   1.155 -	@Override
   1.156 -	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
   1.157 -		throw new SQLFeatureNotSupportedException("Not supported yet.");
   1.158 -	}
   1.159 -
   1.160 -}