java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
branchv_0
changeset 33 04db6ccd6c48
parent 26 4ec8e5534eb9
child 34 9335cf31c0f2
--- 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);
+		}
 	}
 }