diff -r 0684883953ba -r 7d86d90e6e0e java/parameter-lister/src/info/glogalcode/parameterLister/CLI.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/parameter-lister/src/info/glogalcode/parameterLister/CLI.java Sat Sep 10 23:03:43 2016 +0200 @@ -0,0 +1,53 @@ +package info.glogalcode.parameterLister; + +import info.glogalcode.parameterLister.modules.TerminalModuleFactory; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CLI { + + public static final String ENV_BASE = "PARAMETER_LISTER"; + public static final String ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT"; + public static final String DEFAULT_MODULE = TerminalModuleFactory.MODULE_NAME; + + public static final int EXIT_SUCCESS = 0; + public static final int EXIT_UNEXPECTED_ERROR = 1; + // 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF + public static final int EXIT_MODULE_ERROR = 3; + + private static final Logger log = Logger.getLogger(CLI.class.getName()); + + public static void main(String[] args) { + + try { + ServiceLoader modulesLoader = ServiceLoader.load(OutputModuleFactory.class); + final Map modules = new HashMap<>(); + modulesLoader.forEach(f -> modules.put(f.getName(), f)); + + String moduleName = System.getenv(ENV_OUTPUT_MODULE_NAME); + OutputModuleFactory moduleFactory = modules.getOrDefault(moduleName, modules.get(DEFAULT_MODULE)); + OutputModule module = moduleFactory.createModule(); + + try { + module.process(System.out, Arrays.asList(args)); + } catch (OutputModuleException e) { + log.log(Level.SEVERE, "Error while processing output with module " + module.getClass().getName(), e); + System.exit(EXIT_MODULE_ERROR); + } + } catch (Exception e) { + log.log(Level.SEVERE, "Unexpected exception, probably bug.", e); + System.exit(EXIT_UNEXPECTED_ERROR); + + } + System.exit(EXIT_SUCCESS); + } + +}