| 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 CommonJobProperties properties; |
| 44 | |
| 45 | public JobBuilderHelper(String name) { |
| 46 | this.properties = new CommonJobProperties(); |
| 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 CommonJobProperties(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 CommonJobProperties { |
| 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 CommonJobProperties() { |
| 178 | } |
| 179 | |
| 180 | public CommonJobProperties(CommonJobProperties properties) { |
| 181 | this.name = properties.name; |
| 182 | this.restartable = properties.restartable; |
| 183 | this.jobRepository = properties.jobRepository; |
| 184 | this.jobExecutionListeners = new LinkedHashSet<JobExecutionListener>(properties.jobExecutionListeners); |
| 185 | this.jobParametersIncrementer = properties.jobParametersIncrementer; |
| 186 | this.jobParametersValidator = properties.jobParametersValidator; |
| 187 | } |
| 188 | |
| 189 | public JobParametersIncrementer getJobParametersIncrementer() { |
| 190 | return jobParametersIncrementer; |
| 191 | } |
| 192 | |
| 193 | public void setJobParametersIncrementer(JobParametersIncrementer jobParametersIncrementer) { |
| 194 | this.jobParametersIncrementer = jobParametersIncrementer; |
| 195 | } |
| 196 | |
| 197 | public JobParametersValidator getJobParametersValidator() { |
| 198 | return jobParametersValidator; |
| 199 | } |
| 200 | |
| 201 | public void setJobParametersValidator(JobParametersValidator jobParametersValidator) { |
| 202 | this.jobParametersValidator = jobParametersValidator; |
| 203 | } |
| 204 | |
| 205 | public JobRepository getJobRepository() { |
| 206 | return jobRepository; |
| 207 | } |
| 208 | |
| 209 | public void setJobRepository(JobRepository jobRepository) { |
| 210 | this.jobRepository = jobRepository; |
| 211 | } |
| 212 | |
| 213 | public String getName() { |
| 214 | return name; |
| 215 | } |
| 216 | |
| 217 | public void setName(String name) { |
| 218 | this.name = name; |
| 219 | } |
| 220 | |
| 221 | public List<JobExecutionListener> getJobExecutionListeners() { |
| 222 | return new ArrayList<JobExecutionListener>(jobExecutionListeners); |
| 223 | } |
| 224 | |
| 225 | public void addStepExecutionListeners(List<JobExecutionListener> jobExecutionListeners) { |
| 226 | this.jobExecutionListeners.addAll(jobExecutionListeners); |
| 227 | } |
| 228 | |
| 229 | public void addJobExecutionListener(JobExecutionListener jobExecutionListener) { |
| 230 | this.jobExecutionListeners.add(jobExecutionListener); |
| 231 | } |
| 232 | |
| 233 | public boolean getRestartable() { |
| 234 | return restartable; |
| 235 | } |
| 236 | |
| 237 | public void setRestartable(boolean restartable) { |
| 238 | this.restartable = restartable; |
| 239 | } |
| 240 | |
| 241 | private String name; |
| 242 | |
| 243 | } |
| 244 | |
| 245 | } |