EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.retry.policy]

COVERAGE SUMMARY FOR SOURCE FILE [ExceptionClassifierRetryPolicy.java]

nameclass, %method, %block, %line, %
ExceptionClassifierRetryPolicy.java100% (2/2)100% (13/13)100% (163/163)100% (39/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ExceptionClassifierRetryPolicy100% (1/1)100% (7/7)100% (62/62)100% (18/18)
ExceptionClassifierRetryPolicy (): void 100% (1/1)100% (11/11)100% (3/3)
canRetry (RetryContext): boolean 100% (1/1)100% (7/7)100% (2/2)
close (RetryContext): void 100% (1/1)100% (7/7)100% (3/3)
open (RetryContext): RetryContext 100% (1/1)100% (9/9)100% (1/1)
registerThrowable (RetryContext, Throwable): void 100% (1/1)100% (12/12)100% (4/4)
setExceptionClassifier (Classifier): void 100% (1/1)100% (4/4)100% (2/2)
setPolicyMap (Map): void 100% (1/1)100% (12/12)100% (3/3)
     
class ExceptionClassifierRetryPolicy$ExceptionClassifierRetryContext100% (1/1)100% (6/6)100% (101/101)100% (21/21)
ExceptionClassifierRetryPolicy$ExceptionClassifierRetryContext (RetryContext,... 100% (1/1)100% (12/12)100% (4/4)
canRetry (RetryContext): boolean 100% (1/1)100% (11/11)100% (3/3)
close (RetryContext): void 100% (1/1)100% (21/21)100% (3/3)
getContext (RetryPolicy, RetryContext): RetryContext 100% (1/1)100% (20/20)100% (5/5)
open (RetryContext): RetryContext 100% (1/1)100% (2/2)100% (1/1)
registerThrowable (RetryContext, Throwable): void 100% (1/1)100% (35/35)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 */
16 
17package org.springframework.batch.retry.policy;
18 
19import java.util.HashMap;
20import java.util.Map;
21 
22import org.springframework.batch.classify.Classifier;
23import org.springframework.batch.classify.ClassifierSupport;
24import org.springframework.batch.classify.SubclassClassifier;
25import org.springframework.batch.retry.RetryContext;
26import org.springframework.batch.retry.RetryPolicy;
27import org.springframework.batch.retry.context.RetryContextSupport;
28import org.springframework.util.Assert;
29 
30/**
31 * A {@link RetryPolicy} that dynamically adapts to one of a set of injected
32 * policies according to the value of the latest exception.
33 * 
34 * @author Dave Syer
35 * 
36 */
37public class ExceptionClassifierRetryPolicy implements RetryPolicy {
38 
39        private Classifier<Throwable, RetryPolicy> exceptionClassifier = new ClassifierSupport<Throwable, RetryPolicy>(
40                        new NeverRetryPolicy());
41 
42        /**
43         * Setter for policy map used to create a classifier. Either this property
44         * or the exception classifier directly should be set, but not both.
45         * 
46         * @param policyMap a map of Throwable class to {@link RetryPolicy} that
47         * will be used to create a {@link Classifier} to locate a policy.
48         */
49        public void setPolicyMap(Map<Class<? extends Throwable>, RetryPolicy> policyMap) {
50                SubclassClassifier<Throwable, RetryPolicy> subclassClassifier = new SubclassClassifier<Throwable, RetryPolicy>(
51                                policyMap, (RetryPolicy) new NeverRetryPolicy());
52                this.exceptionClassifier = subclassClassifier;
53        }
54 
55        /**
56         * Setter for an exception classifier. The classifier is responsible for
57         * translating exceptions to concrete retry policies. Either this property
58         * or the policy map should be used, but not both.
59         * 
60         * @param exceptionClassifier ExceptionClassifier to use
61         */
62        public void setExceptionClassifier(Classifier<Throwable, RetryPolicy> exceptionClassifier) {
63                this.exceptionClassifier = exceptionClassifier;
64        }
65 
66        /**
67         * Delegate to the policy currently activated in the context.
68         * 
69         * @see org.springframework.batch.retry.RetryPolicy#canRetry(org.springframework.batch.retry.RetryContext)
70         */
71        public boolean canRetry(RetryContext context) {
72                RetryPolicy policy = (RetryPolicy) context;
73                return policy.canRetry(context);
74        }
75 
76        /**
77         * Delegate to the policy currently activated in the context.
78         * 
79         * @see org.springframework.batch.retry.RetryPolicy#close(org.springframework.batch.retry.RetryContext)
80         */
81        public void close(RetryContext context) {
82                RetryPolicy policy = (RetryPolicy) context;
83                policy.close(context);
84        }
85 
86        /**
87         * Create an active context that proxies a retry policy by chosing a target
88         * from the policy map.
89         * 
90         * @see org.springframework.batch.retry.RetryPolicy#open(RetryContext)
91         */
92        public RetryContext open(RetryContext parent) {
93                return new ExceptionClassifierRetryContext(parent, exceptionClassifier).open(parent);
94        }
95 
96        /**
97         * Delegate to the policy currently activated in the context.
98         * 
99         * @see org.springframework.batch.retry.RetryPolicy#registerThrowable(org.springframework.batch.retry.RetryContext,
100         * Throwable)
101         */
102        public void registerThrowable(RetryContext context, Throwable throwable) {
103                RetryPolicy policy = (RetryPolicy) context;
104                policy.registerThrowable(context, throwable);
105                ((RetryContextSupport) context).registerThrowable(throwable);
106        }
107 
108        private static class ExceptionClassifierRetryContext extends RetryContextSupport implements RetryPolicy {
109 
110                final private Classifier<Throwable, RetryPolicy> exceptionClassifier;
111 
112                // Dynamic: depends on the latest exception:
113                private RetryPolicy policy;
114 
115                // Dynamic: depends on the policy:
116                private RetryContext context;
117 
118                final private Map<RetryPolicy, RetryContext> contexts = new HashMap<RetryPolicy, RetryContext>();
119 
120                public ExceptionClassifierRetryContext(RetryContext parent,
121                                Classifier<Throwable, RetryPolicy> exceptionClassifier) {
122                        super(parent);
123                        this.exceptionClassifier = exceptionClassifier;
124                }
125 
126                public boolean canRetry(RetryContext context) {
127                        if (this.context == null) {
128                                // there was no error yet
129                                return true;
130                        }
131                        return policy.canRetry(this.context);
132                }
133 
134                public void close(RetryContext context) {
135                        // Only close those policies that have been used (opened):
136                        for (RetryPolicy policy : contexts.keySet()) {
137                                policy.close(getContext(policy, context.getParent()));
138                        }
139                }
140 
141                public RetryContext open(RetryContext parent) {
142                        return this;
143                }
144 
145                public void registerThrowable(RetryContext context, Throwable throwable) {
146                        policy = exceptionClassifier.classify(throwable);
147                        Assert.notNull(policy, "Could not locate policy for exception=[" + throwable + "].");
148                        this.context = getContext(policy, context.getParent());
149                        policy.registerThrowable(this.context, throwable);
150                }
151 
152                private RetryContext getContext(RetryPolicy policy, RetryContext parent) {
153                        RetryContext context = contexts.get(policy);
154                        if (context == null) {
155                                context = policy.open(parent);
156                                contexts.put(policy, context);
157                        }
158                        return context;
159                }
160 
161        }
162 
163}

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