1 | /* |
2 | * Copyright 2012-2013 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.job.builder; |
17 | |
18 | import java.util.ArrayList; |
19 | import java.util.List; |
20 | |
21 | import org.springframework.batch.core.Job; |
22 | import org.springframework.batch.core.Step; |
23 | import org.springframework.batch.core.job.SimpleJob; |
24 | import org.springframework.batch.core.job.flow.JobExecutionDecider; |
25 | import org.springframework.core.task.TaskExecutor; |
26 | import org.springframework.util.Assert; |
27 | |
28 | /** |
29 | * @author Dave Syer |
30 | * |
31 | * @since 2.2 |
32 | * |
33 | */ |
34 | public class SimpleJobBuilder extends JobBuilderHelper<SimpleJobBuilder> { |
35 | |
36 | private List<Step> steps = new ArrayList<Step>(); |
37 | |
38 | private JobFlowBuilder builder; |
39 | |
40 | /** |
41 | * Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used. |
42 | * |
43 | * @param parent the parent to use |
44 | */ |
45 | public SimpleJobBuilder(JobBuilderHelper<?> parent) { |
46 | super(parent); |
47 | } |
48 | |
49 | public Job build() { |
50 | if (builder != null) { |
51 | return builder.end().build(); |
52 | } |
53 | SimpleJob job = new SimpleJob(getName()); |
54 | super.enhance(job); |
55 | job.setSteps(steps); |
56 | try { |
57 | job.afterPropertiesSet(); |
58 | } |
59 | catch (Exception e) { |
60 | throw new JobBuilderException(e); |
61 | } |
62 | return job; |
63 | } |
64 | |
65 | /** |
66 | * Start the job with this step. |
67 | * |
68 | * @param step a step to start with |
69 | * @return this for fluent chaining |
70 | */ |
71 | public SimpleJobBuilder start(Step step) { |
72 | if (steps.isEmpty()) { |
73 | steps.add(step); |
74 | } |
75 | else { |
76 | steps.set(0, step); |
77 | } |
78 | return this; |
79 | } |
80 | |
81 | /** |
82 | * Branch into a flow conditional on the outcome of the current step. |
83 | * |
84 | * @param pattern a pattern for the exit status of the current step |
85 | * @return a builder for fluent chaining |
86 | */ |
87 | public FlowBuilder.TransitionBuilder<FlowJobBuilder> on(String pattern) { |
88 | Assert.state(steps.size() > 0, "You have to start a job with a step"); |
89 | for (Step step : steps) { |
90 | if (builder == null) { |
91 | builder = new JobFlowBuilder(new FlowJobBuilder(this), step); |
92 | } |
93 | else { |
94 | builder.next(step); |
95 | } |
96 | } |
97 | return builder.on(pattern); |
98 | } |
99 | |
100 | /** |
101 | * Start with this decider. Returns a flow builder and when the flow is ended a job builder will be returned to |
102 | * continue the job configuration if needed. |
103 | * |
104 | * @param decider a decider to execute first |
105 | * @return builder for fluent chaining |
106 | */ |
107 | public JobFlowBuilder start(JobExecutionDecider decider) { |
108 | if (builder == null) { |
109 | builder = new JobFlowBuilder(new FlowJobBuilder(this), decider); |
110 | } |
111 | else { |
112 | builder.start(decider); |
113 | } |
114 | if (!steps.isEmpty()) { |
115 | steps.remove(0); |
116 | } |
117 | for (Step step : steps) { |
118 | builder.next(step); |
119 | } |
120 | return builder; |
121 | } |
122 | |
123 | /** |
124 | * Continue with this decider if the previous step was successful. Returns a flow builder and when the flow is ended |
125 | * a job builder will be returned to continue the job configuration if needed. |
126 | * |
127 | * @param decider a decider to execute next |
128 | * @return builder for fluent chaining |
129 | */ |
130 | public JobFlowBuilder next(JobExecutionDecider decider) { |
131 | for (Step step : steps) { |
132 | if (builder == null) { |
133 | builder = new JobFlowBuilder(new FlowJobBuilder(this), step); |
134 | } |
135 | else { |
136 | builder.next(step); |
137 | } |
138 | } |
139 | if (builder == null) { |
140 | builder = new JobFlowBuilder(new FlowJobBuilder(this), decider); |
141 | } |
142 | else { |
143 | builder.next(decider); |
144 | } |
145 | return builder; |
146 | } |
147 | |
148 | /** |
149 | * Continue or end a job with this step if the previous step was successful. |
150 | * |
151 | * @param step a step to execute next |
152 | * @return this for fluent chaining |
153 | */ |
154 | public SimpleJobBuilder next(Step step) { |
155 | steps.add(step); |
156 | return this; |
157 | } |
158 | |
159 | /** |
160 | * @param executor |
161 | * @return builder for fluent chaining |
162 | */ |
163 | public JobFlowBuilder.SplitBuilder<FlowJobBuilder> split(TaskExecutor executor) { |
164 | for (Step step : steps) { |
165 | if (builder == null) { |
166 | builder = new JobFlowBuilder(new FlowJobBuilder(this), step); |
167 | } |
168 | else { |
169 | builder.next(step); |
170 | } |
171 | } |
172 | if (builder == null) { |
173 | builder = new JobFlowBuilder(new FlowJobBuilder(this)); |
174 | } |
175 | return builder.split(executor); |
176 | } |
177 | |
178 | } |