franta-hg@28
|
1 |
/**
|
franta-hg@28
|
2 |
* parameter-lister
|
franta-hg@28
|
3 |
* Copyright © 2015 František Kučera (frantovo.cz)
|
franta-hg@28
|
4 |
*
|
franta-hg@28
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@28
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@28
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@28
|
8 |
* (at your option) any later version.
|
franta-hg@28
|
9 |
*
|
franta-hg@28
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@28
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@28
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@28
|
13 |
* GNU General Public License for more details.
|
franta-hg@28
|
14 |
*
|
franta-hg@28
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@28
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@28
|
17 |
*/
|
franta-hg@21
|
18 |
package info.glogalcode.parameterLister;
|
franta-hg@21
|
19 |
|
franta-hg@21
|
20 |
import info.glogalcode.parameterLister.modules.TerminalModuleFactory;
|
franta-hg@21
|
21 |
import java.util.Arrays;
|
franta-hg@21
|
22 |
import java.util.HashMap;
|
franta-hg@21
|
23 |
import java.util.Map;
|
franta-hg@21
|
24 |
import java.util.ServiceLoader;
|
franta-hg@21
|
25 |
import java.util.logging.Level;
|
franta-hg@21
|
26 |
import java.util.logging.Logger;
|
franta-hg@21
|
27 |
|
franta-hg@21
|
28 |
/**
|
franta-hg@21
|
29 |
*
|
franta-hg@21
|
30 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@21
|
31 |
*/
|
franta-hg@21
|
32 |
public class CLI {
|
franta-hg@21
|
33 |
|
franta-hg@21
|
34 |
public static final String ENV_BASE = "PARAMETER_LISTER";
|
franta-hg@21
|
35 |
public static final String ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT";
|
franta-hg@21
|
36 |
public static final String DEFAULT_MODULE = TerminalModuleFactory.MODULE_NAME;
|
franta-hg@21
|
37 |
|
franta-hg@21
|
38 |
public static final int EXIT_SUCCESS = 0;
|
franta-hg@21
|
39 |
public static final int EXIT_UNEXPECTED_ERROR = 1;
|
franta-hg@21
|
40 |
// 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
|
franta-hg@21
|
41 |
public static final int EXIT_MODULE_ERROR = 3;
|
franta-hg@21
|
42 |
|
franta-hg@21
|
43 |
private static final Logger log = Logger.getLogger(CLI.class.getName());
|
franta-hg@21
|
44 |
|
franta-hg@21
|
45 |
public static void main(String[] args) {
|
franta-hg@21
|
46 |
|
franta-hg@21
|
47 |
try {
|
franta-hg@21
|
48 |
ServiceLoader<OutputModuleFactory> modulesLoader = ServiceLoader.load(OutputModuleFactory.class);
|
franta-hg@21
|
49 |
final Map<String, OutputModuleFactory> modules = new HashMap<>();
|
franta-hg@21
|
50 |
modulesLoader.forEach(f -> modules.put(f.getName(), f));
|
franta-hg@21
|
51 |
|
franta-hg@21
|
52 |
String moduleName = System.getenv(ENV_OUTPUT_MODULE_NAME);
|
franta-hg@21
|
53 |
OutputModuleFactory moduleFactory = modules.getOrDefault(moduleName, modules.get(DEFAULT_MODULE));
|
franta-hg@21
|
54 |
OutputModule module = moduleFactory.createModule();
|
franta-hg@21
|
55 |
|
franta-hg@21
|
56 |
try {
|
franta-hg@21
|
57 |
module.process(System.out, Arrays.asList(args));
|
franta-hg@21
|
58 |
} catch (OutputModuleException e) {
|
franta-hg@21
|
59 |
log.log(Level.SEVERE, "Error while processing output with module " + module.getClass().getName(), e);
|
franta-hg@21
|
60 |
System.exit(EXIT_MODULE_ERROR);
|
franta-hg@21
|
61 |
}
|
franta-hg@21
|
62 |
} catch (Exception e) {
|
franta-hg@21
|
63 |
log.log(Level.SEVERE, "Unexpected exception, probably bug.", e);
|
franta-hg@21
|
64 |
System.exit(EXIT_UNEXPECTED_ERROR);
|
franta-hg@21
|
65 |
|
franta-hg@21
|
66 |
}
|
franta-hg@21
|
67 |
System.exit(EXIT_SUCCESS);
|
franta-hg@21
|
68 |
}
|
franta-hg@21
|
69 |
|
franta-hg@21
|
70 |
}
|