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.configuration;
20 import info.globalcode.sql.dk.*;
21 import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_USER;
22 import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_PASSWORD;
23 import java.sql.Connection;
24 import java.sql.Driver;
25 import java.sql.DriverManager;
26 import java.sql.SQLException;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import javax.xml.bind.JAXBContext;
30 import javax.xml.bind.Unmarshaller;
33 * Configuration loader – deserializes Configuration from the XML file.
35 * @author Ing. František Kučera (frantovo.cz)
39 private static final Logger log = Logger.getLogger(Loader.class.getName());
41 public Configuration loadConfiguration() throws ConfigurationException {
43 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class.getPackage().getName(), Configuration.class.getClassLoader());
44 Unmarshaller u = jaxb.createUnmarshaller();
45 return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
46 } catch (Exception e) {
47 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
52 * JDBC connection should not be used directly in SQL-DK.
54 * @see DatabaseDefinition#connect(info.globalcode.sql.dk.configuration.Properties)
56 * @param databaseDefinition
58 * @throws java.sql.SQLException
60 public static Connection jdbcConnect(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
61 synchronized (properties) {
63 * Avoid rewriting the properties. Usually, the connection is created only once, but
64 * with --test-connection and with SQL-DK JDBC driver, it might be reused.
66 properties = properties.clone();
68 if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
69 log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
71 Properties credentials = new Properties();
72 credentials.add(new Property(JDBC_PROPERTY_USER, databaseDefinition.getUserName()));
73 credentials.add(new Property(JDBC_PROPERTY_PASSWORD, databaseDefinition.getPassword()));
74 credentials.setDefaults(databaseDefinition.getProperties());
75 properties.setDefaults(credentials);
76 java.util.Properties javaProperties = properties.getJavaProperties();
78 String driverClassName = databaseDefinition.getDriver();
79 final String url = databaseDefinition.getUrl();
80 if (driverClassName == null) {
81 log.log(Level.FINE, "Using DriverManager to create connection for „{0}“", url);
82 return DriverManager.getConnection(url, javaProperties);
84 log.log(Level.FINE, "Using custom Driver „{0}“ to create connection for „{1}“", new Object[]{driverClassName, url});
86 Class<Driver> driverClass = (Class<Driver>) Class.forName(driverClassName);
87 Driver driver = driverClass.newInstance();
88 Connection connection = driver.connect(url, javaProperties);
89 if (connection == null) {
90 log.log(Level.SEVERE, "Driver „{0}“ returend null → it does not accept the URL: „{1}“", new Object[]{driverClassName, url});
91 throw new SQLException("Unable to connect: driver returned null.");
95 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
96 throw new SQLException("Unable to connect usig specific driver: " + driverClassName, e);