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.classify.Classifier;
22 import org.springframework.classify.ClassifierSupport;
23 import org.springframework.batch.repeat.RepeatContext;
24 import org.springframework.batch.repeat.RepeatException;
25
26 /**
27 * Implementation of {@link ExceptionHandler} based on an {@link Classifier}.
28 * The classifier determines whether to log the exception or rethrow it. The
29 * keys in the classifier must be the same as the static enum in this class.
30 *
31 * @author Dave Syer
32 *
33 */
34 public class LogOrRethrowExceptionHandler implements ExceptionHandler {
35
36 /**
37 * Logging levels for the handler.
38 *
39 * @author Dave Syer
40 *
41 */
42 public static enum Level {
43
44 /**
45 * Key for {@link Classifier} signalling that the throwable should be
46 * rethrown. If the throwable is not a RuntimeException it is wrapped in
47 * a {@link RepeatException}.
48 */
49 RETHROW,
50
51 /**
52 * Key for {@link Classifier} signalling that the throwable should be
53 * logged at debug level.
54 */
55 DEBUG,
56
57 /**
58 * Key for {@link Classifier} signalling that the throwable should be
59 * logged at warn level.
60 */
61 WARN,
62
63 /**
64 * Key for {@link Classifier} signalling that the throwable should be
65 * logged at error level.
66 */
67 ERROR
68
69 }
70
71 protected final Log logger = LogFactory.getLog(LogOrRethrowExceptionHandler.class);
72
73 private Classifier<Throwable, Level> exceptionClassifier = new ClassifierSupport<Throwable, Level>(Level.RETHROW);
74
75 /**
76 * Setter for the {@link Classifier} used by this handler. The default is to
77 * map all throwable instances to {@link Level#RETHROW}.
78 *
79 * @param exceptionClassifier the ExceptionClassifier to use
80 */
81 public void setExceptionClassifier(Classifier<Throwable, Level> exceptionClassifier) {
82 this.exceptionClassifier = exceptionClassifier;
83 }
84
85 /**
86 * Classify the throwables and decide whether to rethrow based on the
87 * result. The context is not used.
88 *
89 * @throws Throwable
90 *
91 * @see ExceptionHandler#handleException(RepeatContext, Throwable)
92 */
93 @Override
94 public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
95
96 Level key = exceptionClassifier.classify(throwable);
97 if (Level.ERROR.equals(key)) {
98 logger.error("Exception encountered in batch repeat.", throwable);
99 }
100 else if (Level.WARN.equals(key)) {
101 logger.warn("Exception encountered in batch repeat.", throwable);
102 }
103 else if (Level.DEBUG.equals(key) && logger.isDebugEnabled()) {
104 logger.debug("Exception encountered in batch repeat.", throwable);
105 }
106 else if (Level.RETHROW.equals(key)) {
107 throw throwable;
108 }
109
110 }
111
112 }