java/jdbc-dk-driver/src/main/java/info/globalcode/sql/dk/jdbc/Driver.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 239 39e6c2ad3571
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * SQL-DK
 * Copyright © 2015 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk.jdbc;

import info.globalcode.sql.dk.Constants;
import info.globalcode.sql.dk.configuration.Configuration;
import info.globalcode.sql.dk.configuration.ConfigurationException;
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
import info.globalcode.sql.dk.configuration.Loader;
import info.globalcode.sql.dk.configuration.Properties;
import info.globalcode.sql.dk.configuration.Property;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * <p>
 * Meta JDBC driver that works as redirector/router. It loads SQL-DK's configuration file and
 * instantiates a real JDBC connection (PostgreSQL, MariDB etc.) of given name.
 * </p>
 *
 * <p>
 * Raison d'être: user can define his/her database connections just once (in SQL-DK's configuration)
 * and use them in many other programs – there is no need to define the connection (hostname,
 * username, password, properties…) in each application again and again. User will simply connect to
 * e.g. <code>jdbc:sql-dk://my-connection</code> and this driver reads parameters of
 * <code>my-connection</code> from SQL-DK's configuration and returns real driver implementation.
 * </p>
 *
 * <p>
 * Of course, the real JDBC driver for given database is still needed on the class path.
 * </p>
 *
 * <p>
 * TODO: current version is quite heavy-weight, because it includes whole SQL-DK source tree. Some
 * refactoring and separation is desired to provide more light-weight JDBC driver. However the
 * public interface and behavior of this driver should remain unchanged.
 * </p>
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class Driver implements java.sql.Driver {

	private static final Logger log = Logger.getLogger(Driver.class.getName());

	private final Loader loader = new Loader();

	static {
		try {
			DriverManager.registerDriver(new Driver());
		} catch (SQLException e) {
			log.log(Level.SEVERE, "Unable to register JDBC driver", e);
		}
	}

	@Override
	public Connection connect(String url, java.util.Properties info) throws SQLException {
		if (acceptsURL(url)) {
			log.log(Level.FINER, "Loading SQL-DK configuration for URL: {0}", url);
			String name = extractDatabaseName(url);
			log.log(Level.FINE, "Loading SQL-DK configuration for name: {0}", name);

			try {
				return getConnection(name, info);
			} catch (ConfigurationException e) {
				log.log(Level.SEVERE, "Unable to load SQL-DK configuration for name: {0}. Is it defined in {1}?", new Object[]{name, Constants.CONFIG_FILE});
				throw new SQLException("Unable to load SQL-DK configuration for name: " + name, e);
			}
		} else {
			// The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL.
			return null;
		}
	}

	private Connection getConnection(String connectionName, java.util.Properties info) throws SQLException, ConfigurationException {
		Configuration c = loader.loadConfiguration();
		DatabaseDefinition dd = c.getDatabase(connectionName);

		if (acceptsURL(dd.getUrl())) {
			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});
			throw new ConfigurationException("SQL-DK meta JDBC driver loops to itself.");
		} else {
			return Loader.jdbcConnect(dd, translate(info));
		}
	}

	private String extractDatabaseName(String url) {
		return url.split("//", 2)[1];
	}

	/**
	 * TODO: refactor/move, reuse
	 *
	 * @param info
	 * @return
	 */
	private Properties translate(java.util.Properties info) {
		Properties properties = new Properties();

		for (String name : info.stringPropertyNames()) {
			String value = info.getProperty(name);
			properties.add(new Property(name, value));
		}

		return properties;
	}

	@Override
	public boolean acceptsURL(String url) throws SQLException {
		return url != null && url.startsWith("jdbc:sql-dk://");
	}

	@Override
	public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException {
		return new DriverPropertyInfo[0];
	}

	@Override
	public int getMajorVersion() {
		return 0;
	}

	@Override
	public int getMinorVersion() {
		return 1;
	}

	@Override
	public boolean jdbcCompliant() {
		return false;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		throw new SQLFeatureNotSupportedException("Not supported yet.");
	}

}