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