java/jdbc-dk-driver/src/main/java/info/globalcode/sql/dk/jdbc/Driver.java
branchv_0
changeset 239 39e6c2ad3571
parent 235 8ce612cca4d8
child 250 aae5009bd0af
equal deleted inserted replaced
238:4a1864c3e867 239:39e6c2ad3571
       
     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 			// The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL.
       
    90 			return null;
       
    91 		}
       
    92 	}
       
    93 
       
    94 	private Connection getConnection(String connectionName, java.util.Properties info) throws SQLException, ConfigurationException {
       
    95 		Configuration c = loader.loadConfiguration();
       
    96 		DatabaseDefinition dd = c.getDatabase(connectionName);
       
    97 
       
    98 		if (acceptsURL(dd.getUrl())) {
       
    99 			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});
       
   100 			throw new ConfigurationException("SQL-DK meta JDBC driver loops to itself.");
       
   101 		} else {
       
   102 			return Loader.jdbcConnect(dd, translate(info));
       
   103 		}
       
   104 	}
       
   105 
       
   106 	private String extractDatabaseName(String url) {
       
   107 		return url.split("//", 2)[1];
       
   108 	}
       
   109 
       
   110 	/**
       
   111 	 * TODO: refactor/move, reuse
       
   112 	 *
       
   113 	 * @param info
       
   114 	 * @return
       
   115 	 */
       
   116 	private Properties translate(java.util.Properties info) {
       
   117 		Properties properties = new Properties();
       
   118 
       
   119 		for (String name : info.stringPropertyNames()) {
       
   120 			String value = info.getProperty(name);
       
   121 			properties.add(new Property(name, value));
       
   122 		}
       
   123 
       
   124 		return properties;
       
   125 	}
       
   126 
       
   127 	@Override
       
   128 	public boolean acceptsURL(String url) throws SQLException {
       
   129 		return url != null && url.startsWith("jdbc:sql-dk://");
       
   130 	}
       
   131 
       
   132 	@Override
       
   133 	public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException {
       
   134 		return new DriverPropertyInfo[0];
       
   135 	}
       
   136 
       
   137 	@Override
       
   138 	public int getMajorVersion() {
       
   139 		return 0;
       
   140 	}
       
   141 
       
   142 	@Override
       
   143 	public int getMinorVersion() {
       
   144 		return 1;
       
   145 	}
       
   146 
       
   147 	@Override
       
   148 	public boolean jdbcCompliant() {
       
   149 		return false;
       
   150 	}
       
   151 
       
   152 	@Override
       
   153 	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
       
   154 		throw new SQLFeatureNotSupportedException("Not supported yet.");
       
   155 	}
       
   156 
       
   157 }