View Javadoc

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.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   * A base class and utility for other job builders providing access to common properties like job repository.
34   * 
35   * @author Dave Syer
36   * 
37   * @since 2.2
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  	 * Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used.
52  	 * 
53  	 * @param parent a parent helper containing common step properties
54  	 */
55  	protected JobBuilderHelper(JobBuilderHelper<?> parent) {
56  		this.properties = new CommonStepProperties(parent.properties);
57  	}
58  
59  	/**
60  	 * Add a job parameters validator.
61  	 * 
62  	 * @param jobParametersValidator a job parameters validator
63  	 * @return this to enable fluent chaining
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  	 * Add a job parameters incrementer.
74  	 * 
75  	 * @param jobParametersIncrementer a job parameters incrementer
76  	 * @return this to enable fluent chaining
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  	 * Sets the job repository for the job.
87  	 * 
88  	 * @param jobRepository the job repository (mandatory)
89  	 * @return this to enable fluent chaining
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  	 * Register a job execution listener.
100 	 * 
101 	 * @param listener a job execution listener
102 	 * @return this to enable fluent chaining
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 	 * Set a flag to prevent restart an execution of this job even if it has failed.
113 	 * 
114 	 * @return this to enable fluent chaining
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 }