EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[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% (153/153)100% (38/38)

COVERAGE BREAKDOWN BY CLASS AND METHOD

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

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