java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Dec 2013 22:02:44 +0100
branchv_0
changeset 33 04db6ccd6c48
parent 26 4ec8e5534eb9
child 34 9335cf31c0f2
permissions -rw-r--r--
configuration loading from XML

/**
 * 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;

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;

/**
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class CLIStarter implements ConfigurationProvider {

	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
	private CLIOptions options;
	private Configuration configuration;

	public static void main(String[] args) {

		if (args.length == 0) {
			args = new String[]{CLIParser.Tokens.INFO_HELP};
		}

		try {
			CLIParser parser = new CLIParser();
			CLIOptions options = parser.parseOptions(args);
			options.validate();
			CLIStarter starter = new CLIStarter(options);
			starter.installDefaultConfiguration();
			starter.process();
		} catch (CLIParserException e) {
			log.log(Level.SEVERE, "Unable to parse CLI options", e);
		} catch (InvalidOptionsException e) {
			log.log(Level.SEVERE, "Invalid CLI options", e);
		}
	}

	public CLIStarter(CLIOptions options) {
		this.options = options;
	}

	private void process() {
		/** Show info */
		if (!options.getShowInfo().isEmpty()) {
			InfoLister infoLister = new InfoLister(System.err, this);
			infoLister.showInfo(options);
		}

		MODE mode = options.getMode();
		switch (mode) {
			case QUERY_NOW:
				break;
			case PREPARE_BATCH:
				break;
			case EXECUTE_BATCH:
				break;
			case JUST_SHOW_INFO:
				// already done above
				break;
			default:
				log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
				break;
		}
	}

	@Override
	public Configuration getConfiguration() throws ConfigurationException {
		if (configuration == null) {
			configuration = loadConfiguration();
		}
		return configuration;
	}

	private void installDefaultConfiguration() {
		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() 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);
		}
	}
}