1 | /* |
2 | * Copyright 2006-2010 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.util.Map; |
19 | |
20 | import org.springframework.batch.classify.Classifier; |
21 | import org.springframework.batch.classify.SubclassClassifier; |
22 | |
23 | /** |
24 | * A {@link SkipPolicy} that depends on an exception classifier to make its |
25 | * decision, and then delegates to the classifier result. |
26 | * |
27 | * @author Dave Syer |
28 | * |
29 | * @see SubclassClassifier |
30 | */ |
31 | public class ExceptionClassifierSkipPolicy implements SkipPolicy { |
32 | |
33 | private SubclassClassifier<Throwable, SkipPolicy> classifier; |
34 | |
35 | /** |
36 | * The classifier that will be used to choose a delegate policy. |
37 | * |
38 | * @param classifier the classifier to use to choose a delegate policy |
39 | */ |
40 | public void setExceptionClassifier(SubclassClassifier<Throwable, SkipPolicy> classifier) { |
41 | this.classifier = classifier; |
42 | } |
43 | |
44 | /** |
45 | * Setter for policy map. This property should not be changed dynamically - |
46 | * set it once, e.g. in configuration, and then don't change it during a |
47 | * running application. Either this property or the exception classifier |
48 | * directly should be set, but not both. |
49 | * |
50 | * @param policyMap a map of String to {@link SkipPolicy} that will be used |
51 | * to create a {@link Classifier} to locate a policy. |
52 | */ |
53 | public void setPolicyMap(Map<Class<? extends Throwable>, SkipPolicy> policyMap) { |
54 | SubclassClassifier<Throwable, SkipPolicy> subclassClassifier = new SubclassClassifier<Throwable, SkipPolicy>( |
55 | policyMap, new NeverSkipItemSkipPolicy()); |
56 | this.classifier = subclassClassifier; |
57 | } |
58 | |
59 | /** |
60 | * Consult the classifier and find a delegate policy, and then use that to |
61 | * determine the outcome. |
62 | * |
63 | * @param t the throwable to consider |
64 | * @param skipCount the current skip count |
65 | * @return true if the exception can be skipped |
66 | * @throws SkipLimitExceededException if a limit is exceeded |
67 | */ |
68 | public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException { |
69 | return classifier.classify(t).shouldSkip(t, skipCount); |
70 | } |
71 | |
72 | } |