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.Arrays; |
21 | import java.util.List; |
22 | |
23 | import org.springframework.batch.retry.RetryContext; |
24 | import org.springframework.batch.retry.RetryPolicy; |
25 | import 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 | */ |
34 | public 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 | } |