| 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.core; |
| 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 | * job or step execution. |
| 25 | * |
| 26 | * ExitStatus is immutable and therefore thread-safe. |
| 27 | * |
| 28 | * @author Dave Syer |
| 29 | * |
| 30 | */ |
| 31 | public class ExitStatus implements Serializable, Comparable<ExitStatus> { |
| 32 | |
| 33 | /** |
| 34 | * Convenient constant value representing unknown state - assumed not |
| 35 | * continuable. |
| 36 | */ |
| 37 | public static final ExitStatus UNKNOWN = new ExitStatus("UNKNOWN"); |
| 38 | |
| 39 | /** |
| 40 | * Convenient constant value representing continuable state where processing |
| 41 | * is still taking place, so no further action is required. Used for |
| 42 | * asynchronous execution scenarios where the processing is happening in |
| 43 | * another thread or process and the caller is not required to wait for the |
| 44 | * result. |
| 45 | */ |
| 46 | public static final ExitStatus EXECUTING = new ExitStatus("EXECUTING"); |
| 47 | |
| 48 | /** |
| 49 | * Convenient constant value representing finished processing. |
| 50 | */ |
| 51 | public static final ExitStatus COMPLETED = new ExitStatus("COMPLETED"); |
| 52 | |
| 53 | /** |
| 54 | * Convenient constant value representing job that did no processing (e.g. |
| 55 | * because it was already complete). |
| 56 | */ |
| 57 | public static final ExitStatus NOOP = new ExitStatus("NOOP"); |
| 58 | |
| 59 | /** |
| 60 | * Convenient constant value representing finished processing with an error. |
| 61 | */ |
| 62 | public static final ExitStatus FAILED = new ExitStatus("FAILED"); |
| 63 | |
| 64 | /** |
| 65 | * Convenient constant value representing finished processing with |
| 66 | * interrupted status. |
| 67 | */ |
| 68 | public static final ExitStatus STOPPED = new ExitStatus("STOPPED"); |
| 69 | |
| 70 | private final String exitCode; |
| 71 | |
| 72 | private final String exitDescription; |
| 73 | |
| 74 | public ExitStatus(String exitCode) { |
| 75 | this(exitCode, ""); |
| 76 | } |
| 77 | |
| 78 | public ExitStatus(String exitCode, String exitDescription) { |
| 79 | super(); |
| 80 | this.exitCode = exitCode; |
| 81 | this.exitDescription = exitDescription; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Getter for the exit code (defaults to blank). |
| 86 | * |
| 87 | * @return the exit code. |
| 88 | */ |
| 89 | public String getExitCode() { |
| 90 | return exitCode; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Getter for the exit description (defaults to blank) |
| 95 | */ |
| 96 | public String getExitDescription() { |
| 97 | return exitDescription; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Create a new {@link ExitStatus} with a logical combination of the exit |
| 102 | * code, and a concatenation of the descriptions. If either value has a |
| 103 | * higher severity then its exit code will be used in the result. In the |
| 104 | * case of equal severity, the exit code is replaced if the new value is |
| 105 | * alphabetically greater.<br/> |
| 106 | * <br/> |
| 107 | * |
| 108 | * Severity is defined by the exit code: |
| 109 | * <ul> |
| 110 | * <li>Codes beginning with EXECUTING have severity 1</li> |
| 111 | * <li>Codes beginning with COMPLETED have severity 2</li> |
| 112 | * <li>Codes beginning with NOOP have severity 3</li> |
| 113 | * <li>Codes beginning with STOPPED have severity 4</li> |
| 114 | * <li>Codes beginning with FAILED have severity 5</li> |
| 115 | * <li>Codes beginning with UNKNOWN have severity 6</li> |
| 116 | * </ul> |
| 117 | * Others have severity 7, so custom exit codes always win.<br/> |
| 118 | * |
| 119 | * If the input is null just return this. |
| 120 | * |
| 121 | * @param status an {@link ExitStatus} to combine with this one. |
| 122 | * @return a new {@link ExitStatus} combining the current value and the |
| 123 | * argument provided. |
| 124 | */ |
| 125 | public ExitStatus and(ExitStatus status) { |
| 126 | if (status == null) { |
| 127 | return this; |
| 128 | } |
| 129 | ExitStatus result = addExitDescription(status.exitDescription); |
| 130 | if (compareTo(status) < 0) { |
| 131 | result = result.replaceExitCode(status.exitCode); |
| 132 | } |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param status an {@link ExitStatus} to compare |
| 138 | * @return 1,0,-1 according to the severity and exit code |
| 139 | */ |
| 140 | public int compareTo(ExitStatus status) { |
| 141 | if (severity(status) > severity(this)) { |
| 142 | return -1; |
| 143 | } |
| 144 | if (severity(status) < severity(this)) { |
| 145 | return 1; |
| 146 | } |
| 147 | return this.getExitCode().compareTo(status.getExitCode()); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @param status |
| 152 | * @return |
| 153 | */ |
| 154 | private int severity(ExitStatus status) { |
| 155 | if (status.exitCode.startsWith(EXECUTING.exitCode)) { |
| 156 | return 1; |
| 157 | } |
| 158 | if (status.exitCode.startsWith(COMPLETED.exitCode)) { |
| 159 | return 2; |
| 160 | } |
| 161 | if (status.exitCode.startsWith(NOOP.exitCode)) { |
| 162 | return 3; |
| 163 | } |
| 164 | if (status.exitCode.startsWith(STOPPED.exitCode)) { |
| 165 | return 4; |
| 166 | } |
| 167 | if (status.exitCode.startsWith(FAILED.exitCode)) { |
| 168 | return 5; |
| 169 | } |
| 170 | if (status.exitCode.startsWith(UNKNOWN.exitCode)) { |
| 171 | return 6; |
| 172 | } |
| 173 | return 7; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * (non-Javadoc) |
| 178 | * |
| 179 | * @see java.lang.Object#toString() |
| 180 | */ |
| 181 | public String toString() { |
| 182 | return String.format("exitCode=%s;exitDescription=%s", exitCode, exitDescription); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Compare the fields one by one. |
| 187 | * |
| 188 | * @see java.lang.Object#equals(java.lang.Object) |
| 189 | */ |
| 190 | public boolean equals(Object obj) { |
| 191 | if (obj == null) { |
| 192 | return false; |
| 193 | } |
| 194 | return toString().equals(obj.toString()); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Compatible with the equals implementation. |
| 199 | * |
| 200 | * @see java.lang.Object#hashCode() |
| 201 | */ |
| 202 | public int hashCode() { |
| 203 | return toString().hashCode(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Add an exit code to an existing {@link ExitStatus}. If there is already a |
| 208 | * code present tit will be replaced. |
| 209 | * |
| 210 | * @param code the code to add |
| 211 | * @return a new {@link ExitStatus} with the same properties but a new exit |
| 212 | * code. |
| 213 | */ |
| 214 | public ExitStatus replaceExitCode(String code) { |
| 215 | return new ExitStatus(code, exitDescription); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Check if this status represents a running process. |
| 220 | * |
| 221 | * @return true if the exit code is "RUNNING" or "UNKNOWN" |
| 222 | */ |
| 223 | public boolean isRunning() { |
| 224 | return "RUNNING".equals(this.exitCode) || "UNKNOWN".equals(this.exitCode); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Add an exit description to an existing {@link ExitStatus}. If there is |
| 229 | * already a description present the two will be concatenated with a |
| 230 | * semicolon. |
| 231 | * |
| 232 | * @param description the description to add |
| 233 | * @return a new {@link ExitStatus} with the same properties but a new exit |
| 234 | * description |
| 235 | */ |
| 236 | public ExitStatus addExitDescription(String description) { |
| 237 | StringBuffer buffer = new StringBuffer(); |
| 238 | boolean changed = StringUtils.hasText(description) && !exitDescription.equals(description); |
| 239 | if (StringUtils.hasText(exitDescription)) { |
| 240 | buffer.append(exitDescription); |
| 241 | if (changed) { |
| 242 | buffer.append("; "); |
| 243 | } |
| 244 | } |
| 245 | if (changed) { |
| 246 | buffer.append(description); |
| 247 | } |
| 248 | return new ExitStatus(exitCode, buffer.toString()); |
| 249 | } |
| 250 | |
| 251 | } |