001/*
002 * Copyright 2013-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.jsr.job.flow;
017
018import org.springframework.batch.core.ExitStatus;
019import org.springframework.batch.core.JobExecution;
020import org.springframework.batch.core.job.StepHandler;
021import org.springframework.batch.core.job.flow.FlowExecutionStatus;
022import org.springframework.batch.core.job.flow.JobFlowExecutor;
023import org.springframework.batch.core.repository.JobRepository;
024
025/**
026 * JSR-352 specific {@link JobFlowExecutor}.  Unlike the regular {@link JobFlowExecutor},
027 * this extension does not promote an {@link ExitStatus} from a step to the job level if
028 * a custom exit status has been set on the job.
029 *
030 * @author Michael Minella
031 * @since 3.0
032 */
033public class JsrFlowExecutor extends JobFlowExecutor {
034
035        public JsrFlowExecutor(JobRepository jobRepository,
036                        StepHandler stepHandler, JobExecution execution) {
037                super(jobRepository, stepHandler, execution);
038        }
039
040        /* (non-Javadoc)
041         * @see org.springframework.batch.core.job.flow.JobFlowExecutor#addExitStatus(java.lang.String)
042         */
043        @Override
044        public void addExitStatus(String code) {
045                ExitStatus status = new ExitStatus(code);
046                if((exitStatus != null && ExitStatus.isNonDefaultExitStatus(exitStatus)) && !ExitStatus.isNonDefaultExitStatus(status)) {
047                        exitStatus = exitStatus.and(status);
048                }
049        }
050
051        /* (non-Javadoc)
052         * @see org.springframework.batch.core.job.flow.JobFlowExecutor#updateJobExecutionStatus(org.springframework.batch.core.job.flow.FlowExecutionStatus)
053         */
054        @Override
055        public void updateJobExecutionStatus(FlowExecutionStatus status) {
056                JobExecution execution = super.getJobExecution();
057
058                execution.setStatus(findBatchStatus(status));
059
060                ExitStatus curStatus = execution.getExitStatus();
061                if(ExitStatus.isNonDefaultExitStatus(curStatus)) {
062                        exitStatus = exitStatus.and(new ExitStatus(status.getName()));
063                        execution.setExitStatus(exitStatus);
064                } else {
065                        exitStatus = exitStatus.and(curStatus);
066                        execution.setExitStatus(exitStatus);
067                }
068        }
069}