author | František Kučera <franta-hg@frantovo.cz> |
Sat, 10 Sep 2016 23:52:37 +0200 | |
changeset 25 | 584f60104c13 |
parent 21 | 7d86d90e6e0e |
child 28 | bfef9f34e438 |
permissions | -rw-r--r-- |
franta-hg@21 | 1 |
package info.glogalcode.parameterLister; |
franta-hg@21 | 2 |
|
franta-hg@21 | 3 |
import info.glogalcode.parameterLister.modules.TerminalModuleFactory; |
franta-hg@21 | 4 |
import java.util.Arrays; |
franta-hg@21 | 5 |
import java.util.HashMap; |
franta-hg@21 | 6 |
import java.util.Map; |
franta-hg@21 | 7 |
import java.util.ServiceLoader; |
franta-hg@21 | 8 |
import java.util.logging.Level; |
franta-hg@21 | 9 |
import java.util.logging.Logger; |
franta-hg@21 | 10 |
|
franta-hg@21 | 11 |
/** |
franta-hg@21 | 12 |
* |
franta-hg@21 | 13 |
* @author Ing. František Kučera (frantovo.cz) |
franta-hg@21 | 14 |
*/ |
franta-hg@21 | 15 |
public class CLI { |
franta-hg@21 | 16 |
|
franta-hg@21 | 17 |
public static final String ENV_BASE = "PARAMETER_LISTER"; |
franta-hg@21 | 18 |
public static final String ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT"; |
franta-hg@21 | 19 |
public static final String DEFAULT_MODULE = TerminalModuleFactory.MODULE_NAME; |
franta-hg@21 | 20 |
|
franta-hg@21 | 21 |
public static final int EXIT_SUCCESS = 0; |
franta-hg@21 | 22 |
public static final int EXIT_UNEXPECTED_ERROR = 1; |
franta-hg@21 | 23 |
// 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF |
franta-hg@21 | 24 |
public static final int EXIT_MODULE_ERROR = 3; |
franta-hg@21 | 25 |
|
franta-hg@21 | 26 |
private static final Logger log = Logger.getLogger(CLI.class.getName()); |
franta-hg@21 | 27 |
|
franta-hg@21 | 28 |
public static void main(String[] args) { |
franta-hg@21 | 29 |
|
franta-hg@21 | 30 |
try { |
franta-hg@21 | 31 |
ServiceLoader<OutputModuleFactory> modulesLoader = ServiceLoader.load(OutputModuleFactory.class); |
franta-hg@21 | 32 |
final Map<String, OutputModuleFactory> modules = new HashMap<>(); |
franta-hg@21 | 33 |
modulesLoader.forEach(f -> modules.put(f.getName(), f)); |
franta-hg@21 | 34 |
|
franta-hg@21 | 35 |
String moduleName = System.getenv(ENV_OUTPUT_MODULE_NAME); |
franta-hg@21 | 36 |
OutputModuleFactory moduleFactory = modules.getOrDefault(moduleName, modules.get(DEFAULT_MODULE)); |
franta-hg@21 | 37 |
OutputModule module = moduleFactory.createModule(); |
franta-hg@21 | 38 |
|
franta-hg@21 | 39 |
try { |
franta-hg@21 | 40 |
module.process(System.out, Arrays.asList(args)); |
franta-hg@21 | 41 |
} catch (OutputModuleException e) { |
franta-hg@21 | 42 |
log.log(Level.SEVERE, "Error while processing output with module " + module.getClass().getName(), e); |
franta-hg@21 | 43 |
System.exit(EXIT_MODULE_ERROR); |
franta-hg@21 | 44 |
} |
franta-hg@21 | 45 |
} catch (Exception e) { |
franta-hg@21 | 46 |
log.log(Level.SEVERE, "Unexpected exception, probably bug.", e); |
franta-hg@21 | 47 |
System.exit(EXIT_UNEXPECTED_ERROR); |
franta-hg@21 | 48 |
|
franta-hg@21 | 49 |
} |
franta-hg@21 | 50 |
System.exit(EXIT_SUCCESS); |
franta-hg@21 | 51 |
} |
franta-hg@21 | 52 |
|
franta-hg@21 | 53 |
} |