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 | |
17 | package org.springframework.batch.repeat.exception; |
18 | |
19 | import java.util.HashMap; |
20 | import java.util.Map; |
21 | import java.util.Map.Entry; |
22 | |
23 | import org.apache.commons.logging.Log; |
24 | import org.apache.commons.logging.LogFactory; |
25 | import org.springframework.batch.classify.Classifier; |
26 | import org.springframework.batch.classify.SubclassClassifier; |
27 | import org.springframework.batch.repeat.RepeatContext; |
28 | import org.springframework.batch.repeat.context.RepeatContextCounter; |
29 | import org.springframework.util.ObjectUtils; |
30 | |
31 | /** |
32 | * Implementation of {@link ExceptionHandler} that rethrows when exceptions of a |
33 | * given type reach a threshold. Requires an {@link Classifier} that maps |
34 | * exception types to unique keys, and also a map from those keys to threshold |
35 | * values (Integer type). |
36 | * |
37 | * @author Dave Syer |
38 | * |
39 | */ |
40 | public class RethrowOnThresholdExceptionHandler implements ExceptionHandler { |
41 | |
42 | protected static final IntegerHolder ZERO = new IntegerHolder(0); |
43 | |
44 | protected final Log logger = LogFactory.getLog(RethrowOnThresholdExceptionHandler.class); |
45 | |
46 | private Classifier<? super Throwable, IntegerHolder> exceptionClassifier = new Classifier<Throwable, IntegerHolder>() { |
47 | public RethrowOnThresholdExceptionHandler.IntegerHolder classify(Throwable classifiable) { |
48 | return ZERO; |
49 | } |
50 | }; |
51 | |
52 | private boolean useParent = false; |
53 | |
54 | /** |
55 | * Flag to indicate the the exception counters should be shared between |
56 | * sibling contexts in a nested batch. Default is false. |
57 | * |
58 | * @param useParent true if the parent context should be used to store the |
59 | * counters. |
60 | */ |
61 | public void setUseParent(boolean useParent) { |
62 | this.useParent = useParent; |
63 | } |
64 | |
65 | /** |
66 | * Set up the exception handler. Creates a default exception handler and |
67 | * threshold that maps all exceptions to a threshold of 0 - all exceptions |
68 | * are rethrown by default. |
69 | */ |
70 | public RethrowOnThresholdExceptionHandler() { |
71 | super(); |
72 | } |
73 | |
74 | /** |
75 | * A map from exception classes to a threshold value of type Integer. |
76 | * |
77 | * @param thresholds the threshold value map. |
78 | */ |
79 | public void setThresholds(Map<Class<? extends Throwable>, Integer> thresholds) { |
80 | Map<Class<? extends Throwable>, IntegerHolder> typeMap = new HashMap<Class<? extends Throwable>, IntegerHolder>(); |
81 | for (Entry<Class<? extends Throwable>, Integer> entry : thresholds.entrySet()) { |
82 | typeMap.put(entry.getKey(), new IntegerHolder(entry.getValue())); |
83 | } |
84 | exceptionClassifier = new SubclassClassifier<Throwable, IntegerHolder>(typeMap, ZERO); |
85 | } |
86 | |
87 | /** |
88 | * Classify the throwables and decide whether to re-throw based on the |
89 | * result. The context is used to accumulate the number of exceptions of the |
90 | * same type according to the classifier. |
91 | * |
92 | * @throws Throwable |
93 | * @see ExceptionHandler#handleException(RepeatContext, Throwable) |
94 | */ |
95 | public void handleException(RepeatContext context, Throwable throwable) throws Throwable { |
96 | |
97 | IntegerHolder key = exceptionClassifier.classify(throwable); |
98 | |
99 | RepeatContextCounter counter = getCounter(context, key); |
100 | counter.increment(); |
101 | int count = counter.getCount(); |
102 | int threshold = key.getValue(); |
103 | if (count > threshold) { |
104 | throw throwable; |
105 | } |
106 | |
107 | } |
108 | |
109 | private RepeatContextCounter getCounter(RepeatContext context, IntegerHolder key) { |
110 | String attribute = RethrowOnThresholdExceptionHandler.class.getName() + "." + key; |
111 | // Creates a new counter and stores it in the correct context: |
112 | return new RepeatContextCounter(context, attribute, useParent); |
113 | } |
114 | |
115 | /** |
116 | * @author Dave Syer |
117 | * |
118 | */ |
119 | private static class IntegerHolder { |
120 | |
121 | private final int value; |
122 | |
123 | /** |
124 | * @param value |
125 | */ |
126 | public IntegerHolder(int value) { |
127 | this.value = value; |
128 | } |
129 | |
130 | /** |
131 | * Public getter for the value. |
132 | * @return the value |
133 | */ |
134 | public int getValue() { |
135 | return value; |
136 | } |
137 | |
138 | /* |
139 | * (non-Javadoc) |
140 | * |
141 | * @see java.lang.Object#toString() |
142 | */ |
143 | @Override |
144 | public String toString() { |
145 | return ObjectUtils.getIdentityHexString(this) + "." + value; |
146 | } |
147 | |
148 | } |
149 | |
150 | } |