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 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   * Adapt a String to a {@link JobLaunchRequest} consisting of a reference to a
34   * {@link Job} and some {@link JobParameters}. The input String is in the
35   * format: <code>jobname([(key=value(,key=value)*])</code>, where
36   * <ul>
37   * <li>jobname = the name of a {@link Job} to launch</li>
38   * <li>key = the name of a {@link JobParameter}</li>
39   * <li>value = the value of the parameter</li>
40   * </ul>
41   * Job parameter values are optional, and if provided are separated by commas
42   * and enclosed in square brackets. If no parameters are provided the empty set
43   * will be used.
44   * 
45   * @author Dave Syer
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  }