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