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 java.io.IOException;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.Unmarshaller;
32 * @author Ing. František Kučera (frantovo.cz)
34 public class CLIStarter implements ConfigurationProvider {
36 private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
37 private CLIOptions options;
38 private Configuration configuration;
40 public static void main(String[] args) {
42 if (args.length == 0) {
43 args = new String[]{CLIParser.Tokens.INFO_HELP};
47 CLIParser parser = new CLIParser();
48 CLIOptions options = parser.parseOptions(args);
50 CLIStarter starter = new CLIStarter(options);
51 starter.installDefaultConfiguration();
53 } catch (CLIParserException e) {
54 log.log(Level.SEVERE, "Unable to parse CLI options", e);
55 } catch (InvalidOptionsException e) {
56 log.log(Level.SEVERE, "Invalid CLI options", e);
60 public CLIStarter(CLIOptions options) {
61 this.options = options;
64 private void process() {
66 if (!options.getShowInfo().isEmpty()) {
67 InfoLister infoLister = new InfoLister(System.err, this);
68 infoLister.showInfo(options);
71 MODE mode = options.getMode();
83 log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
89 public Configuration getConfiguration() throws ConfigurationException {
90 if (configuration == null) {
91 configuration = loadConfiguration();
96 private void installDefaultConfiguration() {
97 Constants.DIR.mkdir();
99 if (Constants.CONFIG_FILE.exists()) {
100 log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
103 Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
104 } catch (IOException e) {
105 log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e);
111 private Configuration loadConfiguration() throws ConfigurationException {
113 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
114 Unmarshaller u = jaxb.createUnmarshaller();
115 return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
116 } catch (Exception e) {
117 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);