java/sql-dk/src/info/globalcode/sql/dk/configuration/Loader.java
branchv_0
changeset 192 a32bfcbdee51
parent 189 f4d879cbcee1
child 193 5a18a6adf7f9
equal deleted inserted replaced
191:862d0a8747ac 192:a32bfcbdee51
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    17  */
    18 package info.globalcode.sql.dk.configuration;
    18 package info.globalcode.sql.dk.configuration;
    19 
    19 
    20 import info.globalcode.sql.dk.*;
    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;
    21 import javax.xml.bind.JAXBContext;
    28 import javax.xml.bind.JAXBContext;
    22 import javax.xml.bind.Unmarshaller;
    29 import javax.xml.bind.Unmarshaller;
    23 
    30 
    24 /**
    31 /**
    25  * Configuration loader – deserializes Configuration from the XML file.
    32  * Configuration loader – deserializes Configuration from the XML file.
    26  *
    33  *
    27  * @author Ing. František Kučera (frantovo.cz)
    34  * @author Ing. František Kučera (frantovo.cz)
    28  */
    35  */
    29 public class Loader {
    36 public class Loader {
       
    37 
       
    38 	private static final Logger log = Logger.getLogger(Loader.class.getName());
    30 
    39 
    31 	public Configuration loadConfiguration() throws ConfigurationException {
    40 	public Configuration loadConfiguration() throws ConfigurationException {
    32 		try {
    41 		try {
    33 			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
    42 			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
    34 			Unmarshaller u = jaxb.createUnmarshaller();
    43 			Unmarshaller u = jaxb.createUnmarshaller();
    36 		} catch (Exception e) {
    45 		} catch (Exception e) {
    37 			throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
    46 			throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
    38 		}
    47 		}
    39 	}
    48 	}
    40 
    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 
    41 }
    72 }