java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 23:08:26 +0100
changeset 18 a5a36526ff71
parent 12 27e41d7f5e8d
permissions -rw-r--r--
options: --skip-errors and --skip-errors-silently + exit codes
     1 /**
     2  * copy-image-resizer
     3  * Copyright © 2014 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 cz.frantovo.copyImageResizer.cli;
    19 
    20 import cz.frantovo.copyImageResizer.Counters;
    21 import cz.frantovo.copyImageResizer.InvalidOptionsException;
    22 import cz.frantovo.copyImageResizer.RecursiveException;
    23 import cz.frantovo.copyImageResizer.RecursiveImageResizer;
    24 import cz.frantovo.copyImageResizer.RecursiveOptions;
    25 import cz.frantovo.copyImageResizer.ResizeException;
    26 import java.util.logging.Level;
    27 import java.util.logging.LogRecord;
    28 import java.util.logging.Logger;
    29 
    30 /**
    31  *
    32  * @author Ing. František Kučera (frantovo.cz)
    33  */
    34 public class CLIStarter {
    35 
    36 	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
    37 
    38 	// help:exit-codes
    39 	public static final int EXIT_SUCCESS = 0; // doc:success
    40 	public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug)
    41 	// 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
    42 	//public static final int EXIT_SQL_ERROR = 3; // ___:SQL error
    43 	public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error
    44 	public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error
    45 	//public static final int EXIT_CONFIGURATION_ERROR = 6; // ___:configuration error
    46 	public static final int EXIT_IMAGE_ERROR = 7; // doc:image error
    47 	public static final int EXIT_RECURSIVE_ERROR = 8; // doc:recursive error
    48 
    49 	public static void main(String[] args) {
    50 
    51 		int exitCode;
    52 
    53 		try {
    54 			CLIParser parser = new CLIParser();
    55 			RecursiveOptions options = parser.parseOptions(args);
    56 			options.validate();
    57 			RecursiveImageResizer resizer = new RecursiveImageResizer(options);
    58 			Counters counters = resizer.resize();
    59 
    60 			if (options.getErrorMode() == RecursiveOptions.ERROR_MODE.SILENT_SKIP || counters.get(Counters.COUNTER_TYPE.SKIPPED_ERROR) == 0) {
    61 				log.log(Level.INFO, "Recursive copy/resize succesfully finished. {0}", counters);
    62 				exitCode = EXIT_SUCCESS;
    63 			} else {
    64 				log.log(Level.SEVERE, "Recursive copy/resize finished with errors. {0}", counters);
    65 				exitCode = EXIT_IMAGE_ERROR;
    66 			}
    67 		} catch (CLIParserException e) {
    68 			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    69 			exitCode = EXIT_CLI_PARSE_ERROR;
    70 		} catch (InvalidOptionsException e) {
    71 			log.log(Level.SEVERE, "Invalid CLI options", e);
    72 			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
    73 				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
    74 				r.setThrown(p.getException());
    75 				r.setParameters(new Object[]{p.getDescription()});
    76 				log.log(r);
    77 			}
    78 			exitCode = EXIT_CLI_VALIDATE_ERROR;
    79 		} catch (RecursiveException e) {
    80 			log.log(Level.SEVERE, "Error while processing filesystem hierarchy", e);
    81 			exitCode = EXIT_RECURSIVE_ERROR;
    82 		} catch (ResizeException e) {
    83 			log.log(Level.SEVERE, "Error while resizing image", e);
    84 			exitCode = EXIT_IMAGE_ERROR;
    85 		}
    86 
    87 		System.exit(exitCode);
    88 	}
    89 
    90 }