franta-hg@28: /** franta-hg@28: * parameter-lister franta-hg@28: * Copyright © 2015 František Kučera (frantovo.cz) franta-hg@28: * franta-hg@28: * This program is free software: you can redistribute it and/or modify franta-hg@28: * it under the terms of the GNU General Public License as published by franta-hg@28: * the Free Software Foundation, either version 3 of the License, or franta-hg@28: * (at your option) any later version. franta-hg@28: * franta-hg@28: * This program is distributed in the hope that it will be useful, franta-hg@28: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@28: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@28: * GNU General Public License for more details. franta-hg@28: * franta-hg@28: * You should have received a copy of the GNU General Public License franta-hg@28: * along with this program. If not, see . franta-hg@28: */ 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: }