EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[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% (30/30)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class RethrowOnThresholdExceptionHandler100% (1/1)100% (6/6)100% (107/107)100% (24/24)
<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% (6/6)
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.classify.Classifier;
26import org.springframework.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        @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}

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