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.Collection; |
20 | import java.util.Collections; |
21 | |
22 | import org.springframework.batch.classify.BinaryExceptionClassifier; |
23 | import org.springframework.batch.classify.Classifier; |
24 | import org.springframework.batch.core.Step; |
25 | import org.springframework.batch.core.StepExecution; |
26 | import org.springframework.batch.item.file.FlatFileParseException; |
27 | |
28 | /** |
29 | * <p> |
30 | * {@link SkipPolicy} that determines whether or not reading should continue |
31 | * based upon how many items have been skipped. This is extremely useful |
32 | * behavior, as it allows you to skip records, but will throw a |
33 | * {@link SkipLimitExceededException} if a set limit has been exceeded. For |
34 | * example, it is generally advisable to skip {@link FlatFileParseException}s, |
35 | * however, if the vast majority of records are causing exceptions, the file is |
36 | * likely bad. |
37 | * </p> |
38 | * |
39 | * <p> |
40 | * Furthermore, it is also likely that you only want to skip certain exceptions. |
41 | * {@link FlatFileParseException} is a good example of an exception you will |
42 | * likely want to skip, but a {@link FileNotFoundException} should cause |
43 | * immediate termination of the {@link Step}. Because it would be impossible for |
44 | * a general purpose policy to determine all the types of exceptions that should |
45 | * be skipped from those that shouldn't, two lists are passed in, with all |
46 | * of the exceptions that are 'fatal' and 'skippable'. The two lists are not |
47 | * enforced to be exclusive, they are prioritized instead - exceptions that are |
48 | * fatal will never be skipped, regardless whether the exception can also be |
49 | * classified as skippable. |
50 | * </p> |
51 | * |
52 | * @author Ben Hale |
53 | * @author Lucas Ward |
54 | * @author Robert Kasanicky |
55 | * @author Dave Syer |
56 | */ |
57 | public class LimitCheckingItemSkipPolicy implements SkipPolicy { |
58 | |
59 | private final int skipLimit; |
60 | |
61 | private final Classifier<Throwable, Boolean> fatalExceptionClassifier; |
62 | |
63 | private final Classifier<Throwable, Boolean> skippableExceptionClassifier; |
64 | |
65 | /** |
66 | * Convenience constructor that assumes all exception types are skippable |
67 | * and none are fatal. |
68 | * @param skipLimit the number of exceptions allowed to skip |
69 | */ |
70 | public LimitCheckingItemSkipPolicy(int skipLimit) { |
71 | this(skipLimit, Collections.<Class<? extends Throwable>> singleton(Exception.class), Collections |
72 | .<Class<? extends Throwable>> emptyList()); |
73 | } |
74 | |
75 | /** |
76 | * |
77 | * @param skipLimit the number of skippable exceptions that are allowed to |
78 | * be skipped |
79 | * @param skippableExceptions exception classes that can be skipped |
80 | * (non-critical) |
81 | * @param fatalExceptions exception classes that should never be skipped |
82 | */ |
83 | public LimitCheckingItemSkipPolicy(int skipLimit, Collection<Class<? extends Throwable>> skippableExceptions, |
84 | Collection<Class<? extends Throwable>> fatalExceptions) { |
85 | this(skipLimit, new BinaryExceptionClassifier(skippableExceptions), new BinaryExceptionClassifier( |
86 | fatalExceptions)); |
87 | } |
88 | |
89 | /** |
90 | * |
91 | * @param skipLimit the number of skippable exceptions that are allowed to |
92 | * be skipped |
93 | * @param skippableExceptionClassifier exception classifier for those that |
94 | * can be skipped (non-critical) |
95 | * @param fatalExceptionClassifier exception classifier for classes that |
96 | * should never be skipped |
97 | */ |
98 | public LimitCheckingItemSkipPolicy(int skipLimit, Classifier<Throwable, Boolean> skippableExceptionClassifier, |
99 | Classifier<Throwable, Boolean> fatalExceptionClassifier) { |
100 | this.skipLimit = skipLimit; |
101 | this.skippableExceptionClassifier = skippableExceptionClassifier; |
102 | this.fatalExceptionClassifier = fatalExceptionClassifier; |
103 | } |
104 | |
105 | /** |
106 | * Given the provided exception and skip count, determine whether or not |
107 | * processing should continue for the given exception. If the exception is |
108 | * not within the list of 'skippable exceptions' or belongs to the list of |
109 | * 'fatal exceptions', false will be returned. If the exception is within |
110 | * the skippable list (and not in the fatal list), and {@link StepExecution} |
111 | * skipCount is greater than the skipLimit, then a |
112 | * {@link SkipLimitExceededException} will be thrown. |
113 | */ |
114 | public boolean shouldSkip(Throwable t, int skipCount) { |
115 | if (fatalExceptionClassifier.classify(t)) { |
116 | return false; |
117 | } |
118 | if (skippableExceptionClassifier.classify(t)) { |
119 | if (skipCount < skipLimit) { |
120 | return true; |
121 | } |
122 | else { |
123 | throw new SkipLimitExceededException(skipLimit, t); |
124 | } |
125 | } |
126 | else { |
127 | return false; |
128 | } |
129 | } |
130 | |
131 | } |