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
franta-hg@2
     1
/**
franta-hg@2
     2
 * copy-image-resizer
franta-hg@2
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@2
     4
 *
franta-hg@2
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@2
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@2
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@2
     8
 * (at your option) any later version.
franta-hg@2
     9
 *
franta-hg@2
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@2
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@2
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@2
    13
 * GNU General Public License for more details.
franta-hg@2
    14
 *
franta-hg@2
    15
 * You should have received a copy of the GNU General Public License
franta-hg@2
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@2
    17
 */
franta-hg@4
    18
package cz.frantovo.copyImageResizer.cli;
franta-hg@2
    19
franta-hg@12
    20
import cz.frantovo.copyImageResizer.Counters;
franta-hg@12
    21
import cz.frantovo.copyImageResizer.InvalidOptionsException;
franta-hg@5
    22
import cz.frantovo.copyImageResizer.RecursiveException;
franta-hg@5
    23
import cz.frantovo.copyImageResizer.RecursiveImageResizer;
franta-hg@5
    24
import cz.frantovo.copyImageResizer.RecursiveOptions;
franta-hg@5
    25
import cz.frantovo.copyImageResizer.ResizeException;
franta-hg@5
    26
import java.util.logging.Level;
franta-hg@12
    27
import java.util.logging.LogRecord;
franta-hg@5
    28
import java.util.logging.Logger;
franta-hg@5
    29
franta-hg@2
    30
/**
franta-hg@2
    31
 *
franta-hg@2
    32
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    33
 */
franta-hg@3
    34
public class CLIStarter {
franta-hg@2
    35
franta-hg@5
    36
	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
franta-hg@5
    37
franta-hg@18
    38
	// help:exit-codes
franta-hg@18
    39
	public static final int EXIT_SUCCESS = 0; // doc:success
franta-hg@18
    40
	public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug)
franta-hg@18
    41
	// 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
franta-hg@18
    42
	//public static final int EXIT_SQL_ERROR = 3; // ___:SQL error
franta-hg@18
    43
	public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error
franta-hg@18
    44
	public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error
franta-hg@18
    45
	//public static final int EXIT_CONFIGURATION_ERROR = 6; // ___:configuration error
franta-hg@18
    46
	public static final int EXIT_IMAGE_ERROR = 7; // doc:image error
franta-hg@18
    47
	public static final int EXIT_RECURSIVE_ERROR = 8; // doc:recursive error
franta-hg@18
    48
franta-hg@2
    49
	public static void main(String[] args) {
franta-hg@5
    50
franta-hg@18
    51
		int exitCode;
franta-hg@18
    52
franta-hg@5
    53
		try {
franta-hg@5
    54
			CLIParser parser = new CLIParser();
franta-hg@5
    55
			RecursiveOptions options = parser.parseOptions(args);
franta-hg@12
    56
			options.validate();
franta-hg@11
    57
			RecursiveImageResizer resizer = new RecursiveImageResizer(options);
franta-hg@12
    58
			Counters counters = resizer.resize();
franta-hg@18
    59
franta-hg@18
    60
			if (options.getErrorMode() == RecursiveOptions.ERROR_MODE.SILENT_SKIP || counters.get(Counters.COUNTER_TYPE.SKIPPED_ERROR) == 0) {
franta-hg@18
    61
				log.log(Level.INFO, "Recursive copy/resize succesfully finished. {0}", counters);
franta-hg@18
    62
				exitCode = EXIT_SUCCESS;
franta-hg@18
    63
			} else {
franta-hg@18
    64
				log.log(Level.SEVERE, "Recursive copy/resize finished with errors. {0}", counters);
franta-hg@18
    65
				exitCode = EXIT_IMAGE_ERROR;
franta-hg@18
    66
			}
franta-hg@5
    67
		} catch (CLIParserException e) {
franta-hg@5
    68
			log.log(Level.SEVERE, "Unable to parse CLI options", e);
franta-hg@18
    69
			exitCode = EXIT_CLI_PARSE_ERROR;
franta-hg@12
    70
		} catch (InvalidOptionsException e) {
franta-hg@12
    71
			log.log(Level.SEVERE, "Invalid CLI options", e);
franta-hg@12
    72
			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
franta-hg@12
    73
				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
franta-hg@12
    74
				r.setThrown(p.getException());
franta-hg@12
    75
				r.setParameters(new Object[]{p.getDescription()});
franta-hg@12
    76
				log.log(r);
franta-hg@12
    77
			}
franta-hg@18
    78
			exitCode = EXIT_CLI_VALIDATE_ERROR;
franta-hg@5
    79
		} catch (RecursiveException e) {
franta-hg@5
    80
			log.log(Level.SEVERE, "Error while processing filesystem hierarchy", e);
franta-hg@18
    81
			exitCode = EXIT_RECURSIVE_ERROR;
franta-hg@5
    82
		} catch (ResizeException e) {
franta-hg@5
    83
			log.log(Level.SEVERE, "Error while resizing image", e);
franta-hg@18
    84
			exitCode = EXIT_IMAGE_ERROR;
franta-hg@5
    85
		}
franta-hg@18
    86
franta-hg@18
    87
		System.exit(exitCode);
franta-hg@2
    88
	}
franta-hg@5
    89
franta-hg@2
    90
}