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

COVERAGE SUMMARY FOR SOURCE FILE [SubclassExceptionClassifier.java]

nameclass, %method, %block, %line, %
SubclassExceptionClassifier.java67%  (2/3)100% (6/6)100% (151/151)100% (35/35)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SubclassExceptionClassifier100% (1/1)100% (4/4)100% (131/131)100% (30/30)
SubclassExceptionClassifier (): void 100% (1/1)100% (8/8)100% (3/3)
addRetryableExceptionClass (Object, Object, Map): void 100% (1/1)100% (30/30)100% (5/5)
classify (Throwable): Object 100% (1/1)100% (66/66)100% (15/15)
setTypeMap (Map): void 100% (1/1)100% (27/27)100% (7/7)
     
class SubclassExceptionClassifier$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class SubclassExceptionClassifier$ClassComparator100% (1/1)100% (2/2)100% (20/20)100% (6/6)
SubclassExceptionClassifier$ClassComparator (SubclassExceptionClassifier): void 100% (1/1)100% (6/6)100% (1/1)
compare (Object, Object): int 100% (1/1)100% (14/14)100% (5/5)

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 */
16package org.springframework.batch.support;
17 
18import java.util.Comparator;
19import java.util.HashMap;
20import java.util.Iterator;
21import java.util.Map;
22import java.util.Set;
23import java.util.TreeSet;
24 
25import org.springframework.util.Assert;
26 
27/**
28 * 
29 * @author Dave Syer
30 * 
31 */
32public 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}

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