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 | |
17 | package org.springframework.batch.retry.policy; |
18 | |
19 | import java.util.ArrayList; |
20 | import java.util.List; |
21 | |
22 | import org.springframework.batch.retry.RetryCallback; |
23 | import org.springframework.batch.retry.RetryContext; |
24 | import org.springframework.batch.retry.RetryPolicy; |
25 | import org.springframework.batch.retry.TerminatedRetryException; |
26 | import 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 | */ |
35 | public 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. |
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 | // TODO: throw some sort of composite exception if any of the close |
76 | // methods fail? |
77 | for (int i = 0; i < contexts.length; i++) { |
78 | policies[i].close(contexts[i]); |
79 | } |
80 | } |
81 | |
82 | /** |
83 | * Creates a new context that copies the existing policies and keeps a list |
84 | * of the contexts from each one. |
85 | * |
86 | * @see org.springframework.batch.retry.RetryPolicy#open(org.springframework.batch.retry.RetryCallback, RetryContext) |
87 | */ |
88 | public RetryContext open(RetryCallback callback, RetryContext parent) { |
89 | List list = new ArrayList(); |
90 | for (int i = 0; i < policies.length; i++) { |
91 | list.add(policies[i].open(callback, parent)); |
92 | } |
93 | return new CompositeRetryContext(parent, list); |
94 | } |
95 | |
96 | /** |
97 | * Delegate to the policies that were in operation when the context was |
98 | * created. |
99 | * |
100 | * @see org.springframework.batch.retry.RetryPolicy#close(org.springframework.batch.retry.RetryContext) |
101 | */ |
102 | public void registerThrowable(RetryContext context, Throwable throwable) throws TerminatedRetryException { |
103 | RetryContext[] contexts = ((CompositeRetryContext) context).contexts; |
104 | RetryPolicy[] policies = ((CompositeRetryContext) context).policies; |
105 | for (int i = 0; i < contexts.length; i++) { |
106 | policies[i].registerThrowable(contexts[i], throwable); |
107 | } |
108 | ((RetryContextSupport) context).registerThrowable(throwable); |
109 | } |
110 | |
111 | private class CompositeRetryContext extends RetryContextSupport { |
112 | RetryContext[] contexts; |
113 | |
114 | RetryPolicy[] policies; |
115 | |
116 | public CompositeRetryContext(RetryContext parent, List contexts) { |
117 | super(parent); |
118 | this.contexts = (RetryContext[]) contexts.toArray(new RetryContext[0]); |
119 | this.policies = CompositeRetryPolicy.this.policies; |
120 | } |
121 | |
122 | } |
123 | |
124 | } |