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