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

COVERAGE SUMMARY FOR SOURCE FILE [CompositeRetryPolicy.java]

nameclass, %method, %block, %line, %
CompositeRetryPolicy.java100% (2/2)100% (7/7)100% (157/157)100% (38/38)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CompositeRetryPolicy100% (1/1)100% (6/6)100% (139/139)100% (33/33)
CompositeRetryPolicy (): void 100% (1/1)100% (7/7)100% (3/3)
canRetry (RetryContext): boolean 100% (1/1)100% (28/28)100% (6/6)
close (RetryContext): void 100% (1/1)100% (36/36)100% (12/12)
open (RetryContext): RetryContext 100% (1/1)100% (29/29)100% (4/4)
registerThrowable (RetryContext, Throwable): void 100% (1/1)100% (29/29)100% (6/6)
setPolicies (RetryPolicy []): void 100% (1/1)100% (10/10)100% (2/2)
     
class CompositeRetryPolicy$CompositeRetryContext100% (1/1)100% (1/1)100% (18/18)100% (5/5)
CompositeRetryPolicy$CompositeRetryContext (CompositeRetryPolicy, RetryContex... 100% (1/1)100% (18/18)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.ArrayList;
20import java.util.Arrays;
21import java.util.List;
22 
23import org.springframework.batch.retry.RetryContext;
24import org.springframework.batch.retry.RetryPolicy;
25import org.springframework.batch.retry.context.RetryContextSupport;
26 
27/**
28 * A {@link RetryPolicy} that composes a list of other policies and delegates
29 * calls to them in order.
30 * 
31 * @author Dave Syer
32 * 
33 */
34public class CompositeRetryPolicy implements RetryPolicy {
35 
36        RetryPolicy[] policies = new RetryPolicy[0];
37 
38        /**
39         * Setter for policies.
40         * 
41         * @param policies
42         */
43        public void setPolicies(RetryPolicy[] policies) {
44                this.policies = Arrays.asList(policies).toArray(new RetryPolicy[policies.length]);
45        }
46 
47        /**
48         * Delegate to the policies that were in operation when the context was
49         * created. If any of them cannot retry then return false, oetherwise return
50         * true.
51         * 
52         * @see org.springframework.batch.retry.RetryPolicy#canRetry(org.springframework.batch.retry.RetryContext)
53         */
54        public boolean canRetry(RetryContext context) {
55                RetryContext[] contexts = ((CompositeRetryContext) context).contexts;
56                RetryPolicy[] policies = ((CompositeRetryContext) context).policies;
57                for (int i = 0; i < contexts.length; i++) {
58                        if (!policies[i].canRetry(contexts[i])) {
59                                return false;
60                        }
61                }
62                return true;
63        }
64 
65        /**
66         * Delegate to the policies that were in operation when the context was
67         * created. If any of them fails to close the exception is propagated (and
68         * those later in the chain are closed before re-throwing).
69         * 
70         * @see org.springframework.batch.retry.RetryPolicy#close(org.springframework.batch.retry.RetryContext)
71         */
72        public void close(RetryContext context) {
73                RetryContext[] contexts = ((CompositeRetryContext) context).contexts;
74                RetryPolicy[] policies = ((CompositeRetryContext) context).policies;
75                RuntimeException exception = null;
76                for (int i = 0; i < contexts.length; i++) {
77                        try {
78                                policies[i].close(contexts[i]);
79                        }
80                        catch (RuntimeException e) {
81                                if (exception == null) {
82                                        exception = e;
83                                }
84                        }
85                }
86                if (exception != null) {
87                        throw exception;
88                }
89        }
90 
91        /**
92         * Creates a new context that copies the existing policies and keeps a list
93         * of the contexts from each one.
94         * 
95         * @see org.springframework.batch.retry.RetryPolicy#open(RetryContext)
96         */
97        public RetryContext open(RetryContext parent) {
98                List<RetryContext> list = new ArrayList<RetryContext>();
99                for (int i = 0; i < policies.length; i++) {
100                        list.add(policies[i].open(parent));
101                }
102                return new CompositeRetryContext(parent, list);
103        }
104 
105        /**
106         * Delegate to the policies that were in operation when the context was
107         * created.
108         * 
109         * @see org.springframework.batch.retry.RetryPolicy#close(org.springframework.batch.retry.RetryContext)
110         */
111        public void registerThrowable(RetryContext context, Throwable throwable) {
112                RetryContext[] contexts = ((CompositeRetryContext) context).contexts;
113                RetryPolicy[] policies = ((CompositeRetryContext) context).policies;
114                for (int i = 0; i < contexts.length; i++) {
115                        policies[i].registerThrowable(contexts[i], throwable);
116                }
117                ((RetryContextSupport) context).registerThrowable(throwable);
118        }
119 
120        private class CompositeRetryContext extends RetryContextSupport {
121                RetryContext[] contexts;
122 
123                RetryPolicy[] policies;
124 
125                public CompositeRetryContext(RetryContext parent, List<RetryContext> contexts) {
126                        super(parent);
127                        this.contexts = contexts.toArray(new RetryContext[0]);
128                        this.policies = CompositeRetryPolicy.this.policies;
129                }
130 
131        }
132 
133}

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