java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 21:47:33 +0100
branchv_0
changeset 70 02c8eaa425e8
parent 69 0befec5034c2
child 75 43aa4625ab7e
permissions -rw-r--r--
use formatter also for printing info! --list-types
franta-hg@16
     1
/**
franta-hg@16
     2
 * SQL-DK
franta-hg@16
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@16
     4
 *
franta-hg@16
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@16
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@16
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@16
     8
 * (at your option) any later version.
franta-hg@16
     9
 *
franta-hg@16
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@16
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@16
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@16
    13
 * GNU General Public License for more details.
franta-hg@16
    14
 *
franta-hg@16
    15
 * You should have received a copy of the GNU General Public License
franta-hg@16
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@16
    17
 */
franta-hg@1
    18
package info.globalcode.sql.dk;
franta-hg@1
    19
franta-hg@26
    20
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
franta-hg@20
    21
import info.globalcode.sql.dk.CLIOptions.MODE;
franta-hg@26
    22
import info.globalcode.sql.dk.configuration.Configuration;
franta-hg@33
    23
import info.globalcode.sql.dk.configuration.ConfigurationException;
franta-hg@34
    24
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
franta-hg@34
    25
import info.globalcode.sql.dk.configuration.FormatterDefinition;
franta-hg@34
    26
import info.globalcode.sql.dk.formatting.Formatter;
franta-hg@34
    27
import info.globalcode.sql.dk.formatting.FormatterContext;
franta-hg@34
    28
import info.globalcode.sql.dk.formatting.FormatterException;
franta-hg@33
    29
import java.io.IOException;
franta-hg@64
    30
import java.io.PrintStream;
franta-hg@34
    31
import java.sql.SQLException;
franta-hg@13
    32
import java.util.logging.Level;
franta-hg@63
    33
import java.util.logging.LogRecord;
franta-hg@13
    34
import java.util.logging.Logger;
franta-hg@33
    35
import javax.xml.bind.JAXBContext;
franta-hg@33
    36
import javax.xml.bind.Unmarshaller;
franta-hg@13
    37
franta-hg@1
    38
/**
franta-hg@1
    39
 *
franta-hg@1
    40
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@1
    41
 */
franta-hg@20
    42
public class CLIStarter implements ConfigurationProvider {
franta-hg@1
    43
franta-hg@13
    44
	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
franta-hg@56
    45
	private static final int EXIT_SUCCESS = 0;
franta-hg@56
    46
	private static final int EXIT_EXPECTED_ERROR = 3;
franta-hg@56
    47
	private static final int EXIT_SQL_ERROR = 4;
franta-hg@20
    48
	private CLIOptions options;
franta-hg@20
    49
	private Configuration configuration;
franta-hg@13
    50
franta-hg@1
    51
	public static void main(String[] args) {
franta-hg@55
    52
		log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
franta-hg@56
    53
		int exitCode = EXIT_EXPECTED_ERROR;
franta-hg@63
    54
franta-hg@21
    55
		if (args.length == 0) {
franta-hg@21
    56
			args = new String[]{CLIParser.Tokens.INFO_HELP};
franta-hg@21
    57
		}
franta-hg@21
    58
franta-hg@13
    59
		try {
franta-hg@13
    60
			CLIParser parser = new CLIParser();
franta-hg@13
    61
			CLIOptions options = parser.parseOptions(args);
franta-hg@14
    62
			options.validate();
franta-hg@20
    63
			CLIStarter starter = new CLIStarter(options);
franta-hg@21
    64
			starter.installDefaultConfiguration();
franta-hg@20
    65
			starter.process();
franta-hg@55
    66
			log.log(Level.FINE, "All done");
franta-hg@56
    67
			exitCode = EXIT_SUCCESS;
franta-hg@13
    68
		} catch (CLIParserException e) {
franta-hg@14
    69
			log.log(Level.SEVERE, "Unable to parse CLI options", e);
franta-hg@14
    70
		} catch (InvalidOptionsException e) {
franta-hg@14
    71
			log.log(Level.SEVERE, "Invalid CLI options", e);
franta-hg@48
    72
			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
franta-hg@63
    73
				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
franta-hg@63
    74
				r.setThrown(p.getException());
franta-hg@63
    75
				r.setParameters(new Object[]{p.getDescription()});
franta-hg@63
    76
				log.log(r);
franta-hg@48
    77
			}
franta-hg@34
    78
		} catch (ConfigurationException e) {
franta-hg@34
    79
			log.log(Level.SEVERE, "Configuration problem", e);
franta-hg@34
    80
		} catch (SQLException e) {
franta-hg@34
    81
			log.log(Level.SEVERE, "SQL problem", e);
franta-hg@56
    82
			exitCode = EXIT_SQL_ERROR;
franta-hg@34
    83
		} catch (FormatterException e) {
franta-hg@34
    84
			log.log(Level.SEVERE, "Formatting problem", e);
franta-hg@13
    85
		}
franta-hg@56
    86
franta-hg@56
    87
		System.exit(exitCode);
franta-hg@4
    88
	}
franta-hg@20
    89
franta-hg@20
    90
	public CLIStarter(CLIOptions options) {
franta-hg@20
    91
		this.options = options;
franta-hg@20
    92
	}
franta-hg@20
    93
franta-hg@34
    94
	private void process() throws ConfigurationException, SQLException, FormatterException {
franta-hg@64
    95
		MODE mode = options.getMode();
franta-hg@64
    96
franta-hg@20
    97
		/** Show info */
franta-hg@20
    98
		if (!options.getShowInfo().isEmpty()) {
franta-hg@64
    99
			PrintStream infoOut = mode == MODE.JUST_SHOW_INFO ? System.out : System.err;
franta-hg@69
   100
			InfoLister infoLister = new InfoLister(infoOut, this, options);
franta-hg@70
   101
			infoLister.showInfo();
franta-hg@20
   102
		}
franta-hg@20
   103
franta-hg@20
   104
		switch (mode) {
franta-hg@20
   105
			case QUERY_NOW:
franta-hg@34
   106
				processQueryNow();
franta-hg@20
   107
				break;
franta-hg@20
   108
			case PREPARE_BATCH:
franta-hg@34
   109
				processPrepareBatch();
franta-hg@20
   110
				break;
franta-hg@20
   111
			case EXECUTE_BATCH:
franta-hg@34
   112
				processExecuteBatch();
franta-hg@20
   113
				break;
franta-hg@20
   114
			case JUST_SHOW_INFO:
franta-hg@20
   115
				// already done above
franta-hg@20
   116
				break;
franta-hg@20
   117
			default:
franta-hg@20
   118
				log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
franta-hg@20
   119
				break;
franta-hg@20
   120
		}
franta-hg@20
   121
	}
franta-hg@20
   122
franta-hg@34
   123
	private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
franta-hg@34
   124
		DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
franta-hg@34
   125
		if (dd == null) {
franta-hg@34
   126
			throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
franta-hg@34
   127
		} else {
franta-hg@34
   128
			FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
franta-hg@34
   129
			if (fd == null) {
franta-hg@36
   130
				throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
franta-hg@34
   131
			} else {
franta-hg@42
   132
				try (DatabaseConnection c = dd.connect()) {
franta-hg@55
   133
					log.log(Level.FINE, "Database connected");
franta-hg@42
   134
					Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
franta-hg@42
   135
					c.executeQuery(options.getSQLCommand(), f);
franta-hg@42
   136
				}
franta-hg@34
   137
			}
franta-hg@34
   138
		}
franta-hg@34
   139
	}
franta-hg@34
   140
franta-hg@34
   141
	private void processPrepareBatch() {
franta-hg@34
   142
	}
franta-hg@34
   143
franta-hg@34
   144
	private void processExecuteBatch() {
franta-hg@34
   145
	}
franta-hg@34
   146
franta-hg@20
   147
	@Override
franta-hg@33
   148
	public Configuration getConfiguration() throws ConfigurationException {
franta-hg@20
   149
		if (configuration == null) {
franta-hg@20
   150
			configuration = loadConfiguration();
franta-hg@20
   151
		}
franta-hg@20
   152
		return configuration;
franta-hg@20
   153
	}
franta-hg@20
   154
franta-hg@34
   155
	private void installDefaultConfiguration() throws ConfigurationException {
franta-hg@33
   156
		Constants.DIR.mkdir();
franta-hg@33
   157
franta-hg@33
   158
		if (Constants.CONFIG_FILE.exists()) {
franta-hg@55
   159
			log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
franta-hg@33
   160
		} else {
franta-hg@33
   161
			try {
franta-hg@33
   162
				Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
franta-hg@55
   163
				log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
franta-hg@33
   164
			} catch (IOException e) {
franta-hg@34
   165
				throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
franta-hg@33
   166
			}
franta-hg@33
   167
		}
franta-hg@20
   168
	}
franta-hg@20
   169
franta-hg@33
   170
	private Configuration loadConfiguration() throws ConfigurationException {
franta-hg@33
   171
		try {
franta-hg@33
   172
			JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
franta-hg@33
   173
			Unmarshaller u = jaxb.createUnmarshaller();
franta-hg@33
   174
			return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
franta-hg@33
   175
		} catch (Exception e) {
franta-hg@33
   176
			throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
franta-hg@33
   177
		}
franta-hg@20
   178
	}
franta-hg@1
   179
}