java/sql-dk/src/main/java/info/globalcode/sql/dk/configuration/Loader.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 23 Sep 2022 18:05:50 +0200
branchv_0
changeset 254 c4b901ff0703
parent 250 aae5009bd0af
permissions -rw-r--r--
recfile formatter: write record count (as a comment at the end of the relation)

/**
 * 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, version 3 of the License.
 *
 * 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 {
		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<Driver> driverClass = (Class<Driver>) 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);
			}
		}
	}

}