1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 22 21:02:37 2013 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 22 22:02:44 2013 +0100
1.3 @@ -20,8 +20,12 @@
1.4 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
1.5 import info.globalcode.sql.dk.CLIOptions.MODE;
1.6 import info.globalcode.sql.dk.configuration.Configuration;
1.7 +import info.globalcode.sql.dk.configuration.ConfigurationException;
1.8 +import java.io.IOException;
1.9 import java.util.logging.Level;
1.10 import java.util.logging.Logger;
1.11 +import javax.xml.bind.JAXBContext;
1.12 +import javax.xml.bind.Unmarshaller;
1.13
1.14 /**
1.15 *
1.16 @@ -82,7 +86,7 @@
1.17 }
1.18
1.19 @Override
1.20 - public Configuration getConfiguration() {
1.21 + public Configuration getConfiguration() throws ConfigurationException {
1.22 if (configuration == null) {
1.23 configuration = loadConfiguration();
1.24 }
1.25 @@ -90,15 +94,27 @@
1.26 }
1.27
1.28 private void installDefaultConfiguration() {
1.29 - /**
1.30 - * TODO: check config folder/file and create it if missing
1.31 - */
1.32 + Constants.DIR.mkdir();
1.33 +
1.34 + if (Constants.CONFIG_FILE.exists()) {
1.35 + log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
1.36 + } else {
1.37 + try {
1.38 + Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
1.39 + } catch (IOException e) {
1.40 + log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e);
1.41 + }
1.42 + }
1.43 +
1.44 }
1.45
1.46 - private Configuration loadConfiguration() {
1.47 - /**
1.48 - * TODO: load configuration from XML
1.49 - */
1.50 - return null;
1.51 + private Configuration loadConfiguration() throws ConfigurationException {
1.52 + try {
1.53 + JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
1.54 + Unmarshaller u = jaxb.createUnmarshaller();
1.55 + return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
1.56 + } catch (Exception e) {
1.57 + throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
1.58 + }
1.59 }
1.60 }