franta-hg@171: /** franta-hg@171: * SQL-DK franta-hg@192: * Copyright © 2015 František Kučera (frantovo.cz) franta-hg@171: * franta-hg@171: * This program is free software: you can redistribute it and/or modify franta-hg@171: * it under the terms of the GNU General Public License as published by franta-hg@171: * the Free Software Foundation, either version 3 of the License, or franta-hg@171: * (at your option) any later version. franta-hg@171: * franta-hg@171: * This program is distributed in the hope that it will be useful, franta-hg@171: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@171: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@171: * GNU General Public License for more details. franta-hg@171: * franta-hg@171: * You should have received a copy of the GNU General Public License franta-hg@171: * along with this program. If not, see . franta-hg@171: */ franta-hg@192: package info.globalcode.sql.dk.jdbc; franta-hg@171: franta-hg@192: import info.globalcode.sql.dk.Constants; franta-hg@192: import info.globalcode.sql.dk.configuration.Configuration; franta-hg@192: import info.globalcode.sql.dk.configuration.ConfigurationException; franta-hg@192: import info.globalcode.sql.dk.configuration.DatabaseDefinition; franta-hg@192: import info.globalcode.sql.dk.configuration.Loader; franta-hg@192: import info.globalcode.sql.dk.configuration.Properties; franta-hg@192: import info.globalcode.sql.dk.configuration.Property; franta-hg@192: import java.sql.Connection; franta-hg@171: import java.sql.DriverManager; franta-hg@171: import java.sql.DriverPropertyInfo; franta-hg@171: import java.sql.SQLException; franta-hg@171: import java.sql.SQLFeatureNotSupportedException; franta-hg@171: import java.util.logging.Level; franta-hg@171: import java.util.logging.Logger; franta-hg@171: franta-hg@171: /** franta-hg@192: *

franta-hg@192: * Meta JDBC driver that works as redirector/router. It loads SQL-DK's configuration file and franta-hg@192: * instantiates a real JDBC connection (PostgreSQL, MariDB etc.) of given name. franta-hg@192: *

franta-hg@192: * franta-hg@192: *

franta-hg@192: * Raison d'être: user can define his/her database connections just once (in SQL-DK's configuration) franta-hg@192: * and use them in many other programs – there is no need to define the connection (hostname, franta-hg@192: * username, password, properties…) in each application again and again. User will simply connect to franta-hg@192: * e.g. jdbc:sql-dk://my-connection and this driver reads parameters of franta-hg@192: * my-connection from SQL-DK's configuration and returns real driver implementation. franta-hg@192: *

franta-hg@192: * franta-hg@192: *

franta-hg@192: * Of course, the real JDBC driver for given database is still needed on the class path. franta-hg@192: *

franta-hg@192: * franta-hg@192: *

franta-hg@192: * TODO: current version is quite heavy-weight, because it includes whole SQL-DK source tree. Some franta-hg@195: * refactoring and separation is desired to provide more light-weight JDBC driver. However the franta-hg@195: * public interface and behavior of this driver should remain unchanged. franta-hg@192: *

franta-hg@171: * franta-hg@171: * @author Ing. František Kučera (frantovo.cz) franta-hg@171: */ franta-hg@171: public class Driver implements java.sql.Driver { franta-hg@171: franta-hg@171: private static final Logger log = Logger.getLogger(Driver.class.getName()); franta-hg@171: franta-hg@192: private final Loader loader = new Loader(); franta-hg@192: franta-hg@171: static { franta-hg@171: try { franta-hg@171: DriverManager.registerDriver(new Driver()); franta-hg@171: } catch (SQLException e) { franta-hg@171: log.log(Level.SEVERE, "Unable to register JDBC driver", e); franta-hg@171: } franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@192: public Connection connect(String url, java.util.Properties info) throws SQLException { franta-hg@177: if (acceptsURL(url)) { franta-hg@192: log.log(Level.FINER, "Loading SQL-DK configuration for URL: {0}", url); franta-hg@192: String name = extractDatabaseName(url); franta-hg@192: log.log(Level.FINE, "Loading SQL-DK configuration for name: {0}", name); franta-hg@192: franta-hg@192: try { franta-hg@192: return getConnection(name, info); franta-hg@192: } catch (ConfigurationException e) { franta-hg@192: log.log(Level.SEVERE, "Unable to load SQL-DK configuration for name: {0}. Is it defined in {1}?", new Object[]{name, Constants.CONFIG_FILE}); franta-hg@192: throw new SQLException("Unable to load SQL-DK configuration for name: " + name, e); franta-hg@192: } franta-hg@177: } else { franta-hg@177: throw new SQLException("Unsupported URL: " + url); franta-hg@177: } franta-hg@171: } franta-hg@171: franta-hg@192: private Connection getConnection(String connectionName, java.util.Properties info) throws SQLException, ConfigurationException { franta-hg@192: Configuration c = loader.loadConfiguration(); franta-hg@192: DatabaseDefinition dd = c.getDatabase(connectionName); franta-hg@192: franta-hg@192: if (acceptsURL(dd.getUrl())) { franta-hg@192: 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}); franta-hg@192: throw new ConfigurationException("SQL-DK meta JDBC driver loops to itself."); franta-hg@192: } else { franta-hg@192: return Loader.jdbcConnect(dd, translate(info)); franta-hg@192: } franta-hg@192: } franta-hg@192: franta-hg@192: private String extractDatabaseName(String url) { franta-hg@192: return url.split("//", 2)[1]; franta-hg@192: } franta-hg@192: franta-hg@192: /** franta-hg@192: * TODO: refactor/move, reuse franta-hg@192: * franta-hg@192: * @param info franta-hg@192: * @return franta-hg@192: */ franta-hg@192: private Properties translate(java.util.Properties info) { franta-hg@192: Properties properties = new Properties(); franta-hg@192: franta-hg@192: for (String name : info.stringPropertyNames()) { franta-hg@192: String value = info.getProperty(name); franta-hg@192: properties.add(new Property(name, value)); franta-hg@192: } franta-hg@192: franta-hg@192: return properties; franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@192: public boolean acceptsURL(String url) throws SQLException { franta-hg@192: return url != null && url.startsWith("jdbc:sql-dk://"); franta-hg@192: } franta-hg@192: franta-hg@192: @Override franta-hg@192: public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException { franta-hg@171: return new DriverPropertyInfo[0]; franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public int getMajorVersion() { franta-hg@171: return 0; franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public int getMinorVersion() { franta-hg@171: return 1; franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public boolean jdbcCompliant() { franta-hg@171: return false; franta-hg@171: } franta-hg@171: franta-hg@171: @Override franta-hg@171: public Logger getParentLogger() throws SQLFeatureNotSupportedException { franta-hg@171: throw new SQLFeatureNotSupportedException("Not supported yet."); franta-hg@171: } franta-hg@171: franta-hg@171: }