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
17 package org.springframework.batch.core.step.tasklet;
18
19 import java.util.Map;
20
21 import org.springframework.batch.core.ExitStatus;
22 import org.springframework.util.Assert;
23
24 /**
25 * Maps exit codes to {@link org.springframework.batch.core.ExitStatus}
26 * according to injected map. The injected map is required to contain a value
27 * for 'else' key, this value will be returned if the injected map
28 * does not contain value for the exit code returned by the system process.
29 *
30 * @author Robert Kasanicky
31 */
32 public class ConfigurableSystemProcessExitCodeMapper implements SystemProcessExitCodeMapper {
33
34 public static final String ELSE_KEY = "else";
35
36 private Map<Object, ExitStatus> mappings;
37
38 @Override
39 public ExitStatus getExitStatus(int exitCode) {
40 ExitStatus exitStatus = mappings.get(exitCode);
41 if (exitStatus != null) {
42 return exitStatus;
43 } else {
44 return mappings.get(ELSE_KEY);
45 }
46 }
47
48 /**
49 * @param mappings <code>Integer</code> exit code keys to
50 * {@link org.springframework.batch.core.ExitStatus} values.
51 */
52 public void setMappings(Map<Object, ExitStatus> mappings) {
53 Assert.notNull(mappings.get(ELSE_KEY));
54 this.mappings = mappings;
55 }
56
57 }