forked from spring-projects/spring-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExitStatus.java
299 lines (273 loc) · 8.57 KB
/
ExitStatus.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core;
import org.springframework.util.StringUtils;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
/**
* Value object used to carry information about the status of a
* job or step execution.
*
* ExitStatus is immutable and therefore thread-safe.
*
* @author Dave Syer
*
*/
@SuppressWarnings("serial")
public class ExitStatus implements Serializable, Comparable<ExitStatus> {
/**
* Convenient constant value representing unknown state - assumed not
* continuable.
*/
public static final ExitStatus UNKNOWN = new ExitStatus("UNKNOWN");
/**
* Convenient constant value representing continuable state where processing
* is still taking place, so no further action is required. Used for
* asynchronous execution scenarios where the processing is happening in
* another thread or process and the caller is not required to wait for the
* result.
*/
public static final ExitStatus EXECUTING = new ExitStatus("EXECUTING");
/**
* Convenient constant value representing finished processing.
*/
public static final ExitStatus COMPLETED = new ExitStatus("COMPLETED");
/**
* Convenient constant value representing job that did no processing
* (e.g. because it was already complete).
*/
public static final ExitStatus NOOP = new ExitStatus("NOOP");
/**
* Convenient constant value representing finished processing with an error.
*/
public static final ExitStatus FAILED = new ExitStatus("FAILED");
/**
* Convenient constant value representing finished processing with
* interrupted status.
*/
public static final ExitStatus STOPPED = new ExitStatus("STOPPED");
private final String exitCode;
private final String exitDescription;
/**
* Constructor that accepts the exit code and sets the exit description to an empty {@link String}.
*
* @param exitCode The exit code to be used for the {@link ExitStatus}.
*/
public ExitStatus(String exitCode) {
this(exitCode, "");
}
/**
* Constructor that establishes the exit code and the exit description for the {@link ExitStatus}.
*
* @param exitCode The exit code to be used for the {@link ExitStatus}.
* @param exitDescription The exit description to be used for the {@link ExitStatus}.
*/
public ExitStatus(String exitCode, String exitDescription) {
super();
this.exitCode = exitCode;
this.exitDescription = exitDescription == null ? "" : exitDescription;
}
/**
* Getter for the exit code (defaults to blank).
*
* @return the exit code.
*/
public String getExitCode() {
return exitCode;
}
/**
* Getter for the exit description (defaults to blank)
*
* @return {@link String} containing the exit description.
*/
public String getExitDescription() {
return exitDescription;
}
/**
* Create a new {@link ExitStatus} with a logical combination of the exit
* code, and a concatenation of the descriptions. If either value has a
* higher severity then its exit code will be used in the result. In the
* case of equal severity, the exit code is replaced if the new value is
* alphabetically greater.<br>
* <br>
*
* Severity is defined by the exit code:
* <ul>
* <li>Codes beginning with EXECUTING have severity 1</li>
* <li>Codes beginning with COMPLETED have severity 2</li>
* <li>Codes beginning with NOOP have severity 3</li>
* <li>Codes beginning with STOPPED have severity 4</li>
* <li>Codes beginning with FAILED have severity 5</li>
* <li>Codes beginning with UNKNOWN have severity 6</li>
* </ul>
* Others have severity 7, so custom exit codes always win.<br>
*
* If the input is null just return this.
*
* @param status an {@link ExitStatus} to combine with this one.
* @return a new {@link ExitStatus} combining the current value and the
* argument provided.
*/
public ExitStatus and(ExitStatus status) {
if (status == null) {
return this;
}
ExitStatus result = addExitDescription(status.exitDescription);
if (compareTo(status) < 0) {
result = result.replaceExitCode(status.exitCode);
}
return result;
}
/**
* @param status an {@link ExitStatus} to compare
* @return greater than zero, 0, less than zero according to the severity and exit code
* @see java.lang.Comparable
*/
@Override
public int compareTo(ExitStatus status) {
if (severity(status) > severity(this)) {
return -1;
}
if (severity(status) < severity(this)) {
return 1;
}
return this.getExitCode().compareTo(status.getExitCode());
}
/**
* @param status
* @return
*/
private int severity(ExitStatus status) {
if (status.exitCode.startsWith(EXECUTING.exitCode)) {
return 1;
}
if (status.exitCode.startsWith(COMPLETED.exitCode)) {
return 2;
}
if (status.exitCode.startsWith(NOOP.exitCode)) {
return 3;
}
if (status.exitCode.startsWith(STOPPED.exitCode)) {
return 4;
}
if (status.exitCode.startsWith(FAILED.exitCode)) {
return 5;
}
if (status.exitCode.startsWith(UNKNOWN.exitCode)) {
return 6;
}
return 7;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("exitCode=%s;exitDescription=%s", exitCode, exitDescription);
}
/**
* Compare the fields one by one.
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
return toString().equals(obj.toString());
}
/**
* Compatible with the equals implementation.
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return toString().hashCode();
}
/**
* Add an exit code to an existing {@link ExitStatus}. If there is already a
* code present tit will be replaced.
*
* @param code the code to add
* @return a new {@link ExitStatus} with the same properties but a new exit
* code.
*/
public ExitStatus replaceExitCode(String code) {
return new ExitStatus(code, exitDescription);
}
/**
* Check if this status represents a running process.
*
* @return true if the exit code is "EXECUTING" or "UNKNOWN"
*/
public boolean isRunning() {
return "EXECUTING".equals(this.exitCode) || "UNKNOWN".equals(this.exitCode);
}
/**
* Add an exit description to an existing {@link ExitStatus}. If there is
* already a description present the two will be concatenated with a
* semicolon.
*
* @param description the description to add
* @return a new {@link ExitStatus} with the same properties but a new exit
* description
*/
public ExitStatus addExitDescription(String description) {
StringBuilder buffer = new StringBuilder();
boolean changed = StringUtils.hasText(description) && !exitDescription.equals(description);
if (StringUtils.hasText(exitDescription)) {
buffer.append(exitDescription);
if (changed) {
buffer.append("; ");
}
}
if (changed) {
buffer.append(description);
}
return new ExitStatus(exitCode, buffer.toString());
}
/**
* Extract the stack trace from the throwable provided and append it to
* the exist description.
*
* @param throwable {@link Throwable} instance containing the stack trace.
* @return a new ExitStatus with the stack trace appended
*/
public ExitStatus addExitDescription(Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
String message = writer.toString();
return addExitDescription(message);
}
/**
* @param status the exit code to be evaluated
* @return true if the value matches a known exit code
*/
public static boolean isNonDefaultExitStatus(ExitStatus status) {
return status == null || status.getExitCode() == null ||
status.getExitCode().equals(ExitStatus.COMPLETED.getExitCode()) ||
status.getExitCode().equals(ExitStatus.EXECUTING.getExitCode()) ||
status.getExitCode().equals(ExitStatus.FAILED.getExitCode()) ||
status.getExitCode().equals(ExitStatus.NOOP.getExitCode()) ||
status.getExitCode().equals(ExitStatus.STOPPED.getExitCode()) ||
status.getExitCode().equals(ExitStatus.UNKNOWN.getExitCode());
}
}