EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.repeat.exception]

COVERAGE SUMMARY FOR SOURCE FILE [RethrowOnThresholdExceptionHandler.java]

nameclass, %method, %block, %line, %
RethrowOnThresholdExceptionHandler.java100% (3/3)100% (11/11)100% (137/137)100% (29/29)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class RethrowOnThresholdExceptionHandler100% (1/1)100% (6/6)100% (107/107)100% (23/23)
<static initializer> 100% (1/1)100% (6/6)100% (1/1)
RethrowOnThresholdExceptionHandler (): void 100% (1/1)100% (16/16)100% (5/5)
getCounter (RepeatContext, RethrowOnThresholdExceptionHandler$IntegerHolder):... 100% (1/1)100% (20/20)100% (2/2)
handleException (RepeatContext, Throwable): void 100% (1/1)100% (25/25)100% (8/8)
setThresholds (Map): void 100% (1/1)100% (36/36)100% (5/5)
setUseParent (boolean): void 100% (1/1)100% (4/4)100% (2/2)
     
class RethrowOnThresholdExceptionHandler$1100% (1/1)100% (2/2)100% (8/8)100% (2/2)
RethrowOnThresholdExceptionHandler$1 (RethrowOnThresholdExceptionHandler): void 100% (1/1)100% (6/6)100% (1/1)
classify (Throwable): RethrowOnThresholdExceptionHandler$IntegerHolder 100% (1/1)100% (2/2)100% (1/1)
     
class RethrowOnThresholdExceptionHandler$IntegerHolder100% (1/1)100% (3/3)100% (22/22)100% (5/5)
RethrowOnThresholdExceptionHandler$IntegerHolder (int): void 100% (1/1)100% (6/6)100% (3/3)
getValue (): int 100% (1/1)100% (3/3)100% (1/1)
toString (): String 100% (1/1)100% (13/13)100% (1/1)

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 
17package org.springframework.batch.repeat.exception;
18 
19import java.util.HashMap;
20import java.util.Map;
21import java.util.Map.Entry;
22 
23import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25import org.springframework.batch.classify.Classifier;
26import org.springframework.batch.classify.SubclassClassifier;
27import org.springframework.batch.repeat.RepeatContext;
28import org.springframework.batch.repeat.context.RepeatContextCounter;
29import 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 */
40public 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}

[all classes][org.springframework.batch.repeat.exception]
EMMA 2.0.5312 (C) Vladimir Roubtsov