EMMA Coverage Report (generated Fri Aug 21 15:59:46 BST 2009)
[all classes][org.springframework.batch.core.listener]

COVERAGE SUMMARY FOR SOURCE FILE [ExecutionContextPromotionListener.java]

nameclass, %method, %block, %line, %
ExecutionContextPromotionListener.java100% (1/1)100% (6/6)100% (121/121)100% (27/27)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ExecutionContextPromotionListener100% (1/1)100% (6/6)100% (121/121)100% (27/27)
ExecutionContextPromotionListener (): void 100% (1/1)100% (18/18)100% (4/4)
afterPropertiesSet (): void 100% (1/1)100% (17/17)100% (5/5)
afterStep (StepExecution): ExitStatus 100% (1/1)100% (74/74)100% (12/12)
setKeys (String []): void 100% (1/1)100% (4/4)100% (2/2)
setStatuses (String []): void 100% (1/1)100% (4/4)100% (2/2)
setStrict (boolean): void 100% (1/1)100% (4/4)100% (2/2)

1/*
2 * Copyright 2006-2007 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.listener;
17 
18import org.springframework.batch.core.ExitStatus;
19import org.springframework.batch.core.Job;
20import org.springframework.batch.core.Step;
21import org.springframework.batch.core.StepExecution;
22import org.springframework.batch.item.ExecutionContext;
23import org.springframework.batch.support.PatternMatcher;
24import org.springframework.beans.factory.InitializingBean;
25import org.springframework.util.Assert;
26 
27/**
28 * This class can be used to automatically promote items from the {@link Step}
29 * {@link ExecutionContext} to the {@link Job} {@link ExecutionContext} at the
30 * end of a step. A list of keys should be provided that correspond to the items
31 * in the {@link Step} {@link ExecutionContext} that should be promoted.
32 * 
33 * Additionally, an optional list of statuses can be set to indicate for which
34 * exit status codes the promotion should occur. These statuses will be checked
35 * using the {@link PatternMatcher}, so wildcards are allowed. By default,
36 * promotion will only occur for steps with an exit code of "COMPLETED".
37 * 
38 * @author Dan Garrette
39 * @since 2.0
40 */
41public class ExecutionContextPromotionListener extends StepExecutionListenerSupport implements InitializingBean {
42 
43        private String[] keys = null;
44 
45        private String[] statuses = new String[] { ExitStatus.COMPLETED.getExitCode() };
46 
47        private boolean strict = false;
48 
49        public ExitStatus afterStep(StepExecution stepExecution) {
50                ExecutionContext stepContext = stepExecution.getExecutionContext();
51                ExecutionContext jobContext = stepExecution.getJobExecution().getExecutionContext();
52                String exitCode = stepExecution.getExitStatus().getExitCode();
53                for (String statusPattern : statuses) {
54                        if (PatternMatcher.match(statusPattern, exitCode)) {
55                                for (String key : keys) {
56                                        if (strict) {
57                                                Assert.isTrue(stepContext.containsKey(key), "The key [" + key
58                                                                + "] was not found in the Step's ExecutionContext.");
59                                        }
60                                        jobContext.put(key, stepContext.get(key));
61                                }
62                                break;
63                        }
64                }
65 
66                return null;
67        }
68 
69        public void afterPropertiesSet() throws Exception {
70                Assert.notNull(this.keys, "The 'keys' property must be provided");
71                Assert.notEmpty(this.keys, "The 'keys' property must not be empty");
72                Assert.notNull(this.statuses, "The 'statuses' property must be provided");
73                Assert.notEmpty(this.statuses, "The 'statuses' property must not be empty");
74        }
75 
76        /**
77         * @param keys A list of keys corresponding to items in the {@link Step}
78         * {@link ExecutionContext} that must be promoted.
79         */
80        public void setKeys(String[] keys) {
81                this.keys = keys;
82        }
83 
84        /**
85         * @param statuses A list of statuses for which the promotion should occur.
86         * Statuses can may contain wildcards recognizable by a
87         * {@link PatternMatcher}.
88         */
89        public void setStatuses(String[] statuses) {
90                this.statuses = statuses;
91        }
92 
93        /**
94         * If set to TRUE, the listener will throw an exception if any 'key' is not
95         * found in the Step {@link ExecutionContext}. FALSE by default.
96         * 
97         * @param strict
98         */
99        public void setStrict(boolean strict) {
100                this.strict = strict;
101        }
102 
103}

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