diff -r 7e08730da258 -r 4a1864c3e867 java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Loader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Loader.java Mon Mar 04 20:15:24 2019 +0100 @@ -0,0 +1,101 @@ +/** + * SQL-DK + * Copyright © 2015 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package info.globalcode.sql.dk.configuration; + +import info.globalcode.sql.dk.*; +import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_USER; +import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_PASSWORD; +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; + +/** + * Configuration loader – deserializes Configuration from the XML file. + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Loader { + + private static final Logger log = Logger.getLogger(Loader.class.getName()); + + public Configuration loadConfiguration() throws ConfigurationException { + try { + JAXBContext jaxb = JAXBContext.newInstance(Configuration.class.getPackage().getName(), Configuration.class.getClassLoader()); + Unmarshaller u = jaxb.createUnmarshaller(); + return (Configuration) u.unmarshal(Constants.CONFIG_FILE); + } catch (Exception e) { + throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e); + } + } + + /** + * JDBC connection should not be used directly in SQL-DK. + * + * @see DatabaseDefinition#connect(info.globalcode.sql.dk.configuration.Properties) + * @param properties + * @param databaseDefinition + * @return + * @throws java.sql.SQLException + */ + public static Connection jdbcConnect(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException { + synchronized (properties) { + /** + * Avoid rewriting the properties. Usually, the connection is created only once, but + * with --test-connection and with SQL-DK JDBC driver, it might be reused. + */ + properties = properties.clone(); + } + if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) { + log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!"); + } + Properties credentials = new Properties(); + credentials.add(new Property(JDBC_PROPERTY_USER, databaseDefinition.getUserName())); + credentials.add(new Property(JDBC_PROPERTY_PASSWORD, databaseDefinition.getPassword())); + credentials.setDefaults(databaseDefinition.getProperties()); + properties.setDefaults(credentials); + java.util.Properties javaProperties = properties.getJavaProperties(); + + String driverClassName = databaseDefinition.getDriver(); + final String url = databaseDefinition.getUrl(); + if (driverClassName == null) { + log.log(Level.FINE, "Using DriverManager to create connection for „{0}“", url); + return DriverManager.getConnection(url, javaProperties); + } else { + log.log(Level.FINE, "Using custom Driver „{0}“ to create connection for „{1}“", new Object[]{driverClassName, url}); + try { + Class driverClass = (Class) Class.forName(driverClassName); + Driver driver = driverClass.newInstance(); + Connection connection = driver.connect(url, javaProperties); + if (connection == null) { + log.log(Level.SEVERE, "Driver „{0}“ returend null → it does not accept the URL: „{1}“", new Object[]{driverClassName, url}); + throw new SQLException("Unable to connect: driver returned null."); + } else { + return connection; + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) { + throw new SQLException("Unable to connect usig specific driver: " + driverClassName, e); + } + } + } + +}