3 * Copyright © 2015 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.glogalcode.parameterLister;
20 import info.glogalcode.parameterLister.modules.TerminalModuleFactory;
21 import java.util.Arrays;
22 import java.util.HashMap;
24 import java.util.ServiceLoader;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
30 * @author Ing. František Kučera (frantovo.cz)
34 public static final String ENV_BASE = "PARAMETER_LISTER";
35 public static final String ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT";
36 public static final String DEFAULT_MODULE = TerminalModuleFactory.MODULE_NAME;
38 public static final int EXIT_SUCCESS = 0;
39 public static final int EXIT_UNEXPECTED_ERROR = 1;
40 // 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
41 public static final int EXIT_MODULE_ERROR = 3;
43 private static final Logger log = Logger.getLogger(CLI.class.getName());
45 public static void main(String[] args) {
48 ServiceLoader<OutputModuleFactory> modulesLoader = ServiceLoader.load(OutputModuleFactory.class);
49 final Map<String, OutputModuleFactory> modules = new HashMap<>();
50 modulesLoader.forEach(f -> modules.put(f.getName(), f));
52 String moduleName = System.getenv(ENV_OUTPUT_MODULE_NAME);
53 OutputModuleFactory moduleFactory = modules.getOrDefault(moduleName, modules.get(DEFAULT_MODULE));
54 OutputModule module = moduleFactory.createModule();
57 module.process(System.out, Arrays.asList(args));
58 } catch (OutputModuleException e) {
59 log.log(Level.SEVERE, "Error while processing output with module " + module.getClass().getName(), e);
60 System.exit(EXIT_MODULE_ERROR);
62 } catch (Exception e) {
63 log.log(Level.SEVERE, "Unexpected exception, probably bug.", e);
64 System.exit(EXIT_UNEXPECTED_ERROR);
67 System.exit(EXIT_SUCCESS);