| 1 | /* |
| 2 | * Copyright 2006-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.step.job; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Date; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import org.springframework.batch.core.Job; |
| 25 | import org.springframework.batch.core.JobParameter; |
| 26 | import org.springframework.batch.core.JobParameters; |
| 27 | import org.springframework.batch.core.JobParametersBuilder; |
| 28 | import org.springframework.batch.core.StepExecution; |
| 29 | import org.springframework.batch.item.ExecutionContext; |
| 30 | |
| 31 | /** |
| 32 | * Simple implementation of {@link JobParametersExtractor} which pulls |
| 33 | * parameters with named keys out of the step execution context and the job |
| 34 | * parameters of the surrounding job. |
| 35 | * |
| 36 | * @author Dave Syer |
| 37 | * |
| 38 | */ |
| 39 | public class DefaultJobParametersExtractor implements JobParametersExtractor { |
| 40 | |
| 41 | private Set<String> keys = new HashSet<String>(); |
| 42 | |
| 43 | private boolean useAllParentParameters = true; |
| 44 | |
| 45 | /** |
| 46 | * The key names to pull out of the execution context or job parameters, if |
| 47 | * they exist. If a key doesn't exist in the execution context then the job |
| 48 | * parameters from the enclosing job execution are tried, and if there is |
| 49 | * nothing there either then no parameter is extracted. Key names ending |
| 50 | * with <code>(long)</code>, <code>(int)</code>, <code>(double)</code>, |
| 51 | * <code>(date)</code> or <code>(string)</code> will be assumed to refer to |
| 52 | * values of the respective type and assigned to job parameters accordingly |
| 53 | * (there will be an error if they are not of the right type). Without a |
| 54 | * special suffix in that form a parameter is assumed to be of type String. |
| 55 | * |
| 56 | * @param keys the keys to set |
| 57 | */ |
| 58 | public void setKeys(String[] keys) { |
| 59 | this.keys = new HashSet<String>(Arrays.asList(keys)); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @see JobParametersExtractor#getJobParameters(Job, StepExecution) |
| 64 | */ |
| 65 | @Override |
| 66 | public JobParameters getJobParameters(Job job, StepExecution stepExecution) { |
| 67 | JobParametersBuilder builder = new JobParametersBuilder(); |
| 68 | Map<String, JobParameter> jobParameters = stepExecution.getJobParameters().getParameters(); |
| 69 | ExecutionContext executionContext = stepExecution.getExecutionContext(); |
| 70 | if (useAllParentParameters) { |
| 71 | for (String key : jobParameters.keySet()) { |
| 72 | builder.addParameter(key, jobParameters.get(key)); |
| 73 | } |
| 74 | } |
| 75 | for (String key : keys) { |
| 76 | if (key.endsWith("(long)")) { |
| 77 | key = key.replace("(long)", ""); |
| 78 | if (executionContext.containsKey(key)) { |
| 79 | builder.addLong(key, executionContext.getLong(key)); |
| 80 | } |
| 81 | else if (jobParameters.containsKey(key)) { |
| 82 | builder.addLong(key, (Long) jobParameters.get(key).getValue()); |
| 83 | } |
| 84 | } |
| 85 | else if (key.endsWith("(int)")) { |
| 86 | key = key.replace("(int)", ""); |
| 87 | if (executionContext.containsKey(key)) { |
| 88 | builder.addLong(key, (long) executionContext.getInt(key)); |
| 89 | } |
| 90 | else if (jobParameters.containsKey(key)) { |
| 91 | builder.addLong(key, (Long) jobParameters.get(key).getValue()); |
| 92 | } |
| 93 | } |
| 94 | else if (key.endsWith("(double)")) { |
| 95 | key = key.replace("(double)", ""); |
| 96 | if (executionContext.containsKey(key)) { |
| 97 | builder.addDouble(key, executionContext.getDouble(key)); |
| 98 | } |
| 99 | else if (jobParameters.containsKey(key)) { |
| 100 | builder.addDouble(key, (Double) jobParameters.get(key).getValue()); |
| 101 | } |
| 102 | } |
| 103 | else if (key.endsWith("(string)")) { |
| 104 | key = key.replace("(string)", ""); |
| 105 | if (executionContext.containsKey(key)) { |
| 106 | builder.addString(key, executionContext.getString(key)); |
| 107 | } |
| 108 | else if (jobParameters.containsKey(key)) { |
| 109 | builder.addString(key, (String) jobParameters.get(key).getValue()); |
| 110 | } |
| 111 | } |
| 112 | else if (key.endsWith("(date)")) { |
| 113 | key = key.replace("(date)", ""); |
| 114 | if (executionContext.containsKey(key)) { |
| 115 | builder.addDate(key, (Date) executionContext.get(key)); |
| 116 | } |
| 117 | else if (jobParameters.containsKey(key)) { |
| 118 | builder.addDate(key, (Date) jobParameters.get(key).getValue()); |
| 119 | } |
| 120 | } |
| 121 | else { |
| 122 | if (executionContext.containsKey(key)) { |
| 123 | builder.addString(key, executionContext.get(key).toString()); |
| 124 | } |
| 125 | else if (jobParameters.containsKey(key)) { |
| 126 | builder.addString(key, jobParameters.get(key).getValue().toString()); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | return builder.toJobParameters(); |
| 131 | } |
| 132 | |
| 133 | } |