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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk.jdbc;
19 import info.globalcode.sql.dk.Constants;
20 import info.globalcode.sql.dk.configuration.Configuration;
21 import info.globalcode.sql.dk.configuration.ConfigurationException;
22 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
23 import info.globalcode.sql.dk.configuration.Loader;
24 import info.globalcode.sql.dk.configuration.Properties;
25 import info.globalcode.sql.dk.configuration.Property;
26 import java.sql.Connection;
27 import java.sql.DriverManager;
28 import java.sql.DriverPropertyInfo;
29 import java.sql.SQLException;
30 import java.sql.SQLFeatureNotSupportedException;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
36 * Meta JDBC driver that works as redirector/router. It loads SQL-DK's configuration file and
37 * instantiates a real JDBC connection (PostgreSQL, MariDB etc.) of given name.
41 * Raison d'être: user can define his/her database connections just once (in SQL-DK's configuration)
42 * and use them in many other programs – there is no need to define the connection (hostname,
43 * username, password, properties…) in each application again and again. User will simply connect to
44 * e.g. <code>jdbc:sql-dk://my-connection</code> and this driver reads parameters of
45 * <code>my-connection</code> from SQL-DK's configuration and returns real driver implementation.
49 * Of course, the real JDBC driver for given database is still needed on the class path.
53 * TODO: current version is quite heavy-weight, because it includes whole SQL-DK source tree. Some
54 * refactoring and separation is desired to provide more light-weight JDBC driver. However the
55 * public interface and behavior of this driver should remain unchanged.
58 * @author Ing. František Kučera (frantovo.cz)
60 public class Driver implements java.sql.Driver {
62 private static final Logger log = Logger.getLogger(Driver.class.getName());
64 private final Loader loader = new Loader();
68 DriverManager.registerDriver(new Driver());
69 } catch (SQLException e) {
70 log.log(Level.SEVERE, "Unable to register JDBC driver", e);
75 public Connection connect(String url, java.util.Properties info) throws SQLException {
76 if (acceptsURL(url)) {
77 log.log(Level.FINER, "Loading SQL-DK configuration for URL: {0}", url);
78 String name = extractDatabaseName(url);
79 log.log(Level.FINE, "Loading SQL-DK configuration for name: {0}", name);
82 return getConnection(name, info);
83 } catch (ConfigurationException e) {
84 log.log(Level.SEVERE, "Unable to load SQL-DK configuration for name: {0}. Is it defined in {1}?", new Object[]{name, Constants.CONFIG_FILE});
85 throw new SQLException("Unable to load SQL-DK configuration for name: " + name, e);
88 // The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL.
93 private Connection getConnection(String connectionName, java.util.Properties info) throws SQLException, ConfigurationException {
94 Configuration c = loader.loadConfiguration();
95 DatabaseDefinition dd = c.getDatabase(connectionName);
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.");
101 return Loader.jdbcConnect(dd, translate(info));
105 private String extractDatabaseName(String url) {
106 return url.split("//", 2)[1];
110 * TODO: refactor/move, reuse
115 private Properties translate(java.util.Properties info) {
116 Properties properties = new Properties();
118 for (String name : info.stringPropertyNames()) {
119 String value = info.getProperty(name);
120 properties.add(new Property(name, value));
127 public boolean acceptsURL(String url) throws SQLException {
128 return url != null && url.startsWith("jdbc:sql-dk://");
132 public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException {
133 return new DriverPropertyInfo[0];
137 public int getMajorVersion() {
142 public int getMinorVersion() {
147 public boolean jdbcCompliant() {
152 public Logger getParentLogger() throws SQLFeatureNotSupportedException {
153 throw new SQLFeatureNotSupportedException("Not supported yet.");