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.listener; |
17 | |
18 | import org.springframework.batch.core.ExitStatus; |
19 | import org.springframework.batch.core.Job; |
20 | import org.springframework.batch.core.Step; |
21 | import org.springframework.batch.core.StepExecution; |
22 | import org.springframework.batch.item.ExecutionContext; |
23 | import org.springframework.batch.support.PatternMatcher; |
24 | import org.springframework.beans.factory.InitializingBean; |
25 | import 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 | */ |
41 | public 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 | @Override |
50 | public ExitStatus afterStep(StepExecution stepExecution) { |
51 | ExecutionContext stepContext = stepExecution.getExecutionContext(); |
52 | ExecutionContext jobContext = stepExecution.getJobExecution().getExecutionContext(); |
53 | String exitCode = stepExecution.getExitStatus().getExitCode(); |
54 | for (String statusPattern : statuses) { |
55 | if (PatternMatcher.match(statusPattern, exitCode)) { |
56 | for (String key : keys) { |
57 | if (stepContext.containsKey(key)) { |
58 | jobContext.put(key, stepContext.get(key)); |
59 | } else { |
60 | if (strict) { |
61 | throw new IllegalArgumentException("The key [" + key |
62 | + "] was not found in the Step's ExecutionContext."); |
63 | } |
64 | } |
65 | } |
66 | break; |
67 | } |
68 | } |
69 | |
70 | return null; |
71 | } |
72 | |
73 | @Override |
74 | public void afterPropertiesSet() throws Exception { |
75 | Assert.notNull(this.keys, "The 'keys' property must be provided"); |
76 | Assert.notEmpty(this.keys, "The 'keys' property must not be empty"); |
77 | Assert.notNull(this.statuses, "The 'statuses' property must be provided"); |
78 | Assert.notEmpty(this.statuses, "The 'statuses' property must not be empty"); |
79 | } |
80 | |
81 | /** |
82 | * @param keys A list of keys corresponding to items in the {@link Step} |
83 | * {@link ExecutionContext} that must be promoted. |
84 | */ |
85 | public void setKeys(String[] keys) { |
86 | this.keys = keys; |
87 | } |
88 | |
89 | /** |
90 | * @param statuses A list of statuses for which the promotion should occur. |
91 | * Statuses can may contain wildcards recognizable by a |
92 | * {@link PatternMatcher}. |
93 | */ |
94 | public void setStatuses(String[] statuses) { |
95 | this.statuses = statuses; |
96 | } |
97 | |
98 | /** |
99 | * If set to TRUE, the listener will throw an exception if any 'key' is not |
100 | * found in the Step {@link ExecutionContext}. FALSE by default. |
101 | * |
102 | * @param strict |
103 | */ |
104 | public void setStrict(boolean strict) { |
105 | this.strict = strict; |
106 | } |
107 | |
108 | } |