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 | package org.springframework.batch.repeat; |
17 | |
18 | import java.io.Serializable; |
19 | |
20 | import org.springframework.util.StringUtils; |
21 | |
22 | /** |
23 | * Value object used to carry information about the status of a |
24 | * {@link RepeatOperations}. |
25 | * |
26 | * ExitStatus is immutable and therefore thread-safe. |
27 | * |
28 | * @author Dave Syer |
29 | * |
30 | */ |
31 | public class ExitStatus implements Serializable { |
32 | |
33 | /** |
34 | * Convenient constant value representing unknown state - assumed not |
35 | * continuable. |
36 | */ |
37 | public static final ExitStatus UNKNOWN = new ExitStatus(false, "UNKNOWN"); |
38 | |
39 | /** |
40 | * Convenient constant value representing unfinished processing. |
41 | */ |
42 | public static final ExitStatus CONTINUABLE = new ExitStatus(true, |
43 | "CONTINUABLE"); |
44 | |
45 | /** |
46 | * Convenient constant value representing finished processing. |
47 | */ |
48 | public static final ExitStatus FINISHED = new ExitStatus(false, "COMPLETED"); |
49 | |
50 | /** |
51 | * Convenient constant value representing job that did no processing (e.g. |
52 | * because it was already complete). |
53 | */ |
54 | public static final ExitStatus NOOP = new ExitStatus(false, "NOOP"); |
55 | |
56 | /** |
57 | * Convenient constant value representing finished processing with an error. |
58 | */ |
59 | public static final ExitStatus FAILED = new ExitStatus(false, "FAILED"); |
60 | |
61 | private final boolean continuable; |
62 | |
63 | private final String exitCode; |
64 | |
65 | private final String exitDescription; |
66 | |
67 | public ExitStatus(boolean continuable) { |
68 | this(continuable, "", ""); |
69 | } |
70 | |
71 | public ExitStatus(boolean continuable, String exitCode) { |
72 | this(continuable, exitCode, ""); |
73 | } |
74 | |
75 | public ExitStatus(boolean continuable, String exitCode, |
76 | String exitDescription) { |
77 | super(); |
78 | this.continuable = continuable; |
79 | this.exitCode = exitCode; |
80 | this.exitDescription = exitDescription; |
81 | } |
82 | |
83 | /** |
84 | * Flag to signal that processing can continue. This is distinct from any |
85 | * flag that might indicate that a batch is complete, or terminated, since a |
86 | * batch might be only a small part of a larger whole, which is still not |
87 | * finished. |
88 | * |
89 | * @return true if processing can continue. |
90 | */ |
91 | public boolean isContinuable() { |
92 | return continuable; |
93 | } |
94 | |
95 | /** |
96 | * Getter for the exit code (defaults to blank). |
97 | * |
98 | * @return the exit code. |
99 | */ |
100 | public String getExitCode() { |
101 | return exitCode; |
102 | } |
103 | |
104 | /** |
105 | * Getter for the exit description (defaults to blank) |
106 | */ |
107 | public String getExitDescription() { |
108 | return exitDescription; |
109 | } |
110 | |
111 | /** |
112 | * Create a new {@link ExitStatus} with a logical combination of the |
113 | * continuable flag. |
114 | * |
115 | * @param continuable |
116 | * true if the caller thinks it is safe to continue. |
117 | * @return a new {@link ExitStatus} with {@link #isContinuable()} the |
118 | * logical and of the current value and the argument provided. |
119 | */ |
120 | public ExitStatus and(boolean continuable) { |
121 | return new ExitStatus(this.continuable && continuable, this.exitCode, |
122 | this.exitDescription); |
123 | } |
124 | |
125 | /** |
126 | * Create a new {@link ExitStatus} with a logical combination of the |
127 | * continuable flag, and a concatenation of the descriptions. The exit code |
128 | * is only replaced if the result is continuable or the input is not |
129 | * continuable.<br/> |
130 | * |
131 | * If the input is null just return this. |
132 | * |
133 | * @param status |
134 | * an {@link ExitStatus} to combine with this one. |
135 | * @return a new {@link ExitStatus} with {@link #isContinuable()} the |
136 | * logical and of the current value and the argument provided. |
137 | */ |
138 | public ExitStatus and(ExitStatus status) { |
139 | if (status == null) { |
140 | return this; |
141 | } |
142 | ExitStatus result = and(status.continuable).addExitDescription( |
143 | status.exitDescription); |
144 | if (result.continuable || !status.continuable) { |
145 | result = result.replaceExitCode(status.exitCode); |
146 | } |
147 | return result; |
148 | } |
149 | |
150 | /* |
151 | * (non-Javadoc) |
152 | * |
153 | * @see java.lang.Object#toString() |
154 | */ |
155 | public String toString() { |
156 | return "continuable=" + continuable + ";exitCode=" + exitCode |
157 | + ";exitDescription=" + exitDescription; |
158 | } |
159 | |
160 | /** |
161 | * Compare the fields one by one. |
162 | * |
163 | * @see java.lang.Object#equals(java.lang.Object) |
164 | */ |
165 | public boolean equals(Object obj) { |
166 | if (obj == null) { |
167 | return false; |
168 | } |
169 | return toString().equals(obj.toString()); |
170 | } |
171 | |
172 | /** |
173 | * Compatible with the equals implementation. |
174 | * |
175 | * @see java.lang.Object#hashCode() |
176 | */ |
177 | public int hashCode() { |
178 | return toString().hashCode(); |
179 | } |
180 | |
181 | /** |
182 | * Add an exit code to an existing {@link ExitStatus}. If there is already a |
183 | * code present tit will be replaced. |
184 | * |
185 | * @param code |
186 | * the code to add |
187 | * @return a new {@link ExitStatus} with the same properties but a new exit |
188 | * code. |
189 | */ |
190 | public ExitStatus replaceExitCode(String code) { |
191 | return new ExitStatus(continuable, code, exitDescription); |
192 | } |
193 | |
194 | /** |
195 | * Check if this status represents a running process. |
196 | * |
197 | * @return true if the exit code is "RUNNING" or "UNKNOWN" |
198 | */ |
199 | public boolean isRunning() { |
200 | return "RUNNING".equals(this.exitCode) |
201 | || "UNKNOWN".equals(this.exitCode); |
202 | } |
203 | |
204 | /** |
205 | * Add an exit description to an existing {@link ExitStatus}. If there is |
206 | * already a description present the two will be concatenated with a |
207 | * semicolon. |
208 | * |
209 | * @param description |
210 | * the description to add |
211 | * @return a new {@link ExitStatus} with the same properties but a new exit |
212 | * description |
213 | */ |
214 | public ExitStatus addExitDescription(String description) { |
215 | StringBuffer buffer = new StringBuffer(); |
216 | boolean changed = StringUtils.hasText(description) |
217 | && !exitDescription.equals(description); |
218 | if (StringUtils.hasText(exitDescription)) { |
219 | buffer.append(exitDescription); |
220 | if (changed) { |
221 | buffer.append("; "); |
222 | } |
223 | } |
224 | if (changed) { |
225 | buffer.append(description); |
226 | } |
227 | return new ExitStatus(continuable, exitCode, buffer.toString()); |
228 | } |
229 | |
230 | } |