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.util.HashMap;
19  import java.util.Map;
20  
21  import org.springframework.batch.admin.service.JobService;
22  import org.springframework.batch.core.JobParameter;
23  import org.springframework.batch.core.JobParameters;
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.Transformer;
29  import org.springframework.util.Assert;
30  
31  /**
32   * Adapt a {@link JobLaunchRequest} so that it picks up the job parameters from
33   * the last execution if possible.
34   * 
35   * @author Dave Syer
36   * 
37   */
38  @MessageEndpoint
39  public class LastJobParametersJobLaunchRequestEnhancer implements InitializingBean {
40  
41  	private JobService jobService;
42  
43  	public void setJobService(JobService jobService) {
44  		this.jobService = jobService;
45  	}
46  
47  	public void afterPropertiesSet() throws Exception {
48  		Assert.notNull(jobService, "A JobService must be provided");
49  	}
50  
51  	@Transformer
52  	public JobLaunchRequest adapt(JobLaunchRequest request) throws NoSuchJobException {
53  
54  		Map<String, JobParameter> jobParameters = request.getJobParameters().getParameters();
55  		Map<String, JobParameter> oldParameters = jobService.getLastJobParameters(request.getJob().getName())
56  				.getParameters();
57  
58  		Map<String, JobParameter> map = new HashMap<String, JobParameter>();
59  
60  		for (String key : oldParameters.keySet()) {
61  			map.put(key, oldParameters.get(key));
62  		}
63  		for (String key : jobParameters.keySet()) {
64  			map.put(key, jobParameters.get(key));
65  		}
66  
67  		return new JobLaunchRequest(request.getJob(), new JobParameters(map));
68  
69  	}
70  
71  }