EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.core.job.flow]

COVERAGE SUMMARY FOR SOURCE FILE [JobFlowExecutor.java]

nameclass, %method, %block, %line, %
JobFlowExecutor.java100% (1/1)90%  (9/10)72%  (121/167)77%  (28.6/37)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JobFlowExecutor100% (1/1)90%  (9/10)72%  (121/167)77%  (28.6/37)
isRestart (): boolean 0%   (0/1)0%   (0/15)0%   (0/3)
abandonStepExecution (): void 100% (1/1)40%  (8/20)52%  (2.6/5)
executeStep (Step): String 100% (1/1)54%  (20/37)71%  (5/7)
findBatchStatus (FlowExecutionStatus): BatchStatus 100% (1/1)92%  (24/26)75%  (3/4)
JobFlowExecutor (JobRepository, StepHandler, JobExecution): void 100% (1/1)100% (24/24)100% (8/8)
addExitStatus (String): void 100% (1/1)100% (10/10)100% (2/2)
close (FlowExecution): void 100% (1/1)100% (5/5)100% (2/2)
getJobExecution (): JobExecution 100% (1/1)100% (3/3)100% (1/1)
getStepExecution (): StepExecution 100% (1/1)100% (5/5)100% (1/1)
updateJobExecutionStatus (FlowExecutionStatus): void 100% (1/1)100% (22/22)100% (4/4)

1/*
2 * Copyright 2006-2009 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 
17package org.springframework.batch.core.job.flow;
18 
19import org.springframework.batch.core.BatchStatus;
20import org.springframework.batch.core.ExitStatus;
21import org.springframework.batch.core.JobExecution;
22import org.springframework.batch.core.JobInterruptedException;
23import org.springframework.batch.core.StartLimitExceededException;
24import org.springframework.batch.core.Step;
25import org.springframework.batch.core.StepExecution;
26import org.springframework.batch.core.job.StepHandler;
27import org.springframework.batch.core.repository.JobRepository;
28import org.springframework.batch.core.repository.JobRestartException;
29 
30/**
31 * Implementation of {@link FlowExecutor} for use in components that need to
32 * execute a flow related to a {@link JobExecution}.
33 * 
34 * @author Dave Syer
35 * 
36 */
37public class JobFlowExecutor implements FlowExecutor {
38 
39        private final ThreadLocal<StepExecution> stepExecutionHolder = new ThreadLocal<StepExecution>();
40 
41        private final JobExecution execution;
42 
43        private ExitStatus exitStatus = ExitStatus.EXECUTING;
44 
45        private final StepHandler stepHandler;
46 
47        private final JobRepository jobRepository;
48 
49        /**
50         * @param execution
51         */
52        public JobFlowExecutor(JobRepository jobRepository, StepHandler stepHandler, JobExecution execution) {
53                this.jobRepository = jobRepository;
54                this.stepHandler = stepHandler;
55                this.execution = execution;
56                stepExecutionHolder.set(null);
57        }
58 
59        public String executeStep(Step step) throws JobInterruptedException, JobRestartException,
60                        StartLimitExceededException {
61                StepExecution stepExecution = stepHandler.handleStep(step, execution);
62                stepExecutionHolder.set(stepExecution);
63                if (stepExecution == null) {
64                        return  ExitStatus.COMPLETED.getExitCode();                        
65                }
66                if (stepExecution.isTerminateOnly()) {
67                        throw new JobInterruptedException("Step requested termination: "+stepExecution, stepExecution.getStatus());
68                }
69                return stepExecution.getExitStatus().getExitCode();
70        }
71 
72        public void abandonStepExecution() {
73                StepExecution lastStepExecution = stepExecutionHolder.get();
74                if (lastStepExecution != null && lastStepExecution.getStatus().isGreaterThan(BatchStatus.STOPPING)) {
75                        lastStepExecution.upgradeStatus(BatchStatus.ABANDONED);
76                        jobRepository.update(lastStepExecution);
77                }
78        }
79 
80        public void updateJobExecutionStatus(FlowExecutionStatus status) {
81                execution.setStatus(findBatchStatus(status));
82                exitStatus = exitStatus.and(new ExitStatus(status.getName()));
83                execution.setExitStatus(exitStatus);
84        }
85 
86        public JobExecution getJobExecution() {
87                return execution;
88        }
89 
90        public StepExecution getStepExecution() {
91                return stepExecutionHolder.get();
92        }
93 
94        public void close(FlowExecution result) {
95                stepExecutionHolder.set(null);
96        }
97 
98        public boolean isRestart() {
99                if (getStepExecution() != null && getStepExecution().getStatus() == BatchStatus.ABANDONED) {
100                        /*
101                         * This is assumed to be the last step execution and it was marked
102                         * abandoned, so we are in a restart of a stopped step.
103                         */
104                        // TODO: mark the step execution in some more definitive way?
105                        return true;
106                }
107                return execution.getStepExecutions().isEmpty();
108        }
109 
110        public void addExitStatus(String code) {
111                exitStatus = exitStatus.and(new ExitStatus(code));
112        }
113 
114        /**
115         * @param status
116         * @return
117         */
118        private BatchStatus findBatchStatus(FlowExecutionStatus status) {
119                for (BatchStatus batchStatus : BatchStatus.values()) {
120                        if (status.getName().startsWith(batchStatus.toString())) {
121                                return batchStatus;
122                        }
123                }
124                return BatchStatus.UNKNOWN;
125        }
126 
127}

[all classes][org.springframework.batch.core.job.flow]
EMMA 2.0.5312 (C) Vladimir Roubtsov