View Javadoc

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  package org.springframework.batch.sample.common;
17  
18  import org.apache.commons.io.FilenameUtils;
19  import org.springframework.batch.core.StepExecution;
20  import org.springframework.batch.core.annotation.BeforeStep;
21  import org.springframework.batch.item.ExecutionContext;
22  
23  /**
24   * @author Dave Syer
25   * 
26   */
27  public class OutputFileListener {
28  
29  	private String outputKeyName = "outputFile";
30  
31  	private String inputKeyName = "fileName";
32  
33  	private String path = "file:./target/output/";
34  	
35  	public void setPath(String path) {
36  		this.path = path;
37  	}
38  
39  	public void setOutputKeyName(String outputKeyName) {
40  		this.outputKeyName = outputKeyName;
41  	}
42  
43  	public void setInputKeyName(String inputKeyName) {
44  		this.inputKeyName = inputKeyName;
45  	}
46  
47  	@BeforeStep
48  	public void createOutputNameFromInput(StepExecution stepExecution) {
49  		ExecutionContext executionContext = stepExecution.getExecutionContext();
50  		String inputName = stepExecution.getStepName().replace(":", "-");
51  		if (executionContext.containsKey(inputKeyName)) {
52  			inputName = executionContext.getString(inputKeyName);
53  		}
54  		if (!executionContext.containsKey(outputKeyName)) {
55  			executionContext.putString(outputKeyName, path + FilenameUtils.getBaseName(inputName)
56  					+ ".csv");
57  		}
58  	}
59  
60  }