| 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.Iterator; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.apache.commons.logging.Log; |
| 24 | import org.apache.commons.logging.LogFactory; |
| 25 | import org.springframework.batch.repeat.RepeatContext; |
| 26 | import org.springframework.batch.repeat.context.RepeatContextCounter; |
| 27 | import org.springframework.batch.support.ExceptionClassifier; |
| 28 | import org.springframework.batch.support.ExceptionClassifierSupport; |
| 29 | import org.springframework.util.Assert; |
| 30 | |
| 31 | /** |
| 32 | * Implementation of {@link ExceptionHandler} that rethrows when exceptions of a |
| 33 | * given type reach a threshold. Requires an {@link ExceptionClassifier} that |
| 34 | * maps exception types to unique keys, and also a map from those keys to |
| 35 | * threshold values (Integer type). |
| 36 | * |
| 37 | * @author Dave Syer |
| 38 | * |
| 39 | */ |
| 40 | public class RethrowOnThresholdExceptionHandler implements ExceptionHandler { |
| 41 | |
| 42 | protected final Log logger = LogFactory |
| 43 | .getLog(RethrowOnThresholdExceptionHandler.class); |
| 44 | |
| 45 | private ExceptionClassifier exceptionClassifier = new ExceptionClassifierSupport(); |
| 46 | |
| 47 | private Map thresholds = new HashMap(); |
| 48 | |
| 49 | private boolean useParent = false; |
| 50 | |
| 51 | /** |
| 52 | * Flag to indicate the the exception counters should be shared between |
| 53 | * sibling contexts in a nested batch. Default is false. |
| 54 | * |
| 55 | * @param useParent |
| 56 | * true if the parent context should be used to store the |
| 57 | * counters. |
| 58 | */ |
| 59 | public void setUseParent(boolean useParent) { |
| 60 | this.useParent = useParent; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Set up the exception handler. Creates a default exception handler and |
| 65 | * threshold that maps all exceptions to a threshold of 0 - all exceptions |
| 66 | * are rethrown by default. |
| 67 | */ |
| 68 | public RethrowOnThresholdExceptionHandler() { |
| 69 | super(); |
| 70 | thresholds.put(ExceptionClassifierSupport.DEFAULT, new Integer(0)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * A map from classifier keys to a threshold value of type Integer. The keys |
| 75 | * are usually String literals, depending on the {@link ExceptionClassifier} |
| 76 | * implementation used. |
| 77 | * |
| 78 | * @param thresholds |
| 79 | * the threshold value map. |
| 80 | */ |
| 81 | public void setThresholds(Map thresholds) { |
| 82 | for (Iterator iter = thresholds.entrySet().iterator(); iter.hasNext();) { |
| 83 | Map.Entry entry = (Map.Entry) iter.next(); |
| 84 | if (!(entry.getKey() instanceof String)) { |
| 85 | logger.warn("Key in thresholds map is not of type String: " |
| 86 | + entry.getKey()); |
| 87 | } |
| 88 | Assert |
| 89 | .state( |
| 90 | entry.getValue() instanceof Integer, |
| 91 | "Threshold value must be of type Integer. " |
| 92 | + "Try using the value-type attribute if you care configuring this map via xml."); |
| 93 | } |
| 94 | this.thresholds = thresholds; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Setter for the {@link ExceptionClassifier} used by this handler. The |
| 99 | * default is to map all throwable instances to |
| 100 | * {@link ExceptionClassifierSupport#DEFAULT}, which are then mapped to a |
| 101 | * threshold of 0 by the {@link #setThresholds(Map)} map. |
| 102 | * |
| 103 | * @param exceptionClassifier |
| 104 | */ |
| 105 | public void setExceptionClassifier(ExceptionClassifier exceptionClassifier) { |
| 106 | this.exceptionClassifier = exceptionClassifier; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Classify the throwables and decide whether to re-throw based on the |
| 111 | * result. The context is used to accumulate the number of exceptions of the |
| 112 | * same type according to the classifier. |
| 113 | * |
| 114 | * @throws Throwable |
| 115 | * @see ExceptionHandler#handleException(RepeatContext, Throwable) |
| 116 | */ |
| 117 | public void handleException(RepeatContext context, Throwable throwable) |
| 118 | throws Throwable { |
| 119 | |
| 120 | Object key = exceptionClassifier.classify(throwable); |
| 121 | RepeatContextCounter counter = getCounter(context, key); |
| 122 | counter.increment(); |
| 123 | int count = counter.getCount(); |
| 124 | Integer threshold = (Integer) thresholds.get(key); |
| 125 | if (threshold == null || count > threshold.intValue()) { |
| 126 | throw throwable; |
| 127 | } |
| 128 | |
| 129 | } |
| 130 | |
| 131 | private RepeatContextCounter getCounter(RepeatContext context, Object key) { |
| 132 | String attribute = RethrowOnThresholdExceptionHandler.class.getName() + "." |
| 133 | + key.toString(); |
| 134 | // Creates a new counter and stores it in the correct context: |
| 135 | return new RepeatContextCounter(context, attribute, useParent); |
| 136 | } |
| 137 | |
| 138 | } |