| 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 | |
| 17 | package org.springframework.batch.core; |
| 18 | |
| 19 | import java.io.ObjectStreamException; |
| 20 | import java.io.Serializable; |
| 21 | |
| 22 | /** |
| 23 | * Typesafe enumeration representing the status of an artifact within the batch environment. See Effective Java |
| 24 | * Programming by Joshua Bloch for more details on the pattern used. |
| 25 | * |
| 26 | * A BatchStatus can be safely serialized, however, it should be noted that the pattern can break down if different |
| 27 | * class loaders load the enumeration. |
| 28 | * |
| 29 | * This class is immutable and therefore thread-safe. |
| 30 | * |
| 31 | * @author Lucas Ward |
| 32 | * @author Greg Kick |
| 33 | */ |
| 34 | |
| 35 | public class BatchStatus implements Serializable { |
| 36 | |
| 37 | private static final long serialVersionUID = 1634960297477743037L; |
| 38 | |
| 39 | private final String name; |
| 40 | |
| 41 | private BatchStatus(String name) { |
| 42 | this.name = name; |
| 43 | } |
| 44 | |
| 45 | private Object readResolve() throws ObjectStreamException { |
| 46 | return getStatus(name); |
| 47 | } |
| 48 | |
| 49 | public String toString() { |
| 50 | return name; |
| 51 | } |
| 52 | |
| 53 | public static final BatchStatus COMPLETED = new BatchStatus("COMPLETED"); |
| 54 | |
| 55 | public static final BatchStatus STARTED = new BatchStatus("STARTED"); |
| 56 | |
| 57 | public static final BatchStatus STARTING = new BatchStatus("STARTING"); |
| 58 | |
| 59 | public static final BatchStatus FAILED = new BatchStatus("FAILED"); |
| 60 | |
| 61 | public static final BatchStatus STOPPING = new BatchStatus("STOPPING"); |
| 62 | |
| 63 | public static final BatchStatus STOPPED = new BatchStatus("STOPPED"); |
| 64 | |
| 65 | public static final BatchStatus UNKNOWN = new BatchStatus("UNKNOWN"); |
| 66 | |
| 67 | private static final BatchStatus[] VALUES = { STARTING, STARTED, COMPLETED, FAILED, STOPPING, STOPPED, UNKNOWN }; |
| 68 | |
| 69 | /** |
| 70 | * Given a string representation of a status, return the appropriate BatchStatus. |
| 71 | * |
| 72 | * @param statusAsString string representation of a status |
| 73 | * @return a valid BatchStatus or null if the input is null |
| 74 | * @throws IllegalArgumentException if no status matches provided string. |
| 75 | */ |
| 76 | public static BatchStatus getStatus(String statusAsString) { |
| 77 | if (statusAsString == null) { |
| 78 | return null; |
| 79 | } |
| 80 | final String upperCaseStatusAsString = statusAsString.toUpperCase(); |
| 81 | for (int i = 0; i < VALUES.length; i++) { |
| 82 | if (VALUES[i].toString().equals(upperCaseStatusAsString)) { |
| 83 | return VALUES[i]; |
| 84 | } |
| 85 | } |
| 86 | throw new IllegalArgumentException("The string did not match a valid status."); |
| 87 | } |
| 88 | } |