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 | |
17 | package org.springframework.batch.repeat.exception; |
18 | |
19 | import org.apache.commons.logging.Log; |
20 | import org.apache.commons.logging.LogFactory; |
21 | import org.springframework.batch.repeat.RepeatContext; |
22 | import org.springframework.batch.repeat.RepeatException; |
23 | import org.springframework.batch.support.ExceptionClassifier; |
24 | import 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 | */ |
34 | public 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 | } |