View Javadoc

1   /*
2    * Copyright 2012-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.Arrays;
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  import org.springframework.batch.core.JobParameters;
24  import org.springframework.batch.core.JobParametersInvalidException;
25  import org.springframework.batch.core.JobParametersValidator;
26  import org.springframework.beans.factory.InitializingBean;
27  import org.springframework.util.Assert;
28  
29  /**
30   * Default implementation of {@link JobParametersValidator}.
31   *
32   * @author Dave Syer
33   *
34   */
35  public class DefaultJobParametersValidator implements JobParametersValidator, InitializingBean {
36  
37  	private Collection<String> requiredKeys;
38  
39  	private Collection<String> optionalKeys;
40  
41  	/**
42  	 * Convenient default constructor for unconstrained validation.
43  	 */
44  	public DefaultJobParametersValidator() {
45  		this(new String[0], new String[0]);
46  	}
47  
48  	/**
49  	 * Create a new validator with the required and optional job parameter keys
50  	 * provided.
51  	 *
52  	 * @see DefaultJobParametersValidator#setOptionalKeys(String[])
53  	 * @see DefaultJobParametersValidator#setRequiredKeys(String[])
54  	 *
55  	 * @param requiredKeys the required keys
56  	 * @param optionalKeys the optional keys
57  	 */
58  	public DefaultJobParametersValidator(String[] requiredKeys, String[] optionalKeys) {
59  		super();
60  		setRequiredKeys(requiredKeys);
61  		setOptionalKeys(optionalKeys);
62  	}
63  
64  	/**
65  	 * Check that there are no overlaps between required and optional keys.
66  	 * @throws IllegalStateException if there is an overlap
67  	 */
68  	@Override
69  	public void afterPropertiesSet() throws IllegalStateException {
70  		for (String key : requiredKeys) {
71  			Assert.state(!optionalKeys.contains(key), "Optional keys canot be required: " + key);
72  		}
73  	}
74  
75  	/**
76  	 * Check the parameters meet the specification provided. If optional keys
77  	 * are explicitly specified then all keys must be in that list, or in the
78  	 * required list. Otherwise all keys that are specified as required must be
79  	 * present.
80  	 *
81  	 * @see JobParametersValidator#validate(JobParameters)
82  	 *
83  	 * @throws JobParametersInvalidException if the parameters are not valid
84  	 */
85  	@Override
86  	public void validate(JobParameters parameters) throws JobParametersInvalidException {
87  
88  		if (parameters == null) {
89  			throw new JobParametersInvalidException("The JobParameters can not be null");
90  		}
91  
92  		Set<String> keys = parameters.getParameters().keySet();
93  
94  		// If there are explicit optional keys then all keys must be in that
95  		// group, or in the required group.
96  		if (!optionalKeys.isEmpty()) {
97  
98  			Collection<String> missingKeys = new HashSet<String>();
99  			for (String key : keys) {
100 				if (!optionalKeys.contains(key) && !requiredKeys.contains(key)) {
101 					missingKeys.add(key);
102 				}
103 			}
104 			if (!missingKeys.isEmpty()) {
105 				throw new JobParametersInvalidException(
106 						"The JobParameters contains keys that are not explicitly optional or required: " + missingKeys);
107 			}
108 
109 		}
110 
111 		Collection<String> missingKeys = new HashSet<String>();
112 		for (String key : requiredKeys) {
113 			if (!keys.contains(key)) {
114 				missingKeys.add(key);
115 			}
116 		}
117 		if (!missingKeys.isEmpty()) {
118 			throw new JobParametersInvalidException("The JobParameters do not contain required keys: " + missingKeys);
119 		}
120 
121 	}
122 
123 	/**
124 	 * The keys that are required in the parameters. The default is empty,
125 	 * meaning that all parameters are optional, unless optional keys are
126 	 * explicitly specified.
127 	 *
128 	 * @param requiredKeys the required key values
129 	 *
130 	 * @see #setOptionalKeys(String[])
131 	 */
132 	public final void setRequiredKeys(String[] requiredKeys) {
133 		this.requiredKeys = new HashSet<String>(Arrays.asList(requiredKeys));
134 	}
135 
136 	/**
137 	 * The keys that are optional in the parameters. If any keys are explicitly
138 	 * optional, then to be valid all other keys must be explicitly required.
139 	 * The default is empty, meaning that all parameters that are not required
140 	 * are optional.
141 	 *
142 	 * @param optionalKeys the optional key values
143 	 *
144 	 * @see #setRequiredKeys(String[])
145 	 */
146 	public final void setOptionalKeys(String[] optionalKeys) {
147 		this.optionalKeys = new HashSet<String>(Arrays.asList(optionalKeys));
148 	}
149 
150 }