# HG changeset patch # User František Kučera # Date 1387746164 -3600 # Node ID 04db6ccd6c48e5f6b60a04cb179b45431a6a6450 # Parent 5e412dbd9362b78dea4f4011c32615f4054f3236 configuration loading from XML diff -r 5e412dbd9362 -r 04db6ccd6c48 java/sql-dk/data/info/globalcode/sql/dk/example-config.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/data/info/globalcode/sql/dk/example-config.xml Sun Dec 22 22:02:44 2013 +0100 @@ -0,0 +1,41 @@ + + + + + + + + + + diff -r 5e412dbd9362 -r 04db6ccd6c48 java/sql-dk/src/info/globalcode/sql/dk/CLIParserException.java --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParserException.java Sun Dec 22 21:02:37 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParserException.java Sun Dec 22 22:02:44 2013 +0100 @@ -21,7 +21,7 @@ * * @author Ing. František Kučera (frantovo.cz) */ -public class CLIParserException extends Exception { +public class CLIParserException extends DKException { public CLIParserException() { } diff -r 5e412dbd9362 -r 04db6ccd6c48 java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 22 21:02:37 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 22 22:02:44 2013 +0100 @@ -20,8 +20,12 @@ import info.globalcode.sql.dk.configuration.ConfigurationProvider; import info.globalcode.sql.dk.CLIOptions.MODE; import info.globalcode.sql.dk.configuration.Configuration; +import info.globalcode.sql.dk.configuration.ConfigurationException; +import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; /** * @@ -82,7 +86,7 @@ } @Override - public Configuration getConfiguration() { + public Configuration getConfiguration() throws ConfigurationException { if (configuration == null) { configuration = loadConfiguration(); } @@ -90,15 +94,27 @@ } private void installDefaultConfiguration() { - /** - * TODO: check config folder/file and create it if missing - */ + Constants.DIR.mkdir(); + + if (Constants.CONFIG_FILE.exists()) { + log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE); + } else { + try { + Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE); + } catch (IOException e) { + log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e); + } + } + } - private Configuration loadConfiguration() { - /** - * TODO: load configuration from XML - */ - return null; + private Configuration loadConfiguration() throws ConfigurationException { + try { + JAXBContext jaxb = JAXBContext.newInstance(Configuration.class); + 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); + } } } diff -r 5e412dbd9362 -r 04db6ccd6c48 java/sql-dk/src/info/globalcode/sql/dk/Constants.java --- a/java/sql-dk/src/info/globalcode/sql/dk/Constants.java Sun Dec 22 21:02:37 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Constants.java Sun Dec 22 22:02:44 2013 +0100 @@ -17,6 +17,8 @@ */ package info.globalcode.sql.dk; +import java.io.File; + /** * * @author Ing. František Kučera (frantovo.cz) @@ -27,6 +29,13 @@ public static final String LICENSE_FILE = "info/globalcode/sql/dk/license.txt"; public static final String VERSION_FILE = "info/globalcode/sql/dk/version.txt"; public static final String HELP_FILE = "info/globalcode/sql/dk/help.txt"; + private static final File HOME_DIR = new File(System.getProperty("user.home")); + /** + * Directory where config and log files are stored. + */ + public static final File DIR = new File(HOME_DIR, ".sql-dk"); + public static final File CONFIG_FILE = new File(DIR, "config.xml"); + public static final String EXAMPLE_CONFIG_FILE = "info/globalcode/sql/dk/example-config.xml"; private Constants() { } diff -r 5e412dbd9362 -r 04db6ccd6c48 java/sql-dk/src/info/globalcode/sql/dk/Functions.java --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Sun Dec 22 21:02:37 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Sun Dec 22 22:02:44 2013 +0100 @@ -18,9 +18,15 @@ package info.globalcode.sql.dk; import info.globalcode.sql.dk.configuration.NameIdentified; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collection; import java.util.Map; +import java.util.logging.Level; /** * @@ -95,4 +101,22 @@ return null; } + + /** + * Copy file from Java resources to file system. + */ + public static void installResource(String resourceName, File target) throws IOException { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) { + try (PrintWriter writer = new PrintWriter(target)) { + while (true) { + String line = reader.readLine(); + if (line == null) { + break; + } else { + writer.println(line); + } + } + } + } + } } diff -r 5e412dbd9362 -r 04db6ccd6c48 java/sql-dk/src/info/globalcode/sql/dk/configuration/ConfigurationException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/ConfigurationException.java Sun Dec 22 22:02:44 2013 +0100 @@ -0,0 +1,42 @@ +/** + * SQL-DK + * Copyright © 2013 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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 . + */ +package info.globalcode.sql.dk.configuration; + +import info.globalcode.sql.dk.DKException; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class ConfigurationException extends DKException { + + public ConfigurationException() { + } + + public ConfigurationException(String message) { + super(message); + } + + public ConfigurationException(Throwable cause) { + super(cause); + } + + public ConfigurationException(String message, Throwable cause) { + super(message, cause); + } +} diff -r 5e412dbd9362 -r 04db6ccd6c48 java/sql-dk/src/info/globalcode/sql/dk/configuration/ConfigurationProvider.java --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/ConfigurationProvider.java Sun Dec 22 21:02:37 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/ConfigurationProvider.java Sun Dec 22 22:02:44 2013 +0100 @@ -23,5 +23,5 @@ */ public interface ConfigurationProvider { - public Configuration getConfiguration(); + public Configuration getConfiguration() throws ConfigurationException; }