java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 17 May 2015 13:51:40 +0200
branchv_0
changeset 194 629c9c7eab01
parent 193 5a18a6adf7f9
child 196 76da38d49e81
permissions -rw-r--r--
JDBC driver class can be specified in the database configuration as an optional parameter (useful especially while embedding jdbc-dk-driver into other application that does not support automatic driver discovery)

/**
 * 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 <http://www.gnu.org/licenses/>.
 */
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 {
		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<Driver> driverClass = (Class<Driver>) Class.forName(driverClassName);
				Driver driver = driverClass.newInstance();
				return driver.connect(url, javaProperties);
			} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
				throw new SQLException("Unable to connect usig specific driver: " + driverClassName, e);
			}
		}
	}

}