| 1 | /* |
| 2 | * Copyright 2006-2007 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.item; |
| 17 | |
| 18 | import org.springframework.batch.core.Step; |
| 19 | import org.springframework.batch.core.StepExecutionListener; |
| 20 | import org.springframework.batch.core.StepListener; |
| 21 | import org.springframework.batch.core.repository.JobRepository; |
| 22 | import org.springframework.batch.item.ItemReader; |
| 23 | import org.springframework.batch.item.ItemStream; |
| 24 | import org.springframework.batch.item.ItemWriter; |
| 25 | import org.springframework.batch.item.validator.Validator; |
| 26 | import org.springframework.beans.factory.BeanNameAware; |
| 27 | import org.springframework.beans.factory.FactoryBean; |
| 28 | import org.springframework.transaction.PlatformTransactionManager; |
| 29 | import org.springframework.transaction.interceptor.DefaultTransactionAttribute; |
| 30 | import org.springframework.transaction.interceptor.TransactionAttribute; |
| 31 | import org.springframework.util.Assert; |
| 32 | |
| 33 | /** |
| 34 | * Base class for factory beans for {@link ItemOrientedStep}. Ensures that all |
| 35 | * the mandatory properties are set, and provides basic support for the |
| 36 | * {@link Step} interface responsibilities like start limit. Supports |
| 37 | * registration of {@link ItemStream}s and {@link StepListener}s. |
| 38 | * |
| 39 | * @see SimpleStepFactoryBean |
| 40 | * @see RepeatOperationsStepFactoryBean |
| 41 | * |
| 42 | * @author Dave Syer |
| 43 | * |
| 44 | */ |
| 45 | public abstract class AbstractStepFactoryBean implements FactoryBean, BeanNameAware { |
| 46 | |
| 47 | private String name; |
| 48 | |
| 49 | private int startLimit = Integer.MAX_VALUE; |
| 50 | |
| 51 | private boolean allowStartIfComplete; |
| 52 | |
| 53 | private ItemReader itemReader; |
| 54 | |
| 55 | private ItemWriter itemWriter; |
| 56 | |
| 57 | private PlatformTransactionManager transactionManager; |
| 58 | |
| 59 | private TransactionAttribute transactionAttribute; |
| 60 | |
| 61 | private JobRepository jobRepository; |
| 62 | |
| 63 | private boolean singleton = true; |
| 64 | |
| 65 | private Validator jobRepositoryValidator = new TransactionInterceptorValidator(1); |
| 66 | |
| 67 | private ItemStream[] streams = new ItemStream[0]; |
| 68 | |
| 69 | private StepListener[] listeners = new StepListener[0]; |
| 70 | |
| 71 | /** |
| 72 | * |
| 73 | */ |
| 74 | public AbstractStepFactoryBean() { |
| 75 | super(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set the bean name property, which will become the name of the |
| 80 | * {@link Step} when it is created. |
| 81 | * |
| 82 | * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) |
| 83 | */ |
| 84 | public void setBeanName(String name) { |
| 85 | this.name = name; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Public getter for the String. |
| 90 | * @return the name |
| 91 | */ |
| 92 | public String getName() { |
| 93 | return name; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Public setter for the startLimit. |
| 98 | * |
| 99 | * @param startLimit the startLimit to set |
| 100 | */ |
| 101 | public void setStartLimit(int startLimit) { |
| 102 | this.startLimit = startLimit; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Public setter for the shouldAllowStartIfComplete. |
| 107 | * |
| 108 | * @param allowStartIfComplete the shouldAllowStartIfComplete to set |
| 109 | */ |
| 110 | public void setAllowStartIfComplete(boolean allowStartIfComplete) { |
| 111 | this.allowStartIfComplete = allowStartIfComplete; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param itemReader the itemReader to set |
| 116 | */ |
| 117 | public void setItemReader(ItemReader itemReader) { |
| 118 | this.itemReader = itemReader; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param itemWriter the itemWriter to set |
| 123 | */ |
| 124 | public void setItemWriter(ItemWriter itemWriter) { |
| 125 | this.itemWriter = itemWriter; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * The streams to inject into the {@link Step}. Any instance of |
| 130 | * {@link ItemStream} can be used, and will then receive callbacks at the |
| 131 | * appropriate stage in the step. |
| 132 | * |
| 133 | * @param streams an array of listeners |
| 134 | */ |
| 135 | public void setStreams(ItemStream[] streams) { |
| 136 | this.streams = streams; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * The listeners to inject into the {@link Step}. Any instance of |
| 141 | * {@link StepListener} can be used, and will then receive callbacks at the |
| 142 | * appropriate stage in the step. |
| 143 | * |
| 144 | * @param listeners an array of listeners |
| 145 | */ |
| 146 | public void setListeners(StepListener[] listeners) { |
| 147 | this.listeners = listeners; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Protected getter for the {@link StepListener}s. |
| 152 | * @return the listeners |
| 153 | */ |
| 154 | protected StepListener[] getListeners() { |
| 155 | return listeners; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Protected getter for the {@link ItemReader} for subclasses to use. |
| 160 | * @return the itemReader |
| 161 | */ |
| 162 | protected ItemReader getItemReader() { |
| 163 | return itemReader; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Protected getter for the {@link ItemWriter} for subclasses to use |
| 168 | * @return the itemWriter |
| 169 | */ |
| 170 | protected ItemWriter getItemWriter() { |
| 171 | return itemWriter; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Public setter for {@link JobRepository}. |
| 176 | * |
| 177 | * @param jobRepository is a mandatory dependence (no default). |
| 178 | */ |
| 179 | public void setJobRepository(JobRepository jobRepository) { |
| 180 | this.jobRepository = jobRepository; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Public setter for the {@link PlatformTransactionManager}. |
| 185 | * |
| 186 | * @param transactionManager the transaction manager to set |
| 187 | */ |
| 188 | public void setTransactionManager(PlatformTransactionManager transactionManager) { |
| 189 | this.transactionManager = transactionManager; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Public setter for the {@link TransactionAttribute}. |
| 194 | * @param transactionAttribute the {@link TransactionAttribute} to set |
| 195 | */ |
| 196 | public void setTransactionAttribute(TransactionAttribute transactionAttribute) { |
| 197 | this.transactionAttribute = transactionAttribute; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Protected getter for the {@link TransactionAttribute} for subclasses only. |
| 202 | * @return the transactionAttribute |
| 203 | */ |
| 204 | protected TransactionAttribute getTransactionAttribute() { |
| 205 | return transactionAttribute!=null?transactionAttribute:new DefaultTransactionAttribute() { |
| 206 | |
| 207 | public boolean rollbackOn(Throwable ex) { |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | }; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Create a {@link Step} from the configuration provided. |
| 216 | * |
| 217 | * @see org.springframework.beans.factory.FactoryBean#getObject() |
| 218 | */ |
| 219 | public final Object getObject() throws Exception { |
| 220 | ItemOrientedStep step = new ItemOrientedStep(getName()); |
| 221 | applyConfiguration(step); |
| 222 | return step; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @param step |
| 227 | * |
| 228 | */ |
| 229 | protected void applyConfiguration(ItemOrientedStep step) { |
| 230 | |
| 231 | Assert.notNull(getItemReader(), "ItemReader must be provided"); |
| 232 | Assert.notNull(getItemWriter(), "ItemWriter must be provided"); |
| 233 | Assert.notNull(transactionManager, "TransactionManager must be provided"); |
| 234 | jobRepositoryValidator.validate(jobRepository); |
| 235 | |
| 236 | step.setItemHandler(new SimpleItemHandler(itemReader, itemWriter)); |
| 237 | step.setTransactionManager(transactionManager); |
| 238 | if (transactionAttribute!=null) { |
| 239 | step.setTransactionAttribute(transactionAttribute); |
| 240 | } |
| 241 | step.setJobRepository(jobRepository); |
| 242 | step.setStartLimit(startLimit); |
| 243 | step.setAllowStartIfComplete(allowStartIfComplete); |
| 244 | |
| 245 | step.setStreams(streams); |
| 246 | |
| 247 | ItemReader itemReader = getItemReader(); |
| 248 | ItemWriter itemWriter = getItemWriter(); |
| 249 | |
| 250 | // Since we are going to wrap these things with listener callbacks we |
| 251 | // need to register them here because the step will not know we did |
| 252 | // that. |
| 253 | if (itemReader instanceof ItemStream) { |
| 254 | step.registerStream((ItemStream) itemReader); |
| 255 | } |
| 256 | if (itemReader instanceof StepExecutionListener) { |
| 257 | step.registerStepExecutionListener((StepExecutionListener) itemReader); |
| 258 | } |
| 259 | if (itemWriter instanceof ItemStream) { |
| 260 | step.registerStream((ItemStream) itemWriter); |
| 261 | } |
| 262 | if (itemWriter instanceof StepExecutionListener) { |
| 263 | step.registerStepExecutionListener((StepExecutionListener) itemWriter); |
| 264 | } |
| 265 | |
| 266 | StepExecutionListener[] stepListeners = BatchListenerFactoryHelper.getStepListeners(listeners); |
| 267 | itemReader = BatchListenerFactoryHelper.getItemReader(itemReader, listeners); |
| 268 | itemWriter = BatchListenerFactoryHelper.getItemWriter(itemWriter, listeners); |
| 269 | |
| 270 | // In case they are used by subclasses: |
| 271 | setItemReader(itemReader); |
| 272 | setItemWriter(itemWriter); |
| 273 | |
| 274 | step.setStepExecutionListeners(stepListeners); |
| 275 | step.setItemHandler(new SimpleItemHandler(itemReader, itemWriter)); |
| 276 | |
| 277 | } |
| 278 | |
| 279 | public Class getObjectType() { |
| 280 | return Step.class; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Returns true by default, but in most cases a {@link Step} should not be |
| 285 | * treated as thread safe. Clients are recommended to create a new step for |
| 286 | * each job execution. |
| 287 | * |
| 288 | * @see org.springframework.beans.factory.FactoryBean#isSingleton() |
| 289 | */ |
| 290 | public boolean isSingleton() { |
| 291 | return this.singleton; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Public setter for the singleton flag. |
| 296 | * @param singleton the value to set. Defaults to true. |
| 297 | */ |
| 298 | public void setSingleton(boolean singleton) { |
| 299 | this.singleton = singleton; |
| 300 | } |
| 301 | |
| 302 | } |