--- /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 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<configuration xmlns="https://sql-dk.globalcode.info/xmlns/configuration">
+
+ <!-- Database Connections: -->
+ <!--
+ <database>
+ <name>my_postgres_1</name>
+ <url>jdbc:postgresql://localhost:5432/database_name</url>
+ <userName>dbuser</userName>
+ <password>dbpass</password>
+ </database>
+
+ <database>
+ <name>my_mysql_1</name>
+ <url>jdbc:mysql://localhost:3306/database_name</url>
+ <userName>dbuser</userName>
+ <password>dbpass</password>
+ </database>
+ -->
+
+ <!-- Output formatters: -->
+ <!--
+ <defaultFormatter>tabular</defaultFormatter>
+
+ <formatter>
+ <name>tabular</name>
+ <class>info.globalcode.sql.dk.formatting.TabularFormatter</class>
+ </formatter>
+
+ <formatter>
+ <name>xml</name>
+ <class>info.globalcode.sql.dk.formatting.XmlFormatter</class>
+ </formatter>
+
+ <formatter>
+ <name>silent</name>
+ <class>info.globalcode.sql.dk.formatting.SilentFormatter</class>
+ </formatter>
+ -->
+
+</configuration>
--- 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() {
}
--- 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);
+ }
}
}
--- 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() {
}
--- 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);
+ }
+ }
+ }
+ }
+ }
}
--- /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 <http://www.gnu.org/licenses/>.
+ */
+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);
+ }
+}
--- 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;
}