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. 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 | } |