1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/data/info/globalcode/sql/dk/example-config.xml Sun Dec 22 22:02:44 2013 +0100
1.3 @@ -0,0 +1,41 @@
1.4 +<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
1.5 +<configuration xmlns="https://sql-dk.globalcode.info/xmlns/configuration">
1.6 +
1.7 + <!-- Database Connections: -->
1.8 + <!--
1.9 + <database>
1.10 + <name>my_postgres_1</name>
1.11 + <url>jdbc:postgresql://localhost:5432/database_name</url>
1.12 + <userName>dbuser</userName>
1.13 + <password>dbpass</password>
1.14 + </database>
1.15 +
1.16 + <database>
1.17 + <name>my_mysql_1</name>
1.18 + <url>jdbc:mysql://localhost:3306/database_name</url>
1.19 + <userName>dbuser</userName>
1.20 + <password>dbpass</password>
1.21 + </database>
1.22 + -->
1.23 +
1.24 + <!-- Output formatters: -->
1.25 + <!--
1.26 + <defaultFormatter>tabular</defaultFormatter>
1.27 +
1.28 + <formatter>
1.29 + <name>tabular</name>
1.30 + <class>info.globalcode.sql.dk.formatting.TabularFormatter</class>
1.31 + </formatter>
1.32 +
1.33 + <formatter>
1.34 + <name>xml</name>
1.35 + <class>info.globalcode.sql.dk.formatting.XmlFormatter</class>
1.36 + </formatter>
1.37 +
1.38 + <formatter>
1.39 + <name>silent</name>
1.40 + <class>info.globalcode.sql.dk.formatting.SilentFormatter</class>
1.41 + </formatter>
1.42 + -->
1.43 +
1.44 +</configuration>
2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParserException.java Sun Dec 22 21:02:37 2013 +0100
2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParserException.java Sun Dec 22 22:02:44 2013 +0100
2.3 @@ -21,7 +21,7 @@
2.4 *
2.5 * @author Ing. František Kučera (frantovo.cz)
2.6 */
2.7 -public class CLIParserException extends Exception {
2.8 +public class CLIParserException extends DKException {
2.9
2.10 public CLIParserException() {
2.11 }
3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 22 21:02:37 2013 +0100
3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 22 22:02:44 2013 +0100
3.3 @@ -20,8 +20,12 @@
3.4 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
3.5 import info.globalcode.sql.dk.CLIOptions.MODE;
3.6 import info.globalcode.sql.dk.configuration.Configuration;
3.7 +import info.globalcode.sql.dk.configuration.ConfigurationException;
3.8 +import java.io.IOException;
3.9 import java.util.logging.Level;
3.10 import java.util.logging.Logger;
3.11 +import javax.xml.bind.JAXBContext;
3.12 +import javax.xml.bind.Unmarshaller;
3.13
3.14 /**
3.15 *
3.16 @@ -82,7 +86,7 @@
3.17 }
3.18
3.19 @Override
3.20 - public Configuration getConfiguration() {
3.21 + public Configuration getConfiguration() throws ConfigurationException {
3.22 if (configuration == null) {
3.23 configuration = loadConfiguration();
3.24 }
3.25 @@ -90,15 +94,27 @@
3.26 }
3.27
3.28 private void installDefaultConfiguration() {
3.29 - /**
3.30 - * TODO: check config folder/file and create it if missing
3.31 - */
3.32 + Constants.DIR.mkdir();
3.33 +
3.34 + if (Constants.CONFIG_FILE.exists()) {
3.35 + log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
3.36 + } else {
3.37 + try {
3.38 + Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
3.39 + } catch (IOException e) {
3.40 + log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e);
3.41 + }
3.42 + }
3.43 +
3.44 }
3.45
3.46 - private Configuration loadConfiguration() {
3.47 - /**
3.48 - * TODO: load configuration from XML
3.49 - */
3.50 - return null;
3.51 + private Configuration loadConfiguration() throws ConfigurationException {
3.52 + try {
3.53 + JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
3.54 + Unmarshaller u = jaxb.createUnmarshaller();
3.55 + return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
3.56 + } catch (Exception e) {
3.57 + throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
3.58 + }
3.59 }
3.60 }
4.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Constants.java Sun Dec 22 21:02:37 2013 +0100
4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Constants.java Sun Dec 22 22:02:44 2013 +0100
4.3 @@ -17,6 +17,8 @@
4.4 */
4.5 package info.globalcode.sql.dk;
4.6
4.7 +import java.io.File;
4.8 +
4.9 /**
4.10 *
4.11 * @author Ing. František Kučera (frantovo.cz)
4.12 @@ -27,6 +29,13 @@
4.13 public static final String LICENSE_FILE = "info/globalcode/sql/dk/license.txt";
4.14 public static final String VERSION_FILE = "info/globalcode/sql/dk/version.txt";
4.15 public static final String HELP_FILE = "info/globalcode/sql/dk/help.txt";
4.16 + private static final File HOME_DIR = new File(System.getProperty("user.home"));
4.17 + /**
4.18 + * Directory where config and log files are stored.
4.19 + */
4.20 + public static final File DIR = new File(HOME_DIR, ".sql-dk");
4.21 + public static final File CONFIG_FILE = new File(DIR, "config.xml");
4.22 + public static final String EXAMPLE_CONFIG_FILE = "info/globalcode/sql/dk/example-config.xml";
4.23
4.24 private Constants() {
4.25 }
5.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Sun Dec 22 21:02:37 2013 +0100
5.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java Sun Dec 22 22:02:44 2013 +0100
5.3 @@ -18,9 +18,15 @@
5.4 package info.globalcode.sql.dk;
5.5
5.6 import info.globalcode.sql.dk.configuration.NameIdentified;
5.7 +import java.io.BufferedReader;
5.8 +import java.io.File;
5.9 +import java.io.IOException;
5.10 +import java.io.InputStreamReader;
5.11 +import java.io.PrintWriter;
5.12 import java.util.ArrayList;
5.13 import java.util.Collection;
5.14 import java.util.Map;
5.15 +import java.util.logging.Level;
5.16
5.17 /**
5.18 *
5.19 @@ -95,4 +101,22 @@
5.20
5.21 return null;
5.22 }
5.23 +
5.24 + /**
5.25 + * Copy file from Java resources to file system.
5.26 + */
5.27 + public static void installResource(String resourceName, File target) throws IOException {
5.28 + try (BufferedReader reader = new BufferedReader(new InputStreamReader(Functions.class.getClassLoader().getResourceAsStream(resourceName)))) {
5.29 + try (PrintWriter writer = new PrintWriter(target)) {
5.30 + while (true) {
5.31 + String line = reader.readLine();
5.32 + if (line == null) {
5.33 + break;
5.34 + } else {
5.35 + writer.println(line);
5.36 + }
5.37 + }
5.38 + }
5.39 + }
5.40 + }
5.41 }
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/ConfigurationException.java Sun Dec 22 22:02:44 2013 +0100
6.3 @@ -0,0 +1,42 @@
6.4 +/**
6.5 + * SQL-DK
6.6 + * Copyright © 2013 František Kučera (frantovo.cz)
6.7 + *
6.8 + * This program is free software: you can redistribute it and/or modify
6.9 + * it under the terms of the GNU General Public License as published by
6.10 + * the Free Software Foundation, either version 3 of the License, or
6.11 + * (at your option) any later version.
6.12 + *
6.13 + * This program is distributed in the hope that it will be useful,
6.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6.16 + * GNU General Public License for more details.
6.17 + *
6.18 + * You should have received a copy of the GNU General Public License
6.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
6.20 + */
6.21 +package info.globalcode.sql.dk.configuration;
6.22 +
6.23 +import info.globalcode.sql.dk.DKException;
6.24 +
6.25 +/**
6.26 + *
6.27 + * @author Ing. František Kučera (frantovo.cz)
6.28 + */
6.29 +public class ConfigurationException extends DKException {
6.30 +
6.31 + public ConfigurationException() {
6.32 + }
6.33 +
6.34 + public ConfigurationException(String message) {
6.35 + super(message);
6.36 + }
6.37 +
6.38 + public ConfigurationException(Throwable cause) {
6.39 + super(cause);
6.40 + }
6.41 +
6.42 + public ConfigurationException(String message, Throwable cause) {
6.43 + super(message, cause);
6.44 + }
6.45 +}
7.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/ConfigurationProvider.java Sun Dec 22 21:02:37 2013 +0100
7.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/ConfigurationProvider.java Sun Dec 22 22:02:44 2013 +0100
7.3 @@ -23,5 +23,5 @@
7.4 */
7.5 public interface ConfigurationProvider {
7.6
7.7 - public Configuration getConfiguration();
7.8 + public Configuration getConfiguration() throws ConfigurationException;
7.9 }