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

COVERAGE SUMMARY FOR SOURCE FILE [SimpleLimitExceptionHandler.java]

nameclass, %method, %block, %line, %
SimpleLimitExceptionHandler.java100% (1/1)75%  (6/8)90%  (88/98)82%  (23/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SimpleLimitExceptionHandler100% (1/1)75%  (6/8)90%  (88/98)82%  (23/28)
SimpleLimitExceptionHandler (int): void 0%   (0/1)0%   (0/6)0%   (0/3)
setFatalExceptionClasses (Collection): void 0%   (0/1)0%   (0/4)0%   (0/2)
SimpleLimitExceptionHandler (): void 100% (1/1)100% (19/19)100% (6/6)
afterPropertiesSet (): void 100% (1/1)100% (50/50)100% (9/9)
handleException (RepeatContext, Throwable): void 100% (1/1)100% (6/6)100% (2/2)
setExceptionClasses (Collection): void 100% (1/1)100% (4/4)100% (2/2)
setLimit (int): void 100% (1/1)100% (4/4)100% (2/2)
setUseParent (boolean): void 100% (1/1)100% (5/5)100% (2/2)

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.Collection;
20import java.util.Collections;
21import java.util.HashMap;
22import java.util.Map;
23 
24import org.springframework.batch.repeat.RepeatContext;
25import org.springframework.beans.factory.InitializingBean;
26 
27/**
28 * Simple implementation of exception handler which looks for given exception
29 * types. If one of the types is found then a counter is incremented and the
30 * limit is checked to determine if it has been exceeded and the Throwable
31 * should be re-thrown. Also allows to specify list of 'fatal' exceptions that
32 * are never subject to counting, but are immediately re-thrown. The fatal list
33 * has higher priority so the two lists needn't be exclusive.
34 * 
35 * @author Dave Syer
36 * @author Robert Kasanicky
37 */
38public class SimpleLimitExceptionHandler implements ExceptionHandler, InitializingBean {
39 
40        private RethrowOnThresholdExceptionHandler delegate = new RethrowOnThresholdExceptionHandler();
41 
42        private Collection<Class<? extends Throwable>> exceptionClasses = Collections
43                        .<Class<? extends Throwable>> singleton(Exception.class);
44 
45        private Collection<Class<? extends Throwable>> fatalExceptionClasses = Collections
46                        .<Class<? extends Throwable>> singleton(Error.class);
47 
48        private int limit = 0;
49 
50        /**
51         * Apply the provided properties to create a delegate handler.
52         * 
53         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
54         */
55        public void afterPropertiesSet() throws Exception {
56                if (limit <= 0) {
57                        return;
58                }
59                Map<Class<? extends Throwable>, Integer> thresholds = new HashMap<Class<? extends Throwable>, Integer>();
60                for (Class<? extends Throwable> type : exceptionClasses) {
61                        thresholds.put(type, limit);
62                }
63                // do the fatalExceptionClasses last so they override the others
64                for (Class<? extends Throwable> type : fatalExceptionClasses) {
65                        thresholds.put(type, 0);
66                }
67                delegate.setThresholds(thresholds);
68        }
69 
70        /**
71         * Flag to indicate the the exception counters should be shared between
72         * sibling contexts in a nested batch (i.e. inner loop). Default is false.
73         * Set this flag to true if you want to count exceptions for the whole
74         * (outer) loop in a typical container.
75         * 
76         * @param useParent true if the parent context should be used to store the
77         * counters.
78         */
79        public void setUseParent(boolean useParent) {
80                delegate.setUseParent(useParent);
81        }
82 
83        /**
84         * Convenience constructor for the {@link SimpleLimitExceptionHandler} to
85         * set the limit.
86         * 
87         * @param limit the limit
88         */
89        public SimpleLimitExceptionHandler(int limit) {
90                this();
91                this.limit = limit;
92        }
93 
94        /**
95         * Default constructor for the {@link SimpleLimitExceptionHandler}.
96         */
97        public SimpleLimitExceptionHandler() {
98                super();
99        }
100 
101        /**
102         * Rethrows only if the limit is breached for this context on the exception
103         * type specified.
104         * 
105         * @see #setExceptionClasses(Collection)
106         * @see #setLimit(int)
107         * 
108         * @see org.springframework.batch.repeat.exception.ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext,
109         * Throwable)
110         */
111        public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
112                delegate.handleException(context, throwable);
113        }
114 
115        /**
116         * The limit on the given exception type within a single context before it
117         * is rethrown.
118         * 
119         * @param limit the limit
120         */
121        public void setLimit(final int limit) {
122                this.limit = limit;
123        }
124 
125        /**
126         * Setter for the exception classes that this handler counts. Defaults to
127         * {@link Exception}. If more exceptionClasses are specified handler uses
128         * single counter that is incremented when one of the recognized exception
129         * exceptionClasses is handled.
130         * @param classes exceptionClasses
131         */
132        public void setExceptionClasses(Collection<Class<? extends Throwable>> classes) {
133                this.exceptionClasses = classes;
134        }
135 
136        /**
137         * Setter for the exception classes that shouldn't be counted, but rethrown
138         * immediately. This list has higher priority than
139         * {@link #setExceptionClasses(Collection)}.
140         * 
141         * @param fatalExceptionClasses defaults to {@link Error}
142         */
143        public void setFatalExceptionClasses(Collection<Class<? extends Throwable>> fatalExceptionClasses) {
144                this.fatalExceptionClasses = fatalExceptionClasses;
145        }
146 
147}

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