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