3 * Copyright © 2015 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package info.globalcode.sql.dk.jdbc;
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;
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.
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.
50 * Of course, the real JDBC driver for given database is still needed on the class path.
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.
59 * @author Ing. František Kučera (frantovo.cz)
61 public class Driver implements java.sql.Driver {
63 private static final Logger log = Logger.getLogger(Driver.class.getName());
65 private final Loader loader = new Loader();
69 DriverManager.registerDriver(new Driver());
70 } catch (SQLException e) {
71 log.log(Level.SEVERE, "Unable to register JDBC driver", e);
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);
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);
89 // The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL.
94 private Connection getConnection(String connectionName, java.util.Properties info) throws SQLException, ConfigurationException {
95 Configuration c = loader.loadConfiguration();
96 DatabaseDefinition dd = c.getDatabase(connectionName);
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.");
102 return Loader.jdbcConnect(dd, translate(info));
106 private String extractDatabaseName(String url) {
107 return url.split("//", 2)[1];
111 * TODO: refactor/move, reuse
116 private Properties translate(java.util.Properties info) {
117 Properties properties = new Properties();
119 for (String name : info.stringPropertyNames()) {
120 String value = info.getProperty(name);
121 properties.add(new Property(name, value));
128 public boolean acceptsURL(String url) throws SQLException {
129 return url != null && url.startsWith("jdbc:sql-dk://");
133 public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException {
134 return new DriverPropertyInfo[0];
138 public int getMajorVersion() {
143 public int getMinorVersion() {
148 public boolean jdbcCompliant() {
153 public Logger getParentLogger() throws SQLFeatureNotSupportedException {
154 throw new SQLFeatureNotSupportedException("Not supported yet.");