java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java
branchv_0
changeset 238 4a1864c3e867
parent 237 7e08730da258
child 239 39e6c2ad3571
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java	Mon Mar 04 17:06:42 2019 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,101 +0,0 @@
     1.4 -/**
     1.5 - * SQL-DK
     1.6 - * Copyright © 2015 František Kučera (frantovo.cz)
     1.7 - *
     1.8 - * This program is free software: you can redistribute it and/or modify
     1.9 - * it under the terms of the GNU General Public License as published by
    1.10 - * the Free Software Foundation, either version 3 of the License, or
    1.11 - * (at your option) any later version.
    1.12 - *
    1.13 - * This program is distributed in the hope that it will be useful,
    1.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 - * GNU General Public License for more details.
    1.17 - *
    1.18 - * You should have received a copy of the GNU General Public License
    1.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 - */
    1.21 -package info.globalcode.sql.dk.configuration;
    1.22 -
    1.23 -import info.globalcode.sql.dk.*;
    1.24 -import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_USER;
    1.25 -import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_PASSWORD;
    1.26 -import java.sql.Connection;
    1.27 -import java.sql.Driver;
    1.28 -import java.sql.DriverManager;
    1.29 -import java.sql.SQLException;
    1.30 -import java.util.logging.Level;
    1.31 -import java.util.logging.Logger;
    1.32 -import javax.xml.bind.JAXBContext;
    1.33 -import javax.xml.bind.Unmarshaller;
    1.34 -
    1.35 -/**
    1.36 - * Configuration loader – deserializes Configuration from the XML file.
    1.37 - *
    1.38 - * @author Ing. František Kučera (frantovo.cz)
    1.39 - */
    1.40 -public class Loader {
    1.41 -
    1.42 -	private static final Logger log = Logger.getLogger(Loader.class.getName());
    1.43 -
    1.44 -	public Configuration loadConfiguration() throws ConfigurationException {
    1.45 -		try {
    1.46 -			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class.getPackage().getName(), Configuration.class.getClassLoader());
    1.47 -			Unmarshaller u = jaxb.createUnmarshaller();
    1.48 -			return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
    1.49 -		} catch (Exception e) {
    1.50 -			throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
    1.51 -		}
    1.52 -	}
    1.53 -
    1.54 -	/**
    1.55 -	 * JDBC connection should not be used directly in SQL-DK.
    1.56 -	 *
    1.57 -	 * @see DatabaseDefinition#connect(info.globalcode.sql.dk.configuration.Properties)
    1.58 -	 * @param properties
    1.59 -	 * @param databaseDefinition
    1.60 -	 * @return
    1.61 -	 * @throws java.sql.SQLException
    1.62 -	 */
    1.63 -	public static Connection jdbcConnect(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
    1.64 -		synchronized (properties) {
    1.65 -			/**
    1.66 -			 * Avoid rewriting the properties. Usually, the connection is created only once, but
    1.67 -			 * with --test-connection and with SQL-DK JDBC driver, it might be reused.
    1.68 -			 */
    1.69 -			properties = properties.clone();
    1.70 -		}
    1.71 -		if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
    1.72 -			log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
    1.73 -		}
    1.74 -		Properties credentials = new Properties();
    1.75 -		credentials.add(new Property(JDBC_PROPERTY_USER, databaseDefinition.getUserName()));
    1.76 -		credentials.add(new Property(JDBC_PROPERTY_PASSWORD, databaseDefinition.getPassword()));
    1.77 -		credentials.setDefaults(databaseDefinition.getProperties());
    1.78 -		properties.setDefaults(credentials);
    1.79 -		java.util.Properties javaProperties = properties.getJavaProperties();
    1.80 -
    1.81 -		String driverClassName = databaseDefinition.getDriver();
    1.82 -		final String url = databaseDefinition.getUrl();
    1.83 -		if (driverClassName == null) {
    1.84 -			log.log(Level.FINE, "Using DriverManager to create connection for „{0}“", url);
    1.85 -			return DriverManager.getConnection(url, javaProperties);
    1.86 -		} else {
    1.87 -			log.log(Level.FINE, "Using custom Driver „{0}“ to create connection for „{1}“", new Object[]{driverClassName, url});
    1.88 -			try {
    1.89 -				Class<Driver> driverClass = (Class<Driver>) Class.forName(driverClassName);
    1.90 -				Driver driver = driverClass.newInstance();
    1.91 -				Connection connection = driver.connect(url, javaProperties);
    1.92 -				if (connection == null) {
    1.93 -					log.log(Level.SEVERE, "Driver „{0}“ returend null → it does not accept the URL: „{1}“", new Object[]{driverClassName, url});
    1.94 -					throw new SQLException("Unable to connect: driver returned null.");
    1.95 -				} else {
    1.96 -					return connection;
    1.97 -				}
    1.98 -			} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e) {
    1.99 -				throw new SQLException("Unable to connect usig specific driver: " + driverClassName, e);
   1.100 -			}
   1.101 -		}
   1.102 -	}
   1.103 -
   1.104 -}