1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
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 }