001/*
002 * Copyright 2006-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.core.step.tasklet;
018
019import java.util.Map;
020
021import org.springframework.batch.core.ExitStatus;
022import org.springframework.util.Assert;
023
024/**
025 * Maps exit codes to {@link org.springframework.batch.core.ExitStatus}
026 * according to injected map. The injected map is required to contain a value
027 * for 'else' key, this value will be returned if the injected map
028 * does not contain value for the exit code returned by the system process.
029 *
030 * @author Robert Kasanicky
031 */
032public class ConfigurableSystemProcessExitCodeMapper implements SystemProcessExitCodeMapper {
033
034        public static final String ELSE_KEY = "else";
035
036        private Map<Object, ExitStatus> mappings;
037
038    @Override
039        public ExitStatus getExitStatus(int exitCode) {
040                ExitStatus exitStatus = mappings.get(exitCode);
041                if (exitStatus != null) {
042                        return exitStatus;
043                } else {
044                        return mappings.get(ELSE_KEY);
045                }
046        }
047
048        /**
049         * @param mappings <code>Integer</code> exit code keys to
050         * {@link org.springframework.batch.core.ExitStatus} values.
051         */
052        public void setMappings(Map<Object, ExitStatus> mappings) {
053                Assert.notNull(mappings.get(ELSE_KEY), "Missing value for " + ELSE_KEY);
054                this.mappings = mappings;
055        }
056
057}