franta-hg@2: /** franta-hg@2: * copy-image-resizer franta-hg@2: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@2: * franta-hg@2: * This program is free software: you can redistribute it and/or modify franta-hg@2: * it under the terms of the GNU General Public License as published by franta-hg@2: * the Free Software Foundation, either version 3 of the License, or franta-hg@2: * (at your option) any later version. franta-hg@2: * franta-hg@2: * This program is distributed in the hope that it will be useful, franta-hg@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@2: * GNU General Public License for more details. franta-hg@2: * franta-hg@2: * You should have received a copy of the GNU General Public License franta-hg@2: * along with this program. If not, see . franta-hg@2: */ franta-hg@4: package cz.frantovo.copyImageResizer.cli; franta-hg@2: franta-hg@12: import cz.frantovo.copyImageResizer.Counters; franta-hg@12: import cz.frantovo.copyImageResizer.InvalidOptionsException; franta-hg@5: import cz.frantovo.copyImageResizer.RecursiveException; franta-hg@5: import cz.frantovo.copyImageResizer.RecursiveImageResizer; franta-hg@5: import cz.frantovo.copyImageResizer.RecursiveOptions; franta-hg@5: import cz.frantovo.copyImageResizer.ResizeException; franta-hg@5: import java.util.logging.Level; franta-hg@12: import java.util.logging.LogRecord; franta-hg@5: import java.util.logging.Logger; franta-hg@5: franta-hg@2: /** franta-hg@2: * franta-hg@2: * @author Ing. František Kučera (frantovo.cz) franta-hg@2: */ franta-hg@3: public class CLIStarter { franta-hg@2: franta-hg@5: private static final Logger log = Logger.getLogger(CLIStarter.class.getName()); franta-hg@5: franta-hg@18: // help:exit-codes franta-hg@18: public static final int EXIT_SUCCESS = 0; // doc:success franta-hg@18: public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug) franta-hg@18: // 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF franta-hg@18: //public static final int EXIT_SQL_ERROR = 3; // ___:SQL error franta-hg@18: public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error franta-hg@18: public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error franta-hg@18: //public static final int EXIT_CONFIGURATION_ERROR = 6; // ___:configuration error franta-hg@18: public static final int EXIT_IMAGE_ERROR = 7; // doc:image error franta-hg@18: public static final int EXIT_RECURSIVE_ERROR = 8; // doc:recursive error franta-hg@18: franta-hg@2: public static void main(String[] args) { franta-hg@5: franta-hg@18: int exitCode; franta-hg@18: franta-hg@5: try { franta-hg@5: CLIParser parser = new CLIParser(); franta-hg@5: RecursiveOptions options = parser.parseOptions(args); franta-hg@12: options.validate(); franta-hg@11: RecursiveImageResizer resizer = new RecursiveImageResizer(options); franta-hg@12: Counters counters = resizer.resize(); franta-hg@18: franta-hg@18: if (options.getErrorMode() == RecursiveOptions.ERROR_MODE.SILENT_SKIP || counters.get(Counters.COUNTER_TYPE.SKIPPED_ERROR) == 0) { franta-hg@18: log.log(Level.INFO, "Recursive copy/resize succesfully finished. {0}", counters); franta-hg@18: exitCode = EXIT_SUCCESS; franta-hg@18: } else { franta-hg@18: log.log(Level.SEVERE, "Recursive copy/resize finished with errors. {0}", counters); franta-hg@18: exitCode = EXIT_IMAGE_ERROR; franta-hg@18: } franta-hg@5: } catch (CLIParserException e) { franta-hg@5: log.log(Level.SEVERE, "Unable to parse CLI options", e); franta-hg@18: exitCode = EXIT_CLI_PARSE_ERROR; franta-hg@12: } catch (InvalidOptionsException e) { franta-hg@12: log.log(Level.SEVERE, "Invalid CLI options", e); franta-hg@12: for (InvalidOptionsException.OptionProblem p : e.getProblems()) { franta-hg@12: LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}"); franta-hg@12: r.setThrown(p.getException()); franta-hg@12: r.setParameters(new Object[]{p.getDescription()}); franta-hg@12: log.log(r); franta-hg@12: } franta-hg@18: exitCode = EXIT_CLI_VALIDATE_ERROR; franta-hg@5: } catch (RecursiveException e) { franta-hg@5: log.log(Level.SEVERE, "Error while processing filesystem hierarchy", e); franta-hg@18: exitCode = EXIT_RECURSIVE_ERROR; franta-hg@5: } catch (ResizeException e) { franta-hg@5: log.log(Level.SEVERE, "Error while resizing image", e); franta-hg@18: exitCode = EXIT_IMAGE_ERROR; franta-hg@5: } franta-hg@18: franta-hg@18: System.exit(exitCode); franta-hg@2: } franta-hg@5: franta-hg@2: }