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 public boolean isCompleteOnly() {
57 return completeOnly;
58 }
59
60
61
62
63
64
65 public void setCompleteOnly() {
66 completeOnly = true;
67 }
68
69
70
71
72
73
74 public boolean isTerminateOnly() {
75 return terminateOnly;
76 }
77
78
79
80
81
82
83 public void setTerminateOnly() {
84 terminateOnly = true;
85 setCompleteOnly();
86 }
87
88
89
90
91
92
93 public RepeatContext getParent() {
94 return parent;
95 }
96
97
98
99
100 public synchronized void increment() {
101 count++;
102 }
103
104
105
106
107
108
109 public synchronized int getStartedCount() {
110 return count;
111 }
112
113
114
115
116
117
118
119
120 public void registerDestructionCallback(String name, Runnable callback) {
121 synchronized (callbacks) {
122 Set<Runnable> set = callbacks.get(name);
123 if (set == null) {
124 set = new HashSet<Runnable>();
125 callbacks.put(name, set);
126 }
127 set.add(callback);
128 }
129 }
130
131
132
133
134
135
136 public void close() {
137
138 List<RuntimeException> errors = new ArrayList<RuntimeException>();
139
140 Set<Map.Entry<String, Set<Runnable>>> copy;
141
142 synchronized (callbacks) {
143 copy = new HashSet<Map.Entry<String, Set<Runnable>>>(callbacks.entrySet());
144 }
145
146 for (Map.Entry<String, Set<Runnable>> entry : copy) {
147
148 for (Runnable callback : entry.getValue()) {
149
150
151
152
153
154
155 if (callback != null) {
156
157
158
159
160
161 try {
162 callback.run();
163 }
164 catch (RuntimeException t) {
165 errors.add(t);
166 }
167 }
168 }
169 }
170
171 if (errors.isEmpty()) {
172 return;
173 }
174
175 throw (RuntimeException) errors.get(0);
176 }
177
178 }