View Javadoc
1   /*
2    * Copyright 2009-2010 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.admin.integration;
17  
18  import java.io.File;
19  
20  import org.springframework.batch.core.Job;
21  import org.springframework.batch.core.JobParameters;
22  import org.springframework.batch.core.JobParametersBuilder;
23  import org.springframework.batch.core.launch.NoSuchJobException;
24  import org.springframework.batch.integration.launch.JobLaunchRequest;
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.integration.annotation.MessageEndpoint;
27  import org.springframework.integration.annotation.ServiceActivator;
28  import org.springframework.util.Assert;
29  
30  /**
31   * Adapt a {@link File} to a {@link JobLaunchRequest} with a job parameter
32   * <code>input.file</code> equal to the path of the file.
33   * 
34   * @author Dave Syer
35   * 
36   */
37  @MessageEndpoint
38  public class FileToJobLaunchRequestAdapter implements InitializingBean {
39  
40  	private Job job;
41  
42  	public void setJob(Job job) {
43  		this.job = job;
44  	}
45  
46  	public void afterPropertiesSet() throws Exception {
47  		Assert.notNull(job, "A Job must be provided");
48  	}
49  
50  	@ServiceActivator
51  	public JobLaunchRequest adapt(File file) throws NoSuchJobException {
52  
53  		String fileName = file.getAbsolutePath();
54  
55  		if (!fileName.startsWith("/")) {
56  			fileName = "/" + fileName;
57  		}
58  
59  		fileName = "file://" + fileName;
60  
61  		JobParameters jobParameters = new JobParametersBuilder().addString(
62  				"input.file", fileName).toJobParameters();
63  
64  		if (job.getJobParametersIncrementer() != null) {
65  			jobParameters = job.getJobParametersIncrementer().getNext(jobParameters);
66  		}
67  
68  		return new JobLaunchRequest(job, jobParameters);
69  
70  	}
71  
72  }