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 org.springframework.batch.core.Job;
19 import org.springframework.batch.core.JobParameter;
20 import org.springframework.batch.core.JobParameters;
21 import org.springframework.batch.core.configuration.JobLocator;
22 import org.springframework.batch.core.converter.DefaultJobParametersConverter;
23 import org.springframework.batch.core.converter.JobParametersConverter;
24 import org.springframework.batch.core.launch.NoSuchJobException;
25 import org.springframework.batch.integration.launch.JobLaunchRequest;
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.integration.annotation.MessageEndpoint;
28 import org.springframework.integration.annotation.ServiceActivator;
29 import org.springframework.util.Assert;
30 import org.springframework.util.StringUtils;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 @MessageEndpoint
49 public class StringToJobLaunchRequestAdapter implements InitializingBean {
50
51 private JobLocator jobLocator;
52
53 private JobParametersConverter converter = new DefaultJobParametersConverter();
54
55 private static String PATTERN = "([\\w-_]*)(\\[(.*)\\]|.*)";
56
57 public void setJobLocator(JobLocator jobLocator) {
58 this.jobLocator = jobLocator;
59 }
60
61 public void afterPropertiesSet() throws Exception {
62 Assert.state(jobLocator != null, "A JobLocator must be provided");
63 }
64
65 @ServiceActivator
66 public JobLaunchRequest adapt(String request) throws NoSuchJobException {
67 request = request.trim();
68 Assert.isTrue(request.matches(PATTERN), "Input in wrong format ("
69 + request + "): use jobname([(key=value(,key=value)*])");
70 String jobName = request.replaceAll(PATTERN, "$1");
71 Job job = jobLocator.getJob(jobName);
72 String paramsText = request.replaceAll(PATTERN, "$3");
73 JobParameters jobParameters = converter.getJobParameters(StringUtils
74 .splitArrayElementsIntoProperties(paramsText.split(","), "="));
75 return new JobLaunchRequest(job, jobParameters);
76 }
77 }