EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.core.step.item]

COVERAGE SUMMARY FOR SOURCE FILE [SimpleRetryExceptionHandler.java]

nameclass, %method, %block, %line, %
SimpleRetryExceptionHandler.java100% (1/1)100% (5/5)87%  (74/85)85%  (17/20)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SimpleRetryExceptionHandler100% (1/1)100% (5/5)87%  (74/85)85%  (17/20)
handleException (RepeatContext, Throwable): void 100% (1/1)64%  (16/25)60%  (3/5)
getRepeatContext (): RepeatContext 100% (1/1)80%  (8/10)75%  (3/4)
<static initializer> 100% (1/1)100% (14/14)100% (2/2)
SimpleRetryExceptionHandler (RetryPolicy, ExceptionHandler, Collection): void 100% (1/1)100% (15/15)100% (5/5)
close (RetryContext, RetryCallback, Throwable): void 100% (1/1)100% (21/21)100% (4/4)

1/*
2 * Copyright 2006-2013 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 */
16package org.springframework.batch.core.step.item;
17 
18import java.util.Collection;
19 
20import org.apache.commons.logging.Log;
21import org.apache.commons.logging.LogFactory;
22import org.springframework.batch.repeat.RepeatContext;
23import org.springframework.batch.repeat.exception.ExceptionHandler;
24import org.springframework.batch.repeat.support.RepeatSynchronizationManager;
25import org.springframework.classify.BinaryExceptionClassifier;
26import org.springframework.retry.RetryCallback;
27import org.springframework.retry.RetryContext;
28import org.springframework.retry.RetryPolicy;
29import org.springframework.retry.listener.RetryListenerSupport;
30 
31/**
32 * An {@link ExceptionHandler} that is aware of the retry context so that it can
33 * distinguish between a fatal exception and one that can be retried. Delegates
34 * the actual exception handling to another {@link ExceptionHandler}.
35 *
36 * @author Dave Syer
37 *
38 */
39public class SimpleRetryExceptionHandler extends RetryListenerSupport implements ExceptionHandler {
40 
41        /**
42         * Attribute key, whose existence signals an exhausted retry.
43         */
44        private static final String EXHAUSTED = SimpleRetryExceptionHandler.class.getName() + ".RETRY_EXHAUSTED";
45 
46        private static final Log logger = LogFactory.getLog(SimpleRetryExceptionHandler.class);
47 
48        final private RetryPolicy retryPolicy;
49 
50        final private ExceptionHandler exceptionHandler;
51 
52        final private BinaryExceptionClassifier fatalExceptionClassifier;
53 
54        /**
55         * Create an exception handler from its mandatory properties.
56         *
57         * @param retryPolicy the retry policy that will be under effect when an
58         * exception is encountered
59         * @param exceptionHandler the delegate to use if an exception actually
60         * needs to be handled
61         * @param fatalExceptionClasses
62         */
63        public SimpleRetryExceptionHandler(RetryPolicy retryPolicy, ExceptionHandler exceptionHandler, Collection<Class<? extends Throwable>> fatalExceptionClasses) {
64                this.retryPolicy = retryPolicy;
65                this.exceptionHandler = exceptionHandler;
66                this.fatalExceptionClassifier = new BinaryExceptionClassifier(fatalExceptionClasses);
67        }
68 
69        /**
70         * Check if the exception is going to be retried, and veto the handling if
71         * it is. If retry is exhausted or the exception is on the fatal list, then
72         * handle using the delegate.
73         *
74         * @see ExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext,
75         * java.lang.Throwable)
76         */
77        @Override
78        public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
79                // Only bother to check the delegate exception handler if we know that
80                // retry is exhausted
81                if (fatalExceptionClassifier.classify(throwable) || context.hasAttribute(EXHAUSTED)) {
82                        logger.debug("Handled fatal exception");
83                        exceptionHandler.handleException(context, throwable);
84                }
85                else {
86                        logger.debug("Handled non-fatal exception", throwable);
87                }
88        }
89 
90        /**
91         * If retry is exhausted set up some state in the context that can be used
92         * to signal that the exception should be handled.
93         *
94         * @see org.springframework.retry.RetryListener#close(org.springframework.retry.RetryContext,
95         * org.springframework.retry.RetryCallback, java.lang.Throwable)
96         */
97        @Override
98        public <T> void close(RetryContext context, RetryCallback<T> callback, Throwable throwable) {
99                if (!retryPolicy.canRetry(context)) {
100                        logger.debug("Marking retry as exhausted: "+context);
101                        getRepeatContext().setAttribute(EXHAUSTED, "true");
102                }
103        }
104 
105        /**
106         * Get the parent context (the retry is in an inner "chunk" loop and we want
107         * the exception to be handled at the outer "step" level).
108         * @return the {@link RepeatContext} that should hold the exhausted flag.
109         */
110        private RepeatContext getRepeatContext() {
111                RepeatContext context = RepeatSynchronizationManager.getContext();
112                if (context.getParent() != null) {
113                        return context.getParent();
114                }
115                return context;
116        }
117 
118}

[all classes][org.springframework.batch.core.step.item]
EMMA 2.0.5312 (C) Vladimir Roubtsov