1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.batch.repeat.context;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.springframework.batch.repeat.RepeatContext;
27
28 public class RepeatContextSupport extends SynchronizedAttributeAccessor implements RepeatContext {
29
30 private RepeatContext parent;
31
32 private int count;
33
34 private volatile boolean completeOnly;
35
36 private volatile boolean terminateOnly;
37
38 private Map<String, Set<Runnable>> callbacks = new HashMap<String, Set<Runnable>>();
39
40
41
42
43
44
45
46 public RepeatContextSupport(RepeatContext parent) {
47 super();
48 this.parent = parent;
49 }
50
51
52
53
54
55
56 @Override
57 public boolean isCompleteOnly() {
58 return completeOnly;
59 }
60
61
62
63
64
65
66 @Override
67 public void setCompleteOnly() {
68 completeOnly = true;
69 }
70
71
72
73
74
75
76 @Override
77 public boolean isTerminateOnly() {
78 return terminateOnly;
79 }
80
81
82
83
84
85
86 @Override
87 public void setTerminateOnly() {
88 terminateOnly = true;
89 setCompleteOnly();
90 }
91
92
93
94
95
96
97 @Override
98 public RepeatContext getParent() {
99 return parent;
100 }
101
102
103
104
105 public synchronized void increment() {
106 count++;
107 }
108
109
110
111
112
113
114 @Override
115 public synchronized int getStartedCount() {
116 return count;
117 }
118
119
120
121
122
123
124
125
126 @Override
127 public void registerDestructionCallback(String name, Runnable callback) {
128 synchronized (callbacks) {
129 Set<Runnable> set = callbacks.get(name);
130 if (set == null) {
131 set = new HashSet<Runnable>();
132 callbacks.put(name, set);
133 }
134 set.add(callback);
135 }
136 }
137
138
139
140
141
142
143 @Override
144 public void close() {
145
146 List<RuntimeException> errors = new ArrayList<RuntimeException>();
147
148 Set<Map.Entry<String, Set<Runnable>>> copy;
149
150 synchronized (callbacks) {
151 copy = new HashSet<Map.Entry<String, Set<Runnable>>>(callbacks.entrySet());
152 }
153
154 for (Map.Entry<String, Set<Runnable>> entry : copy) {
155
156 for (Runnable callback : entry.getValue()) {
157
158
159
160
161
162
163 if (callback != null) {
164
165
166
167
168
169 try {
170 callback.run();
171 }
172 catch (RuntimeException t) {
173 errors.add(t);
174 }
175 }
176 }
177 }
178
179 if (errors.isEmpty()) {
180 return;
181 }
182
183 throw (RuntimeException) errors.get(0);
184 }
185
186 }