1 | /* |
2 | * Copyright 2006-2011 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.core.step.builder; |
17 | |
18 | import org.springframework.batch.core.Job; |
19 | import org.springframework.batch.core.Step; |
20 | import org.springframework.batch.core.launch.JobLauncher; |
21 | import org.springframework.batch.core.launch.support.SimpleJobLauncher; |
22 | import org.springframework.batch.core.step.job.JobParametersExtractor; |
23 | import org.springframework.batch.core.step.job.JobStep; |
24 | |
25 | /** |
26 | * A step builder for {@link JobStep} instances. A job step executes a nested {@link Job} with parameters taken from the |
27 | * parent job or from the step execution. |
28 | * |
29 | * @author Dave Syer |
30 | * |
31 | * @since 2.2 |
32 | */ |
33 | public class JobStepBuilder extends StepBuilderHelper<JobStepBuilder> { |
34 | |
35 | private Job job; |
36 | |
37 | private JobLauncher jobLauncher; |
38 | |
39 | private JobParametersExtractor jobParametersExtractor; |
40 | |
41 | /** |
42 | * Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used. |
43 | * |
44 | * @param parent a parent helper containing common step properties |
45 | */ |
46 | public JobStepBuilder(StepBuilderHelper<?> parent) { |
47 | super(parent); |
48 | } |
49 | |
50 | /** |
51 | * Provide a job to execute during the step. |
52 | * |
53 | * @param job the job to execute |
54 | * @return this for fluent chaining |
55 | */ |
56 | public JobStepBuilder job(Job job) { |
57 | this.job = job; |
58 | return this; |
59 | } |
60 | |
61 | /** |
62 | * Add a job launcher. Defaults to a simple job launcher. |
63 | * |
64 | * @param jobLauncher the job launcher to use |
65 | * @return this for fluent chaining |
66 | */ |
67 | public JobStepBuilder launcher(JobLauncher jobLauncher) { |
68 | this.jobLauncher = jobLauncher; |
69 | return this; |
70 | } |
71 | |
72 | /** |
73 | * Provide a job parameters extractor. Useful for extracting job parameters from the parent step execution context |
74 | * or job parameters. |
75 | * |
76 | * @param jobParametersExtractor the job parameters extractor to use |
77 | * @return this for fluent chaining |
78 | */ |
79 | public JobStepBuilder parametersExtractor(JobParametersExtractor jobParametersExtractor) { |
80 | this.jobParametersExtractor = jobParametersExtractor; |
81 | return this; |
82 | } |
83 | |
84 | /** |
85 | * Build a step from the job provided. |
86 | * |
87 | * @return a new job step |
88 | */ |
89 | public Step build() { |
90 | |
91 | JobStep step = new JobStep(); |
92 | step.setName(getName()); |
93 | super.enhance(step); |
94 | if (job != null) { |
95 | step.setJob(job); |
96 | } |
97 | if (jobParametersExtractor != null) { |
98 | step.setJobParametersExtractor(jobParametersExtractor); |
99 | } |
100 | if (jobLauncher == null) { |
101 | SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); |
102 | jobLauncher.setJobRepository(getJobRepository()); |
103 | try { |
104 | jobLauncher.afterPropertiesSet(); |
105 | } |
106 | catch (Exception e) { |
107 | throw new StepBuilderException(e); |
108 | } |
109 | this.jobLauncher = jobLauncher; |
110 | } |
111 | step.setJobLauncher(jobLauncher); |
112 | try { |
113 | step.afterPropertiesSet(); |
114 | } |
115 | catch (Exception e) { |
116 | throw new StepBuilderException(e); |
117 | } |
118 | return step; |
119 | |
120 | } |
121 | |
122 | } |