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