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.launch.support;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.springframework.batch.core.ExitStatus;
025
026/**
027 * An implementation of {@link ExitCodeMapper} that can be configured through a
028 * map from batch exit codes (String) to integer results. Some default entries
029 * are set up to recognise common cases.  Any that are injected are added to these.
030 *
031 * @author Stijn Maller
032 * @author Lucas Ward
033 * @author Dave Syer
034 */
035
036public class SimpleJvmExitCodeMapper implements ExitCodeMapper {
037
038        protected Log logger = LogFactory.getLog(getClass());
039
040        private Map<String, Integer> mapping;
041
042        public SimpleJvmExitCodeMapper() {
043                mapping = new HashMap<String, Integer>();
044                mapping.put(ExitStatus.COMPLETED.getExitCode(), JVM_EXITCODE_COMPLETED);
045                mapping.put(ExitStatus.FAILED.getExitCode(), JVM_EXITCODE_GENERIC_ERROR);
046                mapping.put(ExitCodeMapper.JOB_NOT_PROVIDED, JVM_EXITCODE_JOB_ERROR);
047                mapping.put(ExitCodeMapper.NO_SUCH_JOB, JVM_EXITCODE_JOB_ERROR);
048        }
049
050        public Map<String, Integer> getMapping() {
051                return mapping;
052        }
053
054        /**
055         * Supply the ExitCodeMappings
056         * @param exitCodeMap A set of mappings between environment specific exit
057         * codes and batch framework internal exit codes
058         */
059        public void setMapping(Map<String, Integer> exitCodeMap) {
060                mapping.putAll(exitCodeMap);
061        }
062
063        /**
064         * Get the operating system exit status that matches a certain Batch
065         * Framework exit code
066         * @param exitCode The exit code of the Batch Job as known by the Batch
067         * Framework
068         * @return The exitCode of the Batch Job as known by the JVM
069         */
070        @Override
071        public int intValue(String exitCode) {
072
073                Integer statusCode = null;
074
075                try {
076                        statusCode = mapping.get(exitCode);
077                }
078                catch (RuntimeException ex) {
079                        // We still need to return an exit code, even if there is an issue
080                        // with
081                        // the mapper.
082                        logger.fatal("Error mapping exit code, generic exit status returned.", ex);
083                }
084
085                return (statusCode != null) ? statusCode.intValue() : JVM_EXITCODE_GENERIC_ERROR;
086        }
087
088}