java/jdbc-dk-driver/src/info/globalcode/sql/dk/jdbc/Driver.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 13 Sep 2015 20:10:13 +0200
branchv_0
changeset 228 3787c999d12c
parent 195 aed11d9107bf
child 235 8ce612cca4d8
permissions -rw-r--r--
BarChartFormatter: TODO comments
     1 /**
     2  * SQL-DK
     3  * Copyright © 2015 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk.jdbc;
    19 
    20 import info.globalcode.sql.dk.Constants;
    21 import info.globalcode.sql.dk.configuration.Configuration;
    22 import info.globalcode.sql.dk.configuration.ConfigurationException;
    23 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    24 import info.globalcode.sql.dk.configuration.Loader;
    25 import info.globalcode.sql.dk.configuration.Properties;
    26 import info.globalcode.sql.dk.configuration.Property;
    27 import java.sql.Connection;
    28 import java.sql.DriverManager;
    29 import java.sql.DriverPropertyInfo;
    30 import java.sql.SQLException;
    31 import java.sql.SQLFeatureNotSupportedException;
    32 import java.util.logging.Level;
    33 import java.util.logging.Logger;
    34 
    35 /**
    36  * <p>
    37  * Meta JDBC driver that works as redirector/router. It loads SQL-DK's configuration file and
    38  * instantiates a real JDBC connection (PostgreSQL, MariDB etc.) of given name.
    39  * </p>
    40  *
    41  * <p>
    42  * Raison d'être: user can define his/her database connections just once (in SQL-DK's configuration)
    43  * and use them in many other programs – there is no need to define the connection (hostname,
    44  * username, password, properties…) in each application again and again. User will simply connect to
    45  * e.g. <code>jdbc:sql-dk://my-connection</code> and this driver reads parameters of
    46  * <code>my-connection</code> from SQL-DK's configuration and returns real driver implementation.
    47  * </p>
    48  *
    49  * <p>
    50  * Of course, the real JDBC driver for given database is still needed on the class path.
    51  * </p>
    52  *
    53  * <p>
    54  * TODO: current version is quite heavy-weight, because it includes whole SQL-DK source tree. Some
    55  * refactoring and separation is desired to provide more light-weight JDBC driver. However the
    56  * public interface and behavior of this driver should remain unchanged.
    57  * </p>
    58  *
    59  * @author Ing. František Kučera (frantovo.cz)
    60  */
    61 public class Driver implements java.sql.Driver {
    62 
    63 	private static final Logger log = Logger.getLogger(Driver.class.getName());
    64 
    65 	private final Loader loader = new Loader();
    66 
    67 	static {
    68 		try {
    69 			DriverManager.registerDriver(new Driver());
    70 		} catch (SQLException e) {
    71 			log.log(Level.SEVERE, "Unable to register JDBC driver", e);
    72 		}
    73 	}
    74 
    75 	@Override
    76 	public Connection connect(String url, java.util.Properties info) throws SQLException {
    77 		if (acceptsURL(url)) {
    78 			log.log(Level.FINER, "Loading SQL-DK configuration for URL: {0}", url);
    79 			String name = extractDatabaseName(url);
    80 			log.log(Level.FINE, "Loading SQL-DK configuration for name: {0}", name);
    81 
    82 			try {
    83 				return getConnection(name, info);
    84 			} catch (ConfigurationException e) {
    85 				log.log(Level.SEVERE, "Unable to load SQL-DK configuration for name: {0}. Is it defined in {1}?", new Object[]{name, Constants.CONFIG_FILE});
    86 				throw new SQLException("Unable to load SQL-DK configuration for name: " + name, e);
    87 			}
    88 		} else {
    89 			throw new SQLException("Unsupported URL: " + url);
    90 		}
    91 	}
    92 
    93 	private Connection getConnection(String connectionName, java.util.Properties info) throws SQLException, ConfigurationException {
    94 		Configuration c = loader.loadConfiguration();
    95 		DatabaseDefinition dd = c.getDatabase(connectionName);
    96 
    97 		if (acceptsURL(dd.getUrl())) {
    98 			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});
    99 			throw new ConfigurationException("SQL-DK meta JDBC driver loops to itself.");
   100 		} else {
   101 			return Loader.jdbcConnect(dd, translate(info));
   102 		}
   103 	}
   104 
   105 	private String extractDatabaseName(String url) {
   106 		return url.split("//", 2)[1];
   107 	}
   108 
   109 	/**
   110 	 * TODO: refactor/move, reuse
   111 	 *
   112 	 * @param info
   113 	 * @return
   114 	 */
   115 	private Properties translate(java.util.Properties info) {
   116 		Properties properties = new Properties();
   117 
   118 		for (String name : info.stringPropertyNames()) {
   119 			String value = info.getProperty(name);
   120 			properties.add(new Property(name, value));
   121 		}
   122 
   123 		return properties;
   124 	}
   125 
   126 	@Override
   127 	public boolean acceptsURL(String url) throws SQLException {
   128 		return url != null && url.startsWith("jdbc:sql-dk://");
   129 	}
   130 
   131 	@Override
   132 	public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException {
   133 		return new DriverPropertyInfo[0];
   134 	}
   135 
   136 	@Override
   137 	public int getMajorVersion() {
   138 		return 0;
   139 	}
   140 
   141 	@Override
   142 	public int getMinorVersion() {
   143 		return 1;
   144 	}
   145 
   146 	@Override
   147 	public boolean jdbcCompliant() {
   148 		return false;
   149 	}
   150 
   151 	@Override
   152 	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
   153 		throw new SQLFeatureNotSupportedException("Not supported yet.");
   154 	}
   155 
   156 }