3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import info.globalcode.sql.dk.configuration.ConfigurationProvider;
21 import info.globalcode.sql.dk.CLIOptions.MODE;
22 import info.globalcode.sql.dk.configuration.Configuration;
23 import info.globalcode.sql.dk.configuration.ConfigurationException;
24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
25 import info.globalcode.sql.dk.configuration.FormatterDefinition;
26 import info.globalcode.sql.dk.formatting.Formatter;
27 import info.globalcode.sql.dk.formatting.FormatterContext;
28 import info.globalcode.sql.dk.formatting.FormatterException;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.sql.SQLException;
32 import java.util.logging.Level;
33 import java.util.logging.LogRecord;
34 import java.util.logging.Logger;
35 import javax.xml.bind.JAXBContext;
36 import javax.xml.bind.Unmarshaller;
40 * @author Ing. František Kučera (frantovo.cz)
42 public class CLIStarter implements ConfigurationProvider {
44 private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
45 private static final int EXIT_SUCCESS = 0;
46 private static final int EXIT_EXPECTED_ERROR = 3;
47 private static final int EXIT_SQL_ERROR = 4;
48 private CLIOptions options;
49 private Configuration configuration;
51 public static void main(String[] args) {
52 log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
53 int exitCode = EXIT_EXPECTED_ERROR;
55 if (args.length == 0) {
56 args = new String[]{CLIParser.Tokens.INFO_HELP};
60 CLIParser parser = new CLIParser();
61 CLIOptions options = parser.parseOptions(args);
63 CLIStarter starter = new CLIStarter(options);
64 starter.installDefaultConfiguration();
66 log.log(Level.FINE, "All done");
67 exitCode = EXIT_SUCCESS;
68 } catch (CLIParserException e) {
69 log.log(Level.SEVERE, "Unable to parse CLI options", e);
70 } catch (InvalidOptionsException e) {
71 log.log(Level.SEVERE, "Invalid CLI options", e);
72 for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
73 LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
74 r.setThrown(p.getException());
75 r.setParameters(new Object[]{p.getDescription()});
78 } catch (ConfigurationException e) {
79 log.log(Level.SEVERE, "Configuration problem", e);
80 } catch (SQLException e) {
81 log.log(Level.SEVERE, "SQL problem", e);
82 exitCode = EXIT_SQL_ERROR;
83 } catch (FormatterException e) {
84 log.log(Level.SEVERE, "Formatting problem", e);
87 System.exit(exitCode);
90 public CLIStarter(CLIOptions options) {
91 this.options = options;
94 private void process() throws ConfigurationException, SQLException, FormatterException {
95 MODE mode = options.getMode();
98 if (!options.getShowInfo().isEmpty()) {
99 PrintStream infoOut = mode == MODE.JUST_SHOW_INFO ? System.out : System.err;
100 InfoLister infoLister = new InfoLister(infoOut, this, options);
101 infoLister.showInfo();
109 processPrepareBatch();
112 processExecuteBatch();
115 // already done above
118 log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
123 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
124 DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
125 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
126 try (DatabaseConnection c = dd.connect()) {
127 log.log(Level.FINE, "Database connected");
128 Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
129 c.executeQuery(options.getSQLCommand(), f);
133 private void processPrepareBatch() {
136 private void processExecuteBatch() {
140 public Configuration getConfiguration() throws ConfigurationException {
141 if (configuration == null) {
142 configuration = loadConfiguration();
144 return configuration;
147 private void installDefaultConfiguration() throws ConfigurationException {
148 Constants.DIR.mkdir();
150 if (Constants.CONFIG_FILE.exists()) {
151 log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
154 Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
155 log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
156 } catch (IOException e) {
157 throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
162 private Configuration loadConfiguration() throws ConfigurationException {
164 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
165 Unmarshaller u = jaxb.createUnmarshaller();
166 return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
167 } catch (Exception e) {
168 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);