# HG changeset patch # User František Kučera # Date 1416251157 -3600 # Node ID 27e41d7f5e8d1d0f0cfbe8e0c5b4c61295ffa4c2 # Parent f517bafcf8124bf1f75a7a458ef2fe7fb8d2c47c counters, logging diff -r f517bafcf812 -r 27e41d7f5e8d java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CopyImageResizerException.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CopyImageResizerException.java Mon Nov 17 18:17:13 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CopyImageResizerException.java Mon Nov 17 20:05:57 2014 +0100 @@ -18,10 +18,11 @@ package cz.frantovo.copyImageResizer; /** + * Superclass for all exceptions in this program. * * @author Ing. František Kučera (frantovo.cz) */ -public class CopyImageResizerException extends Exception { +public abstract class CopyImageResizerException extends Exception { public CopyImageResizerException() { } diff -r f517bafcf812 -r 27e41d7f5e8d java/copy-image-resizer/src/cz/frantovo/copyImageResizer/Counters.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/Counters.java Mon Nov 17 20:05:57 2014 +0100 @@ -0,0 +1,77 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +import java.util.EnumMap; +import java.util.Map; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class Counters { + + private final Map data = new EnumMap(COUNTER_TYPE.class); + + public int increment(COUNTER_TYPE counter) { + synchronized (data) { + int value = get(counter); + value++; + data.put(counter, value); + return value; + } + } + + public int get(COUNTER_TYPE counter) { + Integer value = data.get(counter); + return value == null ? 0 : value; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("Counters: "); + + for (COUNTER_TYPE counter : COUNTER_TYPE.values()) { + sb.append(counter); + sb.append("="); + sb.append(get(counter)); + sb.append(" "); + + } + + return sb.toString(); + } + + public static enum COUNTER_TYPE { + + DIRECTORIES, + FILES, + RESIZED, + SKIPPED_SMALLER, + SKIPPED_UNKNOWN_EXTENSION, + SKIPPED_ERROR; + + @Override + public String toString() { + return name().toLowerCase(); + } + + } +} diff -r f517bafcf812 -r 27e41d7f5e8d java/copy-image-resizer/src/cz/frantovo/copyImageResizer/InvalidOptionsException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/InvalidOptionsException.java Mon Nov 17 20:05:57 2014 +0100 @@ -0,0 +1,68 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class InvalidOptionsException extends CopyImageResizerException { + + private final Collection problems = new ArrayList<>(); + + public Collection getProblems() { + return Collections.unmodifiableCollection(problems); + } + + public void addProblem(OptionProblem p) { + problems.add(p); + } + + public boolean hasProblems() { + return !problems.isEmpty(); + } + + public static class OptionProblem { + + private final String description; + private final Throwable exception; + + public OptionProblem(String description) { + this.description = description; + this.exception = null; + } + + public OptionProblem(String description, Throwable exception) { + this.description = description; + this.exception = exception; + } + + public String getDescription() { + return description; + } + + public Throwable getException() { + return exception; + } + } + +} diff -r f517bafcf812 -r 27e41d7f5e8d java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 18:17:13 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 20:05:57 2014 +0100 @@ -37,6 +37,8 @@ public class RecursiveImageResizer { private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName()); + + private final Counters counters = new Counters(); private final SingleImageResizer resizer = new SingleImageResizer(); @@ -46,8 +48,9 @@ this.options = options; } - public void resize() throws RecursiveException, ResizeException { + public Counters resize() throws RecursiveException, ResizeException { resizeDirectory(options.getInput()); + return counters; } private void resizeFile(File inputFile) throws ResizeException { @@ -70,7 +73,7 @@ resizer.resize(image, output, size, format); } } else { - log.log(Level.FINER, "File: {0} has already required size → just copy", inputFileRelative); + log.log(Level.FINER, "File: {0} has already required (or smaller) size → just copy", inputFileRelative); justCopy(inputFile, outputFile); } } diff -r f517bafcf812 -r 27e41d7f5e8d java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java Mon Nov 17 18:17:13 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java Mon Nov 17 20:05:57 2014 +0100 @@ -63,4 +63,26 @@ public void addSize(SizeSpecification size) { sizes.add(size); } + + public void validate() throws InvalidOptionsException { + InvalidOptionsException e = new InvalidOptionsException(); + + if (input == null) { + e.addProblem(new InvalidOptionsException.OptionProblem("input directory must be specified")); + } else if (!input.isDirectory()) { + e.addProblem(new InvalidOptionsException.OptionProblem("input directory must exist and be a directory: " + input)); + } + + if (output == null) { + e.addProblem(new InvalidOptionsException.OptionProblem("output directory must be specified")); + } + + if (sizes.isEmpty()) { + e.addProblem(new InvalidOptionsException.OptionProblem("at least one size (output resolution) must be specified")); + } + + if (e.hasProblems()) { + throw e; + } + } } diff -r f517bafcf812 -r 27e41d7f5e8d java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java Mon Nov 17 18:17:13 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java Mon Nov 17 20:05:57 2014 +0100 @@ -17,11 +17,14 @@ */ package cz.frantovo.copyImageResizer.cli; +import cz.frantovo.copyImageResizer.Counters; +import cz.frantovo.copyImageResizer.InvalidOptionsException; import cz.frantovo.copyImageResizer.RecursiveException; import cz.frantovo.copyImageResizer.RecursiveImageResizer; import cz.frantovo.copyImageResizer.RecursiveOptions; import cz.frantovo.copyImageResizer.ResizeException; import java.util.logging.Level; +import java.util.logging.LogRecord; import java.util.logging.Logger; /** @@ -37,10 +40,20 @@ try { CLIParser parser = new CLIParser(); RecursiveOptions options = parser.parseOptions(args); + options.validate(); RecursiveImageResizer resizer = new RecursiveImageResizer(options); - resizer.resize(); + Counters counters = resizer.resize(); + log.log(Level.INFO, "Recursive copy/resize finished. {0}", counters); } catch (CLIParserException e) { log.log(Level.SEVERE, "Unable to parse CLI options", e); + } catch (InvalidOptionsException e) { + log.log(Level.SEVERE, "Invalid CLI options", e); + for (InvalidOptionsException.OptionProblem p : e.getProblems()) { + LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}"); + r.setThrown(p.getException()); + r.setParameters(new Object[]{p.getDescription()}); + log.log(r); + } } catch (RecursiveException e) { log.log(Level.SEVERE, "Error while processing filesystem hierarchy", e); } catch (ResizeException e) {