| 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 | */ |
| 16 | |
| 17 | package org.springframework.batch.core.launch.support; |
| 18 | |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import org.apache.commons.logging.Log; |
| 23 | import org.apache.commons.logging.LogFactory; |
| 24 | import org.springframework.batch.repeat.ExitStatus; |
| 25 | |
| 26 | /** |
| 27 | * An implementation of {@link ExitCodeMapper} that can be configured through a |
| 28 | * map from batch exit codes (String) to integer results. Some default entries |
| 29 | * are set up to recognise common cases. Any that are injected are added to these. |
| 30 | * |
| 31 | * @author Stijn Maller |
| 32 | * @author Lucas Ward |
| 33 | * @author Dave Syer |
| 34 | */ |
| 35 | |
| 36 | public class SimpleJvmExitCodeMapper implements ExitCodeMapper { |
| 37 | |
| 38 | protected Log logger = LogFactory.getLog(getClass()); |
| 39 | |
| 40 | private Map mapping; |
| 41 | |
| 42 | public SimpleJvmExitCodeMapper() { |
| 43 | mapping = new HashMap(); |
| 44 | mapping.put(ExitStatus.FINISHED.getExitCode(), new Integer(JVM_EXITCODE_COMPLETED)); |
| 45 | mapping.put(ExitStatus.FAILED.getExitCode(), new Integer(JVM_EXITCODE_GENERIC_ERROR)); |
| 46 | mapping.put(ExitCodeMapper.JOB_NOT_PROVIDED, new Integer(JVM_EXITCODE_JOB_ERROR)); |
| 47 | mapping.put(ExitCodeMapper.NO_SUCH_JOB, new Integer(JVM_EXITCODE_JOB_ERROR)); |
| 48 | } |
| 49 | |
| 50 | public Map getMapping() { |
| 51 | return mapping; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Supply the ExitCodeMappings |
| 56 | * @param exitCodeMap A set of mappings between environment specific exit |
| 57 | * codes and batch framework internal exit codes |
| 58 | */ |
| 59 | public void setMapping(Map exitCodeMap) { |
| 60 | mapping.putAll(exitCodeMap); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the operating system exit status that matches a certain Batch |
| 65 | * Framework Exitcode |
| 66 | * @param exitCode The exitcode of the Batch Job as known by the Batch |
| 67 | * Framework |
| 68 | * @return The exitCode of the Batch Job as known by the JVM |
| 69 | */ |
| 70 | public int intValue(String exitCode) { |
| 71 | |
| 72 | Integer statusCode = null; |
| 73 | |
| 74 | try { |
| 75 | statusCode = (Integer) mapping.get(exitCode); |
| 76 | } |
| 77 | catch (RuntimeException ex) { |
| 78 | // We still need to return an exit code, even if there is an issue |
| 79 | // with |
| 80 | // the mapper. |
| 81 | logger.fatal("Error mapping exit code, generic exit status returned.", ex); |
| 82 | } |
| 83 | |
| 84 | return (statusCode != null) ? statusCode.intValue() : JVM_EXITCODE_GENERIC_ERROR; |
| 85 | } |
| 86 | |
| 87 | } |