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