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