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.repeat.policy; |
18 | |
19 | import java.util.ArrayList; |
20 | import java.util.Arrays; |
21 | import java.util.List; |
22 | |
23 | import org.springframework.batch.repeat.CompletionPolicy; |
24 | import org.springframework.batch.repeat.RepeatContext; |
25 | import org.springframework.batch.repeat.RepeatStatus; |
26 | import org.springframework.batch.repeat.context.RepeatContextSupport; |
27 | |
28 | /** |
29 | * Composite policy that loops through a list of delegate policies and answers |
30 | * calls by a concensus. |
31 | * |
32 | * @author Dave Syer |
33 | * |
34 | */ |
35 | public class CompositeCompletionPolicy implements CompletionPolicy { |
36 | |
37 | CompletionPolicy[] policies = new CompletionPolicy[0]; |
38 | |
39 | /** |
40 | * Setter for the policies. |
41 | * |
42 | * @param policies |
43 | */ |
44 | public void setPolicies(CompletionPolicy[] policies) { |
45 | this.policies = Arrays.asList(policies).toArray(new CompletionPolicy[policies.length]); |
46 | } |
47 | |
48 | /** |
49 | * This policy is complete if any of the composed policies is complete. |
50 | * |
51 | * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext, |
52 | * RepeatStatus) |
53 | */ |
54 | public boolean isComplete(RepeatContext context, RepeatStatus result) { |
55 | RepeatContext[] contexts = ((CompositeBatchContext) context).contexts; |
56 | CompletionPolicy[] policies = ((CompositeBatchContext) context).policies; |
57 | for (int i = 0; i < policies.length; i++) { |
58 | if (policies[i].isComplete(contexts[i], result)) { |
59 | return true; |
60 | } |
61 | } |
62 | return false; |
63 | } |
64 | |
65 | /** |
66 | * This policy is complete if any of the composed policies is complete. |
67 | * |
68 | * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext) |
69 | */ |
70 | public boolean isComplete(RepeatContext context) { |
71 | RepeatContext[] contexts = ((CompositeBatchContext) context).contexts; |
72 | CompletionPolicy[] policies = ((CompositeBatchContext) context).policies; |
73 | for (int i = 0; i < policies.length; i++) { |
74 | if (policies[i].isComplete(contexts[i])) { |
75 | return true; |
76 | } |
77 | } |
78 | return false; |
79 | } |
80 | |
81 | /** |
82 | * Create a new composite context from all the available policies. |
83 | * |
84 | * @see org.springframework.batch.repeat.CompletionPolicy#start(RepeatContext) |
85 | */ |
86 | public RepeatContext start(RepeatContext context) { |
87 | List<RepeatContext> list = new ArrayList<RepeatContext>(); |
88 | for (int i = 0; i < policies.length; i++) { |
89 | list.add(policies[i].start(context)); |
90 | } |
91 | return new CompositeBatchContext(context, list); |
92 | |
93 | } |
94 | |
95 | /** |
96 | * Update all the composed contexts, and also increment the parent context. |
97 | * |
98 | * @see org.springframework.batch.repeat.CompletionPolicy#update(org.springframework.batch.repeat.RepeatContext) |
99 | */ |
100 | public void update(RepeatContext context) { |
101 | RepeatContext[] contexts = ((CompositeBatchContext) context).contexts; |
102 | CompletionPolicy[] policies = ((CompositeBatchContext) context).policies; |
103 | for (int i = 0; i < policies.length; i++) { |
104 | policies[i].update(contexts[i]); |
105 | } |
106 | ((RepeatContextSupport) context).increment(); |
107 | } |
108 | |
109 | /** |
110 | * Composite context that knows about the policies and contexts is was |
111 | * created with. |
112 | * |
113 | * @author Dave Syer |
114 | * |
115 | */ |
116 | protected class CompositeBatchContext extends RepeatContextSupport { |
117 | |
118 | private RepeatContext[] contexts; |
119 | |
120 | // Save a reference to the policies when we were created - gives some |
121 | // protection against reference changes (e.g. if the number of policies |
122 | // change). |
123 | private CompletionPolicy[] policies; |
124 | |
125 | public CompositeBatchContext(RepeatContext context, List<RepeatContext> contexts) { |
126 | super(context); |
127 | this.contexts = contexts.toArray(new RepeatContext[contexts.size()]); |
128 | this.policies = CompositeCompletionPolicy.this.policies; |
129 | } |
130 | |
131 | } |
132 | |
133 | } |