View Javadoc

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.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.core.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<String, Integer> mapping;
41  
42  	public SimpleJvmExitCodeMapper() {
43  		mapping = new HashMap<String, Integer>();
44  		mapping.put(ExitStatus.COMPLETED.getExitCode(), JVM_EXITCODE_COMPLETED);
45  		mapping.put(ExitStatus.FAILED.getExitCode(), JVM_EXITCODE_GENERIC_ERROR);
46  		mapping.put(ExitCodeMapper.JOB_NOT_PROVIDED, JVM_EXITCODE_JOB_ERROR);
47  		mapping.put(ExitCodeMapper.NO_SUCH_JOB, JVM_EXITCODE_JOB_ERROR);
48  	}
49  
50  	public Map<String, Integer> 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<String, Integer> 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  	@Override
71  	public int intValue(String exitCode) {
72  
73  		Integer statusCode = null;
74  
75  		try {
76  			statusCode = mapping.get(exitCode);
77  		}
78  		catch (RuntimeException ex) {
79  			// We still need to return an exit code, even if there is an issue
80  			// with
81  			// the mapper.
82  			logger.fatal("Error mapping exit code, generic exit status returned.", ex);
83  		}
84  
85  		return (statusCode != null) ? statusCode.intValue() : JVM_EXITCODE_GENERIC_ERROR;
86  	}
87  
88  }