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@33
|
24 |
import java.io.IOException;
|
franta-hg@13
|
25 |
import java.util.logging.Level;
|
franta-hg@13
|
26 |
import java.util.logging.Logger;
|
franta-hg@33
|
27 |
import javax.xml.bind.JAXBContext;
|
franta-hg@33
|
28 |
import javax.xml.bind.Unmarshaller;
|
franta-hg@13
|
29 |
|
franta-hg@1
|
30 |
/**
|
franta-hg@1
|
31 |
*
|
franta-hg@1
|
32 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
33 |
*/
|
franta-hg@20
|
34 |
public class CLIStarter implements ConfigurationProvider {
|
franta-hg@1
|
35 |
|
franta-hg@13
|
36 |
private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
|
franta-hg@20
|
37 |
private CLIOptions options;
|
franta-hg@20
|
38 |
private Configuration configuration;
|
franta-hg@13
|
39 |
|
franta-hg@1
|
40 |
public static void main(String[] args) {
|
franta-hg@21
|
41 |
|
franta-hg@21
|
42 |
if (args.length == 0) {
|
franta-hg@21
|
43 |
args = new String[]{CLIParser.Tokens.INFO_HELP};
|
franta-hg@21
|
44 |
}
|
franta-hg@21
|
45 |
|
franta-hg@13
|
46 |
try {
|
franta-hg@13
|
47 |
CLIParser parser = new CLIParser();
|
franta-hg@13
|
48 |
CLIOptions options = parser.parseOptions(args);
|
franta-hg@14
|
49 |
options.validate();
|
franta-hg@20
|
50 |
CLIStarter starter = new CLIStarter(options);
|
franta-hg@21
|
51 |
starter.installDefaultConfiguration();
|
franta-hg@20
|
52 |
starter.process();
|
franta-hg@13
|
53 |
} catch (CLIParserException e) {
|
franta-hg@14
|
54 |
log.log(Level.SEVERE, "Unable to parse CLI options", e);
|
franta-hg@14
|
55 |
} catch (InvalidOptionsException e) {
|
franta-hg@14
|
56 |
log.log(Level.SEVERE, "Invalid CLI options", e);
|
franta-hg@13
|
57 |
}
|
franta-hg@4
|
58 |
}
|
franta-hg@20
|
59 |
|
franta-hg@20
|
60 |
public CLIStarter(CLIOptions options) {
|
franta-hg@20
|
61 |
this.options = options;
|
franta-hg@20
|
62 |
}
|
franta-hg@20
|
63 |
|
franta-hg@20
|
64 |
private void process() {
|
franta-hg@20
|
65 |
/** Show info */
|
franta-hg@20
|
66 |
if (!options.getShowInfo().isEmpty()) {
|
franta-hg@20
|
67 |
InfoLister infoLister = new InfoLister(System.err, this);
|
franta-hg@20
|
68 |
infoLister.showInfo(options);
|
franta-hg@20
|
69 |
}
|
franta-hg@20
|
70 |
|
franta-hg@20
|
71 |
MODE mode = options.getMode();
|
franta-hg@20
|
72 |
switch (mode) {
|
franta-hg@20
|
73 |
case QUERY_NOW:
|
franta-hg@20
|
74 |
break;
|
franta-hg@20
|
75 |
case PREPARE_BATCH:
|
franta-hg@20
|
76 |
break;
|
franta-hg@20
|
77 |
case EXECUTE_BATCH:
|
franta-hg@20
|
78 |
break;
|
franta-hg@20
|
79 |
case JUST_SHOW_INFO:
|
franta-hg@20
|
80 |
// already done above
|
franta-hg@20
|
81 |
break;
|
franta-hg@20
|
82 |
default:
|
franta-hg@20
|
83 |
log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
|
franta-hg@20
|
84 |
break;
|
franta-hg@20
|
85 |
}
|
franta-hg@20
|
86 |
}
|
franta-hg@20
|
87 |
|
franta-hg@20
|
88 |
@Override
|
franta-hg@33
|
89 |
public Configuration getConfiguration() throws ConfigurationException {
|
franta-hg@20
|
90 |
if (configuration == null) {
|
franta-hg@20
|
91 |
configuration = loadConfiguration();
|
franta-hg@20
|
92 |
}
|
franta-hg@20
|
93 |
return configuration;
|
franta-hg@20
|
94 |
}
|
franta-hg@20
|
95 |
|
franta-hg@20
|
96 |
private void installDefaultConfiguration() {
|
franta-hg@33
|
97 |
Constants.DIR.mkdir();
|
franta-hg@33
|
98 |
|
franta-hg@33
|
99 |
if (Constants.CONFIG_FILE.exists()) {
|
franta-hg@33
|
100 |
log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE);
|
franta-hg@33
|
101 |
} else {
|
franta-hg@33
|
102 |
try {
|
franta-hg@33
|
103 |
Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE);
|
franta-hg@33
|
104 |
} catch (IOException e) {
|
franta-hg@33
|
105 |
log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e);
|
franta-hg@33
|
106 |
}
|
franta-hg@33
|
107 |
}
|
franta-hg@33
|
108 |
|
franta-hg@20
|
109 |
}
|
franta-hg@20
|
110 |
|
franta-hg@33
|
111 |
private Configuration loadConfiguration() throws ConfigurationException {
|
franta-hg@33
|
112 |
try {
|
franta-hg@33
|
113 |
JAXBContext jaxb = JAXBContext.newInstance(Configuration.class);
|
franta-hg@33
|
114 |
Unmarshaller u = jaxb.createUnmarshaller();
|
franta-hg@33
|
115 |
return (Configuration) u.unmarshal(Constants.CONFIG_FILE);
|
franta-hg@33
|
116 |
} catch (Exception e) {
|
franta-hg@33
|
117 |
throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
|
franta-hg@33
|
118 |
}
|
franta-hg@20
|
119 |
}
|
franta-hg@1
|
120 |
}
|