| 1 | /* |
| 2 | * Copyright 2006-2014 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.builder; |
| 17 | |
| 18 | import org.apache.commons.logging.Log; |
| 19 | import org.apache.commons.logging.LogFactory; |
| 20 | import org.springframework.batch.core.Step; |
| 21 | import org.springframework.batch.core.StepExecutionListener; |
| 22 | import org.springframework.batch.core.annotation.AfterStep; |
| 23 | import org.springframework.batch.core.annotation.BeforeStep; |
| 24 | import org.springframework.batch.core.listener.StepListenerFactoryBean; |
| 25 | import org.springframework.batch.core.repository.JobRepository; |
| 26 | import org.springframework.batch.core.step.AbstractStep; |
| 27 | import org.springframework.batch.core.step.tasklet.TaskletStep; |
| 28 | import org.springframework.batch.support.ReflectionUtils; |
| 29 | import org.springframework.transaction.PlatformTransactionManager; |
| 30 | |
| 31 | import java.lang.reflect.Method; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.HashSet; |
| 34 | import java.util.List; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | /** |
| 38 | * A base class and utility for other step builders providing access to common properties like job repository and |
| 39 | * transaction manager. |
| 40 | * |
| 41 | * @author Dave Syer |
| 42 | * @author Michael Minella |
| 43 | * |
| 44 | * @since 2.2 |
| 45 | */ |
| 46 | public abstract class StepBuilderHelper<B extends StepBuilderHelper<B>> { |
| 47 | |
| 48 | protected final Log logger = LogFactory.getLog(getClass()); |
| 49 | |
| 50 | protected final CommonStepProperties properties; |
| 51 | |
| 52 | public StepBuilderHelper(String name) { |
| 53 | this.properties = new CommonStepProperties(); |
| 54 | properties.name = name; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used. |
| 59 | * |
| 60 | * @param parent a parent helper containing common step properties |
| 61 | */ |
| 62 | protected StepBuilderHelper(StepBuilderHelper<?> parent) { |
| 63 | this.properties = new CommonStepProperties(parent.properties); |
| 64 | } |
| 65 | |
| 66 | public B repository(JobRepository jobRepository) { |
| 67 | properties.jobRepository = jobRepository; |
| 68 | @SuppressWarnings("unchecked") |
| 69 | B result = (B) this; |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | public B transactionManager(PlatformTransactionManager transactionManager) { |
| 74 | properties.transactionManager = transactionManager; |
| 75 | @SuppressWarnings("unchecked") |
| 76 | B result = (B) this; |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | public B startLimit(int startLimit) { |
| 81 | properties.startLimit = startLimit; |
| 82 | @SuppressWarnings("unchecked") |
| 83 | B result = (B) this; |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Registers objects using the annotation based listener configuration. |
| 89 | * |
| 90 | * @param listener the object that has a method configured with listener annotation |
| 91 | * @return this for fluent chaining |
| 92 | */ |
| 93 | public B listener(Object listener) { |
| 94 | Set<Method> stepExecutionListenerMethods = new HashSet<Method>(); |
| 95 | stepExecutionListenerMethods.addAll(ReflectionUtils.findMethod(listener.getClass(), BeforeStep.class)); |
| 96 | stepExecutionListenerMethods.addAll(ReflectionUtils.findMethod(listener.getClass(), AfterStep.class)); |
| 97 | |
| 98 | if(stepExecutionListenerMethods.size() > 0) { |
| 99 | StepListenerFactoryBean factory = new StepListenerFactoryBean(); |
| 100 | factory.setDelegate(listener); |
| 101 | properties.addStepExecutionListener((StepExecutionListener) factory.getObject()); |
| 102 | } |
| 103 | |
| 104 | @SuppressWarnings("unchecked") |
| 105 | B result = (B) this; |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | public B listener(StepExecutionListener listener) { |
| 110 | properties.addStepExecutionListener(listener); |
| 111 | @SuppressWarnings("unchecked") |
| 112 | B result = (B) this; |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | public B allowStartIfComplete(boolean allowStartIfComplete) { |
| 117 | properties.allowStartIfComplete = allowStartIfComplete; |
| 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 PlatformTransactionManager getTransactionManager() { |
| 132 | return properties.transactionManager; |
| 133 | } |
| 134 | |
| 135 | protected boolean isAllowStartIfComplete() { |
| 136 | return properties.allowStartIfComplete != null ? properties.allowStartIfComplete : false; |
| 137 | } |
| 138 | |
| 139 | protected void enhance(Step target) { |
| 140 | |
| 141 | if (target instanceof AbstractStep) { |
| 142 | |
| 143 | AbstractStep step = (AbstractStep) target; |
| 144 | step.setJobRepository(properties.getJobRepository()); |
| 145 | |
| 146 | Boolean allowStartIfComplete = properties.allowStartIfComplete; |
| 147 | if (allowStartIfComplete != null) { |
| 148 | step.setAllowStartIfComplete(allowStartIfComplete); |
| 149 | } |
| 150 | |
| 151 | step.setStartLimit(properties.startLimit); |
| 152 | |
| 153 | List<StepExecutionListener> listeners = properties.stepExecutionListeners; |
| 154 | if (!listeners.isEmpty()) { |
| 155 | step.setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0])); |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |
| 160 | if (target instanceof TaskletStep) { |
| 161 | TaskletStep step = (TaskletStep) target; |
| 162 | step.setTransactionManager(properties.transactionManager); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | public static class CommonStepProperties { |
| 168 | |
| 169 | private List<StepExecutionListener> stepExecutionListeners = new ArrayList<StepExecutionListener>(); |
| 170 | |
| 171 | private int startLimit = Integer.MAX_VALUE; |
| 172 | |
| 173 | private Boolean allowStartIfComplete; |
| 174 | |
| 175 | private JobRepository jobRepository; |
| 176 | |
| 177 | private PlatformTransactionManager transactionManager; |
| 178 | |
| 179 | public CommonStepProperties() { |
| 180 | } |
| 181 | |
| 182 | public CommonStepProperties(CommonStepProperties properties) { |
| 183 | this.name = properties.name; |
| 184 | this.startLimit = properties.startLimit; |
| 185 | this.allowStartIfComplete = properties.allowStartIfComplete; |
| 186 | this.jobRepository = properties.jobRepository; |
| 187 | this.transactionManager = properties.transactionManager; |
| 188 | this.stepExecutionListeners = new ArrayList<StepExecutionListener>(properties.stepExecutionListeners); |
| 189 | } |
| 190 | |
| 191 | public JobRepository getJobRepository() { |
| 192 | return jobRepository; |
| 193 | } |
| 194 | |
| 195 | public void setJobRepository(JobRepository jobRepository) { |
| 196 | this.jobRepository = jobRepository; |
| 197 | } |
| 198 | |
| 199 | public PlatformTransactionManager getTransactionManager() { |
| 200 | return transactionManager; |
| 201 | } |
| 202 | |
| 203 | public void setTransactionManager(PlatformTransactionManager transactionManager) { |
| 204 | this.transactionManager = transactionManager; |
| 205 | } |
| 206 | |
| 207 | public String getName() { |
| 208 | return name; |
| 209 | } |
| 210 | |
| 211 | public void setName(String name) { |
| 212 | this.name = name; |
| 213 | } |
| 214 | |
| 215 | public List<StepExecutionListener> getStepExecutionListeners() { |
| 216 | return stepExecutionListeners; |
| 217 | } |
| 218 | |
| 219 | public void addStepExecutionListeners(List<StepExecutionListener> stepExecutionListeners) { |
| 220 | this.stepExecutionListeners.addAll(stepExecutionListeners); |
| 221 | } |
| 222 | |
| 223 | public void addStepExecutionListener(StepExecutionListener stepExecutionListener) { |
| 224 | this.stepExecutionListeners.add(stepExecutionListener); |
| 225 | } |
| 226 | |
| 227 | public Integer getStartLimit() { |
| 228 | return startLimit; |
| 229 | } |
| 230 | |
| 231 | public void setStartLimit(Integer startLimit) { |
| 232 | this.startLimit = startLimit; |
| 233 | } |
| 234 | |
| 235 | public Boolean getAllowStartIfComplete() { |
| 236 | return allowStartIfComplete; |
| 237 | } |
| 238 | |
| 239 | public void setAllowStartIfComplete(Boolean allowStartIfComplete) { |
| 240 | this.allowStartIfComplete = allowStartIfComplete; |
| 241 | } |
| 242 | |
| 243 | private String name; |
| 244 | |
| 245 | } |
| 246 | |
| 247 | } |