java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 17 May 2015 00:27:56 +0200
branchv_0
changeset 192 a32bfcbdee51
parent 189 f4d879cbcee1
child 193 5a18a6adf7f9
permissions -rw-r--r--
jdbc-dk-driver: first working version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     1
/**
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     2
 * SQL-DK
189
f4d879cbcee1 separate configuration loading into the Loader class
František Kučera <franta-hg@frantovo.cz>
parents: 179
diff changeset
     3
 * Copyright © 2015 František Kučera (frantovo.cz)
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     4
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     8
 * (at your option) any later version.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     9
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    13
 * GNU General Public License for more details.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    14
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    15
 * You should have received a copy of the GNU General Public License
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    17
 */
189
f4d879cbcee1 separate configuration loading into the Loader class
František Kučera <franta-hg@frantovo.cz>
parents: 179
diff changeset
    18
package info.globalcode.sql.dk.configuration;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
189
f4d879cbcee1 separate configuration loading into the Loader class
František Kučera <franta-hg@frantovo.cz>
parents: 179
diff changeset
    20
import info.globalcode.sql.dk.*;
192
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    21
import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_USER;
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    22
import static info.globalcode.sql.dk.DatabaseConnection.JDBC_PROPERTY_PASSWORD;
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    23
import java.sql.Connection;
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    24
import java.sql.DriverManager;
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    25
import java.sql.SQLException;
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    26
import java.util.logging.Level;
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    27
import java.util.logging.Logger;
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    28
import javax.xml.bind.JAXBContext;
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    29
import javax.xml.bind.Unmarshaller;
13
599aad77e986 fix: try/catch
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    30
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
/**
189
f4d879cbcee1 separate configuration loading into the Loader class
František Kučera <franta-hg@frantovo.cz>
parents: 179
diff changeset
    32
 * Configuration loader – deserializes Configuration from the XML file.
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 *
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 * @author Ing. František Kučera (frantovo.cz)
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
 */
189
f4d879cbcee1 separate configuration loading into the Loader class
František Kučera <franta-hg@frantovo.cz>
parents: 179
diff changeset
    36
public class Loader {
20
e225bdcd260e refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    37
192
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    38
	private static final Logger log = Logger.getLogger(Loader.class.getName());
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    39
189
f4d879cbcee1 separate configuration loading into the Loader class
František Kučera <franta-hg@frantovo.cz>
parents: 179
diff changeset
    40
	public Configuration loadConfiguration() throws ConfigurationException {
33
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    41
		try {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    42
			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    43
			Unmarshaller u = jaxb.createUnmarshaller();
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    44
			return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    45
		} catch (Exception e) {
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    46
			throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
04db6ccd6c48 configuration loading from XML
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    47
		}
20
e225bdcd260e refactor, configuration
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    48
	}
80
c4635ab3a7af bash completion: generate helper files with databases and formatters from configuration
František Kučera <franta-hg@frantovo.cz>
parents: 75
diff changeset
    49
192
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    50
	/**
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    51
	 * JDBC connection should not be used directly in SQL-DK.
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    52
	 *
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    53
	 * @see DatabaseDefinition#connect(info.globalcode.sql.dk.configuration.Properties)
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    54
	 * @param properties
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    55
	 * @param databaseDefinition
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    56
	 * @return
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    57
	 * @throws java.sql.SQLException
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    58
	 */
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    59
	public static Connection jdbcConnect(DatabaseDefinition databaseDefinition, Properties properties) throws SQLException {
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    60
		if (properties.hasProperty(JDBC_PROPERTY_PASSWORD)) {
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    61
			log.log(Level.WARNING, "Passing DB password as CLI parameter is insecure!");
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    62
		}
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    63
		Properties credentials = new Properties();
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    64
		credentials.add(new Property(JDBC_PROPERTY_USER, databaseDefinition.getUserName()));
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    65
		credentials.add(new Property(JDBC_PROPERTY_PASSWORD, databaseDefinition.getPassword()));
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    66
		credentials.setDefaults(databaseDefinition.getProperties());
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    67
		properties.setDefaults(credentials);
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    68
		java.util.Properties javaProperties = properties.getJavaProperties();
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    69
		return DriverManager.getConnection(databaseDefinition.getUrl(), javaProperties);
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    70
	}
a32bfcbdee51 jdbc-dk-driver: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 189
diff changeset
    71
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
}