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 | package org.springframework.batch.support; |
17 | |
18 | import java.util.Comparator; |
19 | import java.util.HashMap; |
20 | import java.util.Iterator; |
21 | import java.util.Map; |
22 | import java.util.Set; |
23 | import java.util.TreeSet; |
24 | |
25 | import org.springframework.util.Assert; |
26 | |
27 | /** |
28 | * |
29 | * @author Dave Syer |
30 | * |
31 | */ |
32 | public class SubclassExceptionClassifier extends ExceptionClassifierSupport { |
33 | |
34 | private Map classified = new HashMap(); |
35 | |
36 | /** |
37 | * Map of Throwable class types to keys for the classifier. Any subclass of |
38 | * the type provided will be classified as of the type given by the |
39 | * corresponding map entry value. |
40 | * |
41 | * @param typeMap the typeMap to set |
42 | */ |
43 | public final void setTypeMap(Map typeMap) { |
44 | Map map = new HashMap(); |
45 | for (Iterator iter = typeMap.entrySet().iterator(); iter.hasNext();) { |
46 | Map.Entry entry = (Map.Entry) iter.next(); |
47 | addRetryableExceptionClass(entry.getKey(), entry.getValue(), map); |
48 | } |
49 | this.classified = map; |
50 | } |
51 | |
52 | /** |
53 | * Return the value from the type map whose key is the class of the given |
54 | * Throwable, or its nearest ancestor if a subclass. |
55 | * |
56 | * @see org.springframework.batch.support.ExceptionClassifierSupport#classify(java.lang.Throwable) |
57 | */ |
58 | public Object classify(Throwable throwable) { |
59 | |
60 | if (throwable == null) { |
61 | return super.classify(throwable); |
62 | } |
63 | |
64 | Class exceptionClass = throwable.getClass(); |
65 | if (classified.containsKey(exceptionClass)) { |
66 | return classified.get(exceptionClass); |
67 | } |
68 | |
69 | // check for subclasses |
70 | Set classes = new TreeSet(new ClassComparator()); |
71 | classes.addAll(classified.keySet()); |
72 | for (Iterator iterator = classes.iterator(); iterator.hasNext();) { |
73 | Class cls = (Class) iterator.next(); |
74 | if (cls.isAssignableFrom(exceptionClass)) { |
75 | Object value = classified.get(cls); |
76 | addRetryableExceptionClass(exceptionClass, value, this.classified); |
77 | return value; |
78 | } |
79 | } |
80 | |
81 | return super.classify(throwable); |
82 | } |
83 | |
84 | private void addRetryableExceptionClass(Object candidateClass, Object classifiedAs, Map map) { |
85 | Assert.isAssignable(Class.class, candidateClass.getClass()); |
86 | Class exceptionClass = (Class) candidateClass; |
87 | Assert.isAssignable(Throwable.class, exceptionClass); |
88 | map.put(exceptionClass, classifiedAs); |
89 | } |
90 | |
91 | /** |
92 | * Comparator for classes to order by inheritance. |
93 | * |
94 | * @author Dave Syer |
95 | * |
96 | */ |
97 | private class ClassComparator implements Comparator { |
98 | /** |
99 | * @return 1 if arg0 is assignable from arg1, -1 otherwise |
100 | * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) |
101 | */ |
102 | public int compare(Object arg0, Object arg1) { |
103 | Class cls0 = (Class) arg0; |
104 | Class cls1 = (Class) arg1; |
105 | if (cls0.isAssignableFrom(cls1)) { |
106 | return 1; |
107 | } |
108 | return -1; |
109 | } |
110 | } |
111 | |
112 | } |