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