View Javadoc

1   /*
2    * Copyright 2011-2013 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;
17  
18  import java.util.List;
19  
20  import org.springframework.batch.core.JobParameters;
21  import org.springframework.batch.core.JobParametersInvalidException;
22  import org.springframework.batch.core.JobParametersValidator;
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.util.Assert;
25  
26  /**
27   * Composite {@link JobParametersValidator} that passes the job parameters through a sequence of
28   * injected <code>JobParametersValidator</code>s
29   *
30   * @author Morten Andersen-Gott
31   *
32   */
33  public class CompositeJobParametersValidator implements JobParametersValidator, InitializingBean {
34  
35  	private List<JobParametersValidator> validators;
36  
37  	/**
38  	 * Validates the JobParameters according to the injected JobParameterValidators
39  	 * Validation stops and exception is thrown on first validation error
40  	 *
41  	 * @param parameters some {@link JobParameters}
42  	 * @throws JobParametersInvalidException if the parameters are invalid
43  	 */
44  	@Override
45  	public void validate(JobParameters parameters)	throws JobParametersInvalidException {
46  		for (JobParametersValidator validator : validators) {
47  			validator.validate(parameters);
48  		}
49  	}
50  
51  	/**
52  	 * Public setter for the validators
53  	 * @param validators
54  	 */
55  	public void setValidators(List<JobParametersValidator> validators) {
56  		this.validators = validators;
57  	}
58  
59  	@Override
60  	public void afterPropertiesSet() throws Exception {
61  		Assert.notNull(validators, "The 'validators' may not be null");
62  		Assert.notEmpty(validators, "The 'validators' may not be empty");
63  	}
64  
65  
66  
67  }