| 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.job.flow.support.state; |
| 18 | |
| 19 | import org.springframework.batch.core.job.flow.FlowExecutionStatus; |
| 20 | import org.springframework.batch.core.job.flow.FlowExecutor; |
| 21 | import org.springframework.batch.core.job.flow.State; |
| 22 | |
| 23 | /** |
| 24 | * {@link State} implementation for ending a job if it is in progress and |
| 25 | * continuing if just starting. |
| 26 | * |
| 27 | * @author Dave Syer |
| 28 | * @since 2.0 |
| 29 | */ |
| 30 | public class EndState extends AbstractState { |
| 31 | |
| 32 | private final FlowExecutionStatus status; |
| 33 | |
| 34 | private final boolean abandon; |
| 35 | |
| 36 | private final String code; |
| 37 | |
| 38 | /** |
| 39 | * @param status The {@link FlowExecutionStatus} to end with |
| 40 | * @param name The name of the state |
| 41 | */ |
| 42 | public EndState(FlowExecutionStatus status, String name) { |
| 43 | this(status, status.getName(), name); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param status The {@link FlowExecutionStatus} to end with |
| 48 | * @param name The name of the state |
| 49 | */ |
| 50 | public EndState(FlowExecutionStatus status, String code, String name) { |
| 51 | this(status, code, name, false); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param status The {@link FlowExecutionStatus} to end with |
| 56 | * @param name The name of the state |
| 57 | * @param abandon flag to indicate that previous step execution can be |
| 58 | * marked as abandoned (if there is one) |
| 59 | * |
| 60 | */ |
| 61 | public EndState(FlowExecutionStatus status, String code, String name, boolean abandon) { |
| 62 | super(name); |
| 63 | this.status = status; |
| 64 | this.code = code; |
| 65 | this.abandon = abandon; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Return the {@link FlowExecutionStatus} stored. |
| 70 | * |
| 71 | * @see State#handle(FlowExecutor) |
| 72 | */ |
| 73 | @Override |
| 74 | public FlowExecutionStatus handle(FlowExecutor executor) throws Exception { |
| 75 | synchronized (executor) { |
| 76 | |
| 77 | if (status.isStop()) { |
| 78 | if (!executor.isRestart()) { |
| 79 | /* |
| 80 | * If there are step executions, then we are not at the |
| 81 | * beginning of a restart. |
| 82 | */ |
| 83 | if (abandon) { |
| 84 | /* |
| 85 | * Only if instructed to do so, upgrade the status of |
| 86 | * last step execution so it is not replayed on a |
| 87 | * restart... |
| 88 | */ |
| 89 | executor.abandonStepExecution(); |
| 90 | } |
| 91 | } |
| 92 | else { |
| 93 | /* |
| 94 | * If we are a stop state and we got this far then it must |
| 95 | * be a restart, so return COMPLETED. |
| 96 | */ |
| 97 | return FlowExecutionStatus.COMPLETED; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | executor.addExitStatus(code); |
| 102 | return status; |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* (non-Javadoc) |
| 108 | * @see org.springframework.batch.core.job.flow.State#isEndState() |
| 109 | */ |
| 110 | public boolean isEndState() { |
| 111 | return !status.isStop(); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * (non-Javadoc) |
| 116 | * |
| 117 | * @see java.lang.Object#toString() |
| 118 | */ |
| 119 | @Override |
| 120 | public String toString() { |
| 121 | return super.toString() + " status=[" + status + "]"; |
| 122 | } |
| 123 | } |