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.step.builder;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.springframework.batch.core.Step;
24  import org.springframework.batch.core.StepExecutionListener;
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.transaction.PlatformTransactionManager;
29  
30  /**
31   * A base class and utility for other step builders providing access to common properties like job repository and
32   * transaction manager.
33   * 
34   * @author Dave Syer
35   * 
36   * @since 2.2
37   */
38  public abstract class StepBuilderHelper<B extends StepBuilderHelper<B>> {
39  
40  	protected final Log logger = LogFactory.getLog(getClass());
41  
42  	private final CommonStepProperties properties;
43  
44  	public StepBuilderHelper(String name) {
45  		this.properties = new CommonStepProperties();
46  		properties.name = name;
47  	}
48  
49  	/**
50  	 * Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used.
51  	 * 
52  	 * @param parent a parent helper containing common step properties
53  	 */
54  	protected StepBuilderHelper(StepBuilderHelper<?> parent) {
55  		this.properties = new CommonStepProperties(parent.properties);
56  	}
57  
58  	public B repository(JobRepository jobRepository) {
59  		properties.jobRepository = jobRepository;
60  		@SuppressWarnings("unchecked")
61  		B result = (B) this;
62  		return result;
63  	}
64  
65  	public B transactionManager(PlatformTransactionManager transactionManager) {
66  		properties.transactionManager = transactionManager;
67  		@SuppressWarnings("unchecked")
68  		B result = (B) this;
69  		return result;
70  	}
71  
72  	public B startLimit(int startLimit) {
73  		properties.startLimit = startLimit;
74  		@SuppressWarnings("unchecked")
75  		B result = (B) this;
76  		return result;
77  	}
78  
79  	public B listener(StepExecutionListener listener) {
80  		properties.addStepExecutionListener(listener);
81  		@SuppressWarnings("unchecked")
82  		B result = (B) this;
83  		return result;
84  	}
85  
86  	public B allowStartIfComplete(boolean allowStartIfComplete) {
87  		properties.allowStartIfComplete = allowStartIfComplete;
88  		@SuppressWarnings("unchecked")
89  		B result = (B) this;
90  		return result;
91  	}
92  
93  	protected String getName() {
94  		return properties.name;
95  	}
96  
97  	protected JobRepository getJobRepository() {
98  		return properties.jobRepository;
99  	}
100 
101 	protected PlatformTransactionManager getTransactionManager() {
102 		return properties.transactionManager;
103 	}
104 
105 	protected boolean isAllowStartIfComplete() {
106 		return properties.allowStartIfComplete != null ? properties.allowStartIfComplete : false;
107 	}
108 
109 	protected void enhance(Step target) {
110 
111 		if (target instanceof AbstractStep) {
112 
113 			AbstractStep step = (AbstractStep) target;
114 			step.setJobRepository(properties.getJobRepository());
115 
116 			Boolean allowStartIfComplete = properties.allowStartIfComplete;
117 			if (allowStartIfComplete != null) {
118 				step.setAllowStartIfComplete(allowStartIfComplete);
119 			}
120 
121 			step.setStartLimit(properties.startLimit);
122 
123 			List<StepExecutionListener> listeners = properties.stepExecutionListeners;
124 			if (!listeners.isEmpty()) {
125 				step.setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0]));
126 			}
127 
128 		}
129 
130 		if (target instanceof TaskletStep) {
131 			TaskletStep step = (TaskletStep) target;
132 			step.setTransactionManager(properties.transactionManager);
133 		}
134 
135 	}
136 
137 	public static class CommonStepProperties {
138 
139 		private List<StepExecutionListener> stepExecutionListeners = new ArrayList<StepExecutionListener>();
140 
141 		private int startLimit = Integer.MAX_VALUE;
142 
143 		private Boolean allowStartIfComplete;
144 
145 		private JobRepository jobRepository;
146 
147 		private PlatformTransactionManager transactionManager;
148 
149 		public CommonStepProperties() {
150 		}
151 
152 		public CommonStepProperties(CommonStepProperties properties) {
153 			this.name = properties.name;
154 			this.startLimit = properties.startLimit;
155 			this.allowStartIfComplete = properties.allowStartIfComplete;
156 			this.jobRepository = properties.jobRepository;
157 			this.transactionManager = properties.transactionManager;
158 		}
159 
160 		public JobRepository getJobRepository() {
161 			return jobRepository;
162 		}
163 
164 		public void setJobRepository(JobRepository jobRepository) {
165 			this.jobRepository = jobRepository;
166 		}
167 
168 		public PlatformTransactionManager getTransactionManager() {
169 			return transactionManager;
170 		}
171 
172 		public void setTransactionManager(PlatformTransactionManager transactionManager) {
173 			this.transactionManager = transactionManager;
174 		}
175 
176 		public String getName() {
177 			return name;
178 		}
179 
180 		public void setName(String name) {
181 			this.name = name;
182 		}
183 
184 		public List<StepExecutionListener> getStepExecutionListeners() {
185 			return stepExecutionListeners;
186 		}
187 
188 		public void addStepExecutionListeners(List<StepExecutionListener> stepExecutionListeners) {
189 			this.stepExecutionListeners.addAll(stepExecutionListeners);
190 		}
191 
192 		public void addStepExecutionListener(StepExecutionListener stepExecutionListener) {
193 			this.stepExecutionListeners.add(stepExecutionListener);
194 		}
195 
196 		public Integer getStartLimit() {
197 			return startLimit;
198 		}
199 
200 		public void setStartLimit(Integer startLimit) {
201 			this.startLimit = startLimit;
202 		}
203 
204 		public Boolean getAllowStartIfComplete() {
205 			return allowStartIfComplete;
206 		}
207 
208 		public void setAllowStartIfComplete(Boolean allowStartIfComplete) {
209 			this.allowStartIfComplete = allowStartIfComplete;
210 		}
211 
212 		private String name;
213 
214 	}
215 
216 }