EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[all classes][org.springframework.batch.repeat.exception]

COVERAGE SUMMARY FOR SOURCE FILE [LogOrRethrowExceptionHandler.java]

nameclass, %method, %block, %line, %
LogOrRethrowExceptionHandler.java100% (2/2)100% (5/5)100% (83/83)100% (18/18)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class LogOrRethrowExceptionHandler100% (1/1)100% (3/3)100% (75/75)100% (16/16)
LogOrRethrowExceptionHandler (): void 100% (1/1)100% (20/20)100% (3/3)
handleException (RepeatContext, Throwable): void 100% (1/1)100% (51/51)100% (11/11)
setExceptionClassifier (ExceptionClassifier): void 100% (1/1)100% (4/4)100% (2/2)
     
class LogOrRethrowExceptionHandler$1100% (1/1)100% (2/2)100% (8/8)100% (2/2)
LogOrRethrowExceptionHandler$1 (LogOrRethrowExceptionHandler): void 100% (1/1)100% (6/6)100% (1/1)
classify (Throwable): Object 100% (1/1)100% (2/2)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 org.apache.commons.logging.Log;
20import org.apache.commons.logging.LogFactory;
21import org.springframework.batch.repeat.RepeatContext;
22import org.springframework.batch.repeat.RepeatException;
23import org.springframework.batch.support.ExceptionClassifier;
24import org.springframework.batch.support.ExceptionClassifierSupport;
25 
26/**
27 * Implementation of {@link ExceptionHandler} based on an {@link ExceptionClassifier}. The classifier determines
28 * whether to log the exception or rethrow it. The keys in the classifier must be the same as the static contants in
29 * this class.
30 * 
31 * @author Dave Syer
32 * 
33 */
34public class LogOrRethrowExceptionHandler implements ExceptionHandler {
35 
36        /**
37         * Key for {@link ExceptionClassifier} signalling that the throwable should be rethrown. If the throwable is not a
38         * RuntimeException it is wrapped in a {@link RepeatException}.
39         */
40        public static final String RETHROW = "rethrow";
41 
42        /**
43         * Key for {@link ExceptionClassifier} signalling that the throwable should be logged at debug level.
44         */
45        public static final String DEBUG = "debug";
46 
47        /**
48         * Key for {@link ExceptionClassifier} signalling that the throwable should be logged at warn level.
49         */
50        public static final String WARN = "warn";
51 
52        /**
53         * Key for {@link ExceptionClassifier} signalling that the throwable should be logged at error level.
54         */
55        public static final String ERROR = "error";
56 
57        protected final Log logger = LogFactory.getLog(LogOrRethrowExceptionHandler.class);
58 
59        private ExceptionClassifier exceptionClassifier = new ExceptionClassifierSupport() {
60                public Object classify(Throwable throwable) {
61                        return RETHROW;
62                }
63        };
64 
65        /**
66         * Setter for the {@link ExceptionClassifier} used by this handler. The default is to map all throwable instances to
67         * {@link #RETHROW}.
68         * 
69         * @param exceptionClassifier
70         */
71        public void setExceptionClassifier(ExceptionClassifier exceptionClassifier) {
72                this.exceptionClassifier = exceptionClassifier;
73        }
74 
75        /**
76         * Classify the throwables and decide whether to rethrow based on the result. The context is not used.
77         * 
78         * @throws Throwable
79         * 
80         * @see ExceptionHandler#handleException(RepeatContext, Throwable)
81         */
82        public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
83 
84                Object key = exceptionClassifier.classify(throwable);
85                if (ERROR.equals(key)) {
86                        logger.error("Exception encountered in batch repeat.", throwable);
87                } else if (WARN.equals(key)) {
88                        logger.warn("Exception encountered in batch repeat.", throwable);
89                } else if (DEBUG.equals(key) && logger.isDebugEnabled()) {
90                        logger.debug("Exception encountered in batch repeat.", throwable);
91                } else if (RETHROW.equals(key)) {
92                        throw throwable;
93                } else {
94                        throw new IllegalStateException(
95                                "Unclassified exception encountered.  Did you mean to classifiy this as 'rethrow'?");
96                }
97 
98        }
99 
100}

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