| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.core.step.skip; |
| 17 | |
| 18 | import java.io.FileNotFoundException; |
| 19 | import java.util.Collections; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import org.springframework.batch.core.Step; |
| 26 | import org.springframework.batch.core.StepExecution; |
| 27 | import org.springframework.batch.item.file.FlatFileParseException; |
| 28 | import org.springframework.batch.support.ExceptionClassifier; |
| 29 | import org.springframework.batch.support.SubclassExceptionClassifier; |
| 30 | |
| 31 | /** |
| 32 | * <p> |
| 33 | * {@link ItemSkipPolicy} that determines whether or not reading should continue |
| 34 | * based upon how many items have been skipped. This is extremely useful |
| 35 | * behavior, as it allows you to skip records, but will throw a |
| 36 | * {@link SkipLimitExceededException} if a set limit has been exceeded. For |
| 37 | * example, it is generally advisable to skip {@link FlatFileParseException}s, |
| 38 | * however, if the vast majority of records are causing exceptions, the file is |
| 39 | * likely bad. |
| 40 | * </p> |
| 41 | * |
| 42 | * <p> |
| 43 | * Furthermore, it is also likely that you only want to skip certain exceptions. |
| 44 | * {@link FlatFileParseException} is a good example of an exception you will |
| 45 | * likely want to skip, but a {@link FileNotFoundException} should cause |
| 46 | * immediate termination of the {@link Step}. Because it would be impossible |
| 47 | * for a general purpose policy to determine all the types of exceptions that |
| 48 | * should be skipped from those that shouldn't, two lists must be passed in, |
| 49 | * with all of the exceptions that are 'fatal' and 'skippable'. The two lists |
| 50 | * are not enforced to be exclusive, they are prioritized instead - exceptions |
| 51 | * that are fatal will never be skipped, regardless whether the exception can |
| 52 | * also be classified as skippable. |
| 53 | * </p> |
| 54 | * |
| 55 | * @author Ben Hale |
| 56 | * @author Lucas Ward |
| 57 | * @author Robert Kasanicky |
| 58 | */ |
| 59 | public class LimitCheckingItemSkipPolicy implements ItemSkipPolicy { |
| 60 | |
| 61 | /** |
| 62 | * Label for classifying skippable exceptions. |
| 63 | */ |
| 64 | private static final String SKIP = "skip"; |
| 65 | |
| 66 | /** |
| 67 | * Label for classifying fatal exceptions - these are never skipped. |
| 68 | */ |
| 69 | private static final String NEVER_SKIP = "neverSkip"; |
| 70 | |
| 71 | private final int skipLimit; |
| 72 | |
| 73 | private ExceptionClassifier exceptionClassifier; |
| 74 | |
| 75 | /** |
| 76 | * Convenience constructor that assumes all exception types are skippable |
| 77 | * and none are fatal. |
| 78 | * @param skipLimit the number of exceptions allowed to skip |
| 79 | */ |
| 80 | public LimitCheckingItemSkipPolicy(int skipLimit) { |
| 81 | this(skipLimit, Collections.singletonList(Exception.class), Collections.EMPTY_LIST); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * |
| 86 | * @param skipLimit the number of skippable exceptions that are allowed to |
| 87 | * be skipped |
| 88 | * @param skippableExceptions exception classes that can be skipped |
| 89 | * (non-critical) |
| 90 | * @param fatalExceptions exception classes that should never be skipped |
| 91 | */ |
| 92 | public LimitCheckingItemSkipPolicy(int skipLimit, List skippableExceptions, List fatalExceptions) { |
| 93 | this.skipLimit = skipLimit; |
| 94 | SubclassExceptionClassifier exceptionClassifier = new SubclassExceptionClassifier(); |
| 95 | Map typeMap = new HashMap(); |
| 96 | for (Iterator iterator = skippableExceptions.iterator(); iterator.hasNext();) { |
| 97 | Class throwable = (Class) iterator.next(); |
| 98 | typeMap.put(throwable, SKIP); |
| 99 | } |
| 100 | for (Iterator iterator = fatalExceptions.iterator(); iterator.hasNext();) { |
| 101 | Class throwable = (Class) iterator.next(); |
| 102 | typeMap.put(throwable, NEVER_SKIP); |
| 103 | } |
| 104 | exceptionClassifier.setTypeMap(typeMap); |
| 105 | this.exceptionClassifier = exceptionClassifier; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Given the provided exception and skip count, determine whether or not |
| 110 | * processing should continue for the given exception. If the exception is |
| 111 | * not within the list of 'skippable exceptions' or belongs to the list of |
| 112 | * 'fatal exceptions', false will be returned. If the exception is within |
| 113 | * the skippable list (and not in the fatal list), and {@link StepExecution} |
| 114 | * skipCount is greater than the skipLimit, then a |
| 115 | * {@link SkipLimitExceededException} will be thrown. |
| 116 | */ |
| 117 | public boolean shouldSkip(Throwable t, int skipCount) { |
| 118 | if (exceptionClassifier.classify(t).equals(NEVER_SKIP)) { |
| 119 | return false; |
| 120 | } |
| 121 | if (exceptionClassifier.classify(t).equals(SKIP)) { |
| 122 | if (skipCount < skipLimit) { |
| 123 | return true; |
| 124 | } |
| 125 | else { |
| 126 | throw new SkipLimitExceededException(skipLimit, t); |
| 127 | } |
| 128 | } |
| 129 | else { |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |