1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.core.job.builder;
17
18 import java.util.ArrayList;
19 import java.util.LinkedHashSet;
20 import java.util.List;
21 import java.util.Set;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.springframework.batch.core.Job;
26 import org.springframework.batch.core.JobExecutionListener;
27 import org.springframework.batch.core.JobParametersIncrementer;
28 import org.springframework.batch.core.JobParametersValidator;
29 import org.springframework.batch.core.job.AbstractJob;
30 import org.springframework.batch.core.repository.JobRepository;
31
32
33
34
35
36
37
38
39 public abstract class JobBuilderHelper<B extends JobBuilderHelper<B>> {
40
41 protected final Log logger = LogFactory.getLog(getClass());
42
43 private final CommonStepProperties properties;
44
45 public JobBuilderHelper(String name) {
46 this.properties = new CommonStepProperties();
47 properties.name = name;
48 }
49
50
51
52
53
54
55 protected JobBuilderHelper(JobBuilderHelper<?> parent) {
56 this.properties = new CommonStepProperties(parent.properties);
57 }
58
59
60
61
62
63
64
65 public B validator(JobParametersValidator jobParametersValidator) {
66 properties.jobParametersValidator = jobParametersValidator;
67 @SuppressWarnings("unchecked")
68 B result = (B) this;
69 return result;
70 }
71
72
73
74
75
76
77
78 public B incrementer(JobParametersIncrementer jobParametersIncrementer) {
79 properties.jobParametersIncrementer = jobParametersIncrementer;
80 @SuppressWarnings("unchecked")
81 B result = (B) this;
82 return result;
83 }
84
85
86
87
88
89
90
91 public B repository(JobRepository jobRepository) {
92 properties.jobRepository = jobRepository;
93 @SuppressWarnings("unchecked")
94 B result = (B) this;
95 return result;
96 }
97
98
99
100
101
102
103
104 public B listener(JobExecutionListener listener) {
105 properties.addJobExecutionListener(listener);
106 @SuppressWarnings("unchecked")
107 B result = (B) this;
108 return result;
109 }
110
111
112
113
114
115
116 public B preventRestart() {
117 properties.restartable = false;
118 @SuppressWarnings("unchecked")
119 B result = (B) this;
120 return result;
121 }
122
123 protected String getName() {
124 return properties.name;
125 }
126
127 protected JobRepository getJobRepository() {
128 return properties.jobRepository;
129 }
130
131 protected boolean isRestartable() {
132 return properties.restartable;
133 }
134
135 protected void enhance(Job target) {
136
137 if (target instanceof AbstractJob) {
138
139 AbstractJob job = (AbstractJob) target;
140 job.setJobRepository(properties.getJobRepository());
141
142 JobParametersIncrementer jobParametersIncrementer = properties.getJobParametersIncrementer();
143 if (jobParametersIncrementer != null) {
144 job.setJobParametersIncrementer(jobParametersIncrementer);
145 }
146 JobParametersValidator jobParametersValidator = properties.getJobParametersValidator();
147 if (jobParametersValidator != null) {
148 job.setJobParametersValidator(jobParametersValidator);
149 }
150
151 Boolean restartable = properties.getRestartable();
152 if (restartable != null) {
153 job.setRestartable(restartable);
154 }
155
156 List<JobExecutionListener> listeners = properties.getJobExecutionListeners();
157 if (!listeners.isEmpty()) {
158 job.setJobExecutionListeners(listeners.toArray(new JobExecutionListener[0]));
159 }
160
161 }
162
163 }
164
165 public static class CommonStepProperties {
166
167 private Set<JobExecutionListener> jobExecutionListeners = new LinkedHashSet<JobExecutionListener>();
168
169 private boolean restartable = true;
170
171 private JobRepository jobRepository;
172
173 private JobParametersIncrementer jobParametersIncrementer;
174
175 private JobParametersValidator jobParametersValidator;
176
177 public CommonStepProperties() {
178 }
179
180 public CommonStepProperties(CommonStepProperties properties) {
181 this.name = properties.name;
182 this.restartable = properties.restartable;
183 this.jobRepository = properties.jobRepository;
184 }
185
186 public JobParametersIncrementer getJobParametersIncrementer() {
187 return jobParametersIncrementer;
188 }
189
190 public void setJobParametersIncrementer(JobParametersIncrementer jobParametersIncrementer) {
191 this.jobParametersIncrementer = jobParametersIncrementer;
192 }
193
194 public JobParametersValidator getJobParametersValidator() {
195 return jobParametersValidator;
196 }
197
198 public void setJobParametersValidator(JobParametersValidator jobParametersValidator) {
199 this.jobParametersValidator = jobParametersValidator;
200 }
201
202 public JobRepository getJobRepository() {
203 return jobRepository;
204 }
205
206 public void setJobRepository(JobRepository jobRepository) {
207 this.jobRepository = jobRepository;
208 }
209
210 public String getName() {
211 return name;
212 }
213
214 public void setName(String name) {
215 this.name = name;
216 }
217
218 public List<JobExecutionListener> getJobExecutionListeners() {
219 return new ArrayList<JobExecutionListener>(jobExecutionListeners);
220 }
221
222 public void addStepExecutionListeners(List<JobExecutionListener> jobExecutionListeners) {
223 this.jobExecutionListeners.addAll(jobExecutionListeners);
224 }
225
226 public void addJobExecutionListener(JobExecutionListener jobExecutionListener) {
227 this.jobExecutionListeners.add(jobExecutionListener);
228 }
229
230 public boolean getRestartable() {
231 return restartable;
232 }
233
234 public void setRestartable(boolean restartable) {
235 this.restartable = restartable;
236 }
237
238 private String name;
239
240 }
241
242 }