expect invalid regexp in prefix/suffix and throw wrapped exception → better exit code
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.sql.SQLException;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 import javax.xml.bind.JAXBContext;
34 import javax.xml.bind.Unmarshaller;
38 * @author Ing. František Kučera (frantovo.cz)
40 public class CLIStarter implements ConfigurationProvider {
42 private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
43 private static final int EXIT_SUCCESS = 0;
44 private static final int EXIT_EXPECTED_ERROR = 3;
45 private static final int EXIT_SQL_ERROR = 4;
46 private CLIOptions options;
47 private Configuration configuration;
49 public static void main(String[] args) {
50 log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
51 int exitCode = EXIT_EXPECTED_ERROR;
53 if (args.length == 0) {
54 args = new String[]{CLIParser.Tokens.INFO_HELP};
58 CLIParser parser = new CLIParser();
59 CLIOptions options = parser.parseOptions(args);
61 CLIStarter starter = new CLIStarter(options);
62 starter.installDefaultConfiguration();
64 log.log(Level.FINE, "All done");
65 exitCode = EXIT_SUCCESS;
66 } catch (CLIParserException e) {
67 log.log(Level.SEVERE, "Unable to parse CLI options", e);
68 } catch (InvalidOptionsException e) {
69 log.log(Level.SEVERE, "Invalid CLI options", e);
70 for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
71 log.log(Level.SEVERE, "Option problem: {0}", p.getDescription());
73 } catch (ConfigurationException e) {
74 log.log(Level.SEVERE, "Configuration problem", e);
75 } catch (SQLException e) {
76 log.log(Level.SEVERE, "SQL problem", e);
77 exitCode = EXIT_SQL_ERROR;
78 } catch (FormatterException e) {
79 log.log(Level.SEVERE, "Formatting problem", e);
82 System.exit(exitCode);
85 public CLIStarter(CLIOptions options) {
86 this.options = options;
89 private void process() throws ConfigurationException, SQLException, FormatterException {
91 if (!options.getShowInfo().isEmpty()) {
92 InfoLister infoLister = new InfoLister(System.err, this);
93 infoLister.showInfo(options);
96 MODE mode = options.getMode();
102 processPrepareBatch();
105 processExecuteBatch();
108 // already done above
111 log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
116 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
117 DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName());
119 throw new ConfigurationException("Database is not configured: " + options.getDatabaseName());
121 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
123 throw new ConfigurationException("Formatter is not configured: " + options.getFormatterName());
125 try (DatabaseConnection c = dd.connect()) {
126 log.log(Level.FINE, "Database connected");
127 Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
128 c.executeQuery(options.getSQLCommand(), f);
134 private void processPrepareBatch() {
137 private void processExecuteBatch() {
141 public Configuration getConfiguration() throws ConfigurationException {
142 if (configuration == null) {
143 configuration = loadConfiguration();
145 return configuration;
148 private void installDefaultConfiguration() throws ConfigurationException {
149 Constants.DIR.mkdir();
151 if (Constants.CONFIG_FILE.exists()) {
152 log.log(Level.FINER, "Config file already exists: {0}", Constants.CONFIG_FILE);
155 Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
156 log.log(Level.FINE, "Installing default config file: {0}", Constants.CONFIG_FILE);
157 } catch (IOException e) {
158 throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e);
163 private Configuration loadConfiguration() throws ConfigurationException {
165 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
166 Unmarshaller u = jaxb.createUnmarshaller();
167 return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
168 } catch (Exception e) {
169 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);