java/parameter-lister/src/info/glogalcode/parameterLister/CLI.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 11 Sep 2016 19:52:26 +0200
changeset 28 bfef9f34e438
parent 21 7d86d90e6e0e
permissions -rw-r--r--
license: GNU GPL v3
     1 /**
     2  * parameter-lister
     3  * Copyright © 2015 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package info.glogalcode.parameterLister;
    19 
    20 import info.glogalcode.parameterLister.modules.TerminalModuleFactory;
    21 import java.util.Arrays;
    22 import java.util.HashMap;
    23 import java.util.Map;
    24 import java.util.ServiceLoader;
    25 import java.util.logging.Level;
    26 import java.util.logging.Logger;
    27 
    28 /**
    29  *
    30  * @author Ing. František Kučera (frantovo.cz)
    31  */
    32 public class CLI {
    33 
    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;
    37 
    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;
    42 
    43 	private static final Logger log = Logger.getLogger(CLI.class.getName());
    44 
    45 	public static void main(String[] args) {
    46 
    47 		try {
    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));
    51 
    52 			String moduleName = System.getenv(ENV_OUTPUT_MODULE_NAME);
    53 			OutputModuleFactory moduleFactory = modules.getOrDefault(moduleName, modules.get(DEFAULT_MODULE));
    54 			OutputModule module = moduleFactory.createModule();
    55 
    56 			try {
    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);
    61 			}
    62 		} catch (Exception e) {
    63 			log.log(Level.SEVERE, "Unexpected exception, probably bug.", e);
    64 			System.exit(EXIT_UNEXPECTED_ERROR);
    65 
    66 		}
    67 		System.exit(EXIT_SUCCESS);
    68 	}
    69 
    70 }