| 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.repository.JobRepository; |
| 20 | import org.springframework.batch.item.ItemReader; |
| 21 | import org.springframework.batch.item.ItemWriter; |
| 22 | import org.springframework.beans.factory.BeanNameAware; |
| 23 | import org.springframework.beans.factory.FactoryBean; |
| 24 | import org.springframework.transaction.PlatformTransactionManager; |
| 25 | import org.springframework.util.Assert; |
| 26 | |
| 27 | /** |
| 28 | * Base class for factory beans for {@link ItemOrientedStep}. Ensures that all |
| 29 | * the mandatory properties are set, and provides basic support for the |
| 30 | * {@link Step} interface responsibilities like start limit. |
| 31 | * |
| 32 | * @author Dave Syer |
| 33 | * |
| 34 | */ |
| 35 | public abstract class AbstractStepFactoryBean implements FactoryBean, BeanNameAware { |
| 36 | |
| 37 | private String name; |
| 38 | |
| 39 | private int startLimit = Integer.MAX_VALUE; |
| 40 | |
| 41 | private boolean allowStartIfComplete; |
| 42 | |
| 43 | private ItemReader itemReader; |
| 44 | |
| 45 | private ItemWriter itemWriter; |
| 46 | |
| 47 | private PlatformTransactionManager transactionManager; |
| 48 | |
| 49 | private JobRepository jobRepository; |
| 50 | |
| 51 | private boolean singleton = true; |
| 52 | |
| 53 | /** |
| 54 | * |
| 55 | */ |
| 56 | public AbstractStepFactoryBean() { |
| 57 | super(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set the bean name property, which will become the name of the |
| 62 | * {@link Step} when it is created. |
| 63 | * |
| 64 | * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) |
| 65 | */ |
| 66 | public void setBeanName(String name) { |
| 67 | this.name = name; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Public getter for the String. |
| 72 | * @return the name |
| 73 | */ |
| 74 | public String getName() { |
| 75 | return name; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Public setter for the startLimit. |
| 80 | * |
| 81 | * @param startLimit the startLimit to set |
| 82 | */ |
| 83 | public void setStartLimit(int startLimit) { |
| 84 | this.startLimit = startLimit; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Public setter for the shouldAllowStartIfComplete. |
| 89 | * |
| 90 | * @param allowStartIfComplete the shouldAllowStartIfComplete to set |
| 91 | */ |
| 92 | public void setAllowStartIfComplete(boolean allowStartIfComplete) { |
| 93 | this.allowStartIfComplete = allowStartIfComplete; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param itemReader the itemReader to set |
| 98 | */ |
| 99 | public void setItemReader(ItemReader itemReader) { |
| 100 | this.itemReader = itemReader; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param itemWriter the itemWriter to set |
| 105 | */ |
| 106 | public void setItemWriter(ItemWriter itemWriter) { |
| 107 | this.itemWriter = itemWriter; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Protected getter for the {@link ItemReader} for subclasses to use. |
| 112 | * @return the itemReader |
| 113 | */ |
| 114 | protected ItemReader getItemReader() { |
| 115 | return itemReader; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Protected getter for the {@link ItemWriter} for subclasses to use |
| 120 | * @return the itemWriter |
| 121 | */ |
| 122 | protected ItemWriter getItemWriter() { |
| 123 | return itemWriter; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Public setter for {@link JobRepository}. |
| 128 | * |
| 129 | * @param jobRepository is a mandatory dependence (no default). |
| 130 | */ |
| 131 | public void setJobRepository(JobRepository jobRepository) { |
| 132 | this.jobRepository = jobRepository; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Public setter for the {@link PlatformTransactionManager}. |
| 137 | * |
| 138 | * @param transactionManager the transaction manager to set |
| 139 | */ |
| 140 | public void setTransactionManager(PlatformTransactionManager transactionManager) { |
| 141 | this.transactionManager = transactionManager; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Create a {@link Step} from the configuration provided. |
| 146 | * |
| 147 | * @see org.springframework.beans.factory.FactoryBean#getObject() |
| 148 | */ |
| 149 | public final Object getObject() throws Exception { |
| 150 | ItemOrientedStep step = new ItemOrientedStep(getName()); |
| 151 | applyConfiguration(step); |
| 152 | return step; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param step |
| 157 | * |
| 158 | */ |
| 159 | protected void applyConfiguration(ItemOrientedStep step) { |
| 160 | |
| 161 | Assert.notNull(getItemReader(), "ItemReader must be provided"); |
| 162 | Assert.notNull(getItemWriter(), "ItemWriter must be provided"); |
| 163 | Assert.notNull(jobRepository, "JobRepository must be provided"); |
| 164 | Assert.notNull(transactionManager, "TransactionManager must be provided"); |
| 165 | |
| 166 | step.setItemHandler(new SimpleItemHandler(itemReader, itemWriter)); |
| 167 | step.setTransactionManager(transactionManager); |
| 168 | step.setJobRepository(jobRepository); |
| 169 | step.setStartLimit(startLimit); |
| 170 | step.setAllowStartIfComplete(allowStartIfComplete); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | public Class getObjectType() { |
| 175 | return Step.class; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Returns true by default, but in most cases a {@link Step} should not be |
| 180 | * treated as thread safe. Clients are recommended to create a new step for |
| 181 | * each job execution. |
| 182 | * |
| 183 | * @see org.springframework.beans.factory.FactoryBean#isSingleton() |
| 184 | */ |
| 185 | public boolean isSingleton() { |
| 186 | return this.singleton; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Public setter for the singleton flag. |
| 191 | * @param singleton the value to set. Defaults to true. |
| 192 | */ |
| 193 | public void setSingleton(boolean singleton) { |
| 194 | this.singleton = singleton; |
| 195 | } |
| 196 | |
| 197 | } |