EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.core.step.job]

COVERAGE SUMMARY FOR SOURCE FILE [DefaultJobParametersExtractor.java]

nameclass, %method, %block, %line, %
DefaultJobParametersExtractor.java100% (1/1)100% (3/3)100% (280/280)100% (47/47)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefaultJobParametersExtractor100% (1/1)100% (3/3)100% (280/280)100% (47/47)
DefaultJobParametersExtractor (): void 100% (1/1)100% (11/11)100% (3/3)
getJobParameters (Job, StepExecution): JobParameters 100% (1/1)100% (261/261)100% (42/42)
setKeys (String []): void 100% (1/1)100% (8/8)100% (2/2)

1/*
2 * Copyright 2006-2010 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 */
16package org.springframework.batch.core.step.job;
17 
18import java.util.Arrays;
19import java.util.Date;
20import java.util.HashSet;
21import java.util.Map;
22import java.util.Set;
23 
24import org.springframework.batch.core.Job;
25import org.springframework.batch.core.JobParameter;
26import org.springframework.batch.core.JobParameters;
27import org.springframework.batch.core.JobParametersBuilder;
28import org.springframework.batch.core.StepExecution;
29import 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 */
39public 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(StepExecution)
64         */
65        public JobParameters getJobParameters(Job job, StepExecution stepExecution) {
66                JobParametersBuilder builder = new JobParametersBuilder();
67                Map<String, JobParameter> jobParameters = stepExecution.getJobParameters().getParameters();
68                ExecutionContext executionContext = stepExecution.getExecutionContext();
69                if (useAllParentParameters) {
70                        for (String key : jobParameters.keySet()) {
71                                builder.addParameter(key, jobParameters.get(key));
72                        }
73                }
74                for (String key : keys) {
75                        if (key.endsWith("(long)")) {
76                                key = key.replace("(long)", "");
77                                if (executionContext.containsKey(key)) {
78                                        builder.addLong(key, executionContext.getLong(key));
79                                }
80                                else if (jobParameters.containsKey(key)) {
81                                        builder.addLong(key, (Long) jobParameters.get(key).getValue());
82                                }
83                        }
84                        else if (key.endsWith("(int)")) {
85                                key = key.replace("(int)", "");
86                                if (executionContext.containsKey(key)) {
87                                        builder.addLong(key, (long) executionContext.getInt(key));
88                                }
89                                else if (jobParameters.containsKey(key)) {
90                                        builder.addLong(key, (Long) jobParameters.get(key).getValue());
91                                }
92                        }
93                        else if (key.endsWith("(double)")) {
94                                key = key.replace("(double)", "");
95                                if (executionContext.containsKey(key)) {
96                                        builder.addDouble(key, executionContext.getDouble(key));
97                                }
98                                else if (jobParameters.containsKey(key)) {
99                                        builder.addDouble(key, (Double) jobParameters.get(key).getValue());
100                                }
101                        }
102                        else if (key.endsWith("(string)")) {
103                                key = key.replace("(string)", "");
104                                if (executionContext.containsKey(key)) {
105                                        builder.addString(key, executionContext.getString(key));
106                                }
107                                else if (jobParameters.containsKey(key)) {
108                                        builder.addString(key, (String) jobParameters.get(key).getValue());
109                                }
110                        }
111                        else if (key.endsWith("(date)")) {
112                                key = key.replace("(date)", "");
113                                if (executionContext.containsKey(key)) {
114                                        builder.addDate(key, (Date) executionContext.get(key));
115                                }
116                                else if (jobParameters.containsKey(key)) {
117                                        builder.addDate(key, (Date) jobParameters.get(key).getValue());
118                                }
119                        }
120                        else {
121                                if (executionContext.containsKey(key)) {
122                                        builder.addString(key, executionContext.get(key).toString());
123                                }
124                                else if (jobParameters.containsKey(key)) {
125                                        builder.addString(key, jobParameters.get(key).getValue().toString());
126                                }
127                        }
128                }
129                return builder.toJobParameters();
130        }
131 
132}

[all classes][org.springframework.batch.core.step.job]
EMMA 2.0.5312 (C) Vladimir Roubtsov