View Javadoc
1   /*
2    * Copyright 2013-2014 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  
17  package org.springframework.batch.admin.web;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.springframework.batch.admin.domain.DetailedJobInfo;
24  import org.springframework.batch.admin.domain.DetailedJobInfoResource;
25  import org.springframework.batch.admin.domain.JobExecutionInfo;
26  import org.springframework.batch.admin.domain.NoSuchBatchJobException;
27  import org.springframework.batch.core.JobExecution;
28  import org.springframework.batch.core.launch.NoSuchJobException;
29  import org.springframework.data.domain.PageImpl;
30  import org.springframework.data.domain.Pageable;
31  import org.springframework.data.web.PagedResourcesAssembler;
32  import org.springframework.hateoas.ExposesResourceFor;
33  import org.springframework.hateoas.PagedResources;
34  import org.springframework.http.HttpStatus;
35  import org.springframework.web.bind.annotation.PathVariable;
36  import org.springframework.web.bind.annotation.RequestMapping;
37  import org.springframework.web.bind.annotation.RequestMethod;
38  import org.springframework.web.bind.annotation.ResponseStatus;
39  import org.springframework.web.bind.annotation.RestController;
40  
41  
42  /**
43   * Controller for batch jobs and job instances, job executions on a given batch job.
44   * 
45   * @author Dave Syer
46   * @author Ilayaperumal Gopinathan
47   * @author Andrew Eisenberg
48   * @since 2.0
49   * 
50   */
51  @RestController
52  @RequestMapping("/batch/configurations")
53  @ExposesResourceFor(DetailedJobInfoResource.class)
54  public class BatchJobsController extends AbstractBatchJobsController {
55  
56  	/**
57  	 * Get the paged resources of {@link org.springframework.batch.admin.domain.DetailedJobInfoResource}
58  	 * 
59  	 * @param pageable the paging metadata
60  	 * @param assembler the paged resource assembler of type {@link org.springframework.batch.admin.domain.DetailedJobInfo}
61  	 */
62  	@RequestMapping(value = "", method = RequestMethod.GET)
63  	@ResponseStatus(HttpStatus.OK)
64  	public PagedResources<DetailedJobInfoResource> jobs(Pageable pageable,
65  			PagedResourcesAssembler<DetailedJobInfo> assembler) {
66  		int total = jobService.countJobs();
67  		Collection<String> names = jobService.listJobs(pageable.getOffset(), pageable.getPageSize());
68  		List<DetailedJobInfo> detailedJobs = new ArrayList<DetailedJobInfo>();
69  		for (String name : names) {
70  			detailedJobs.add(getJobInfo(name));
71  		}
72  		return assembler.toResource(
73  				new PageImpl<DetailedJobInfo>(detailedJobs, pageable, total),
74  				jobInfoResourceAssembler);
75  	}
76  
77  	/**
78  	 * @param jobName name of the job
79  	 * @return ExpandedJobInfo for the given job name
80  	 */
81  	@RequestMapping(value = "/{jobName}", method = RequestMethod.GET)
82  	@ResponseStatus(HttpStatus.OK)
83  	public DetailedJobInfoResource jobinfo(@PathVariable String jobName) {
84  		return getJobInfoResource(jobName);
85  	}
86  
87  	/**
88  	 * @param jobName
89  	 * @return the detailed job info resource for the given job name.
90  	 */
91  	private DetailedJobInfoResource getJobInfoResource(String jobName) {
92  		return jobInfoResourceAssembler.toResource(getJobInfo(jobName));
93  	}
94  
95  	/**
96  	 * Get detailed job info
97  	 *
98  	 * @param jobName name of the job
99  	 * @return a job info for this job
100 	 */
101 	private DetailedJobInfo getJobInfo(String jobName) {
102 		boolean launchable = jobService.isLaunchable(jobName);
103 		try {
104 			int count = jobService.countJobExecutionsForJob(jobName);
105 			return new DetailedJobInfo(jobName, count, launchable,
106 					jobService.isIncrementable(jobName),
107 					getLastExecution(jobName));
108 		}
109 		catch (NoSuchJobException e) {
110 			throw new NoSuchBatchJobException(jobName);
111 		}
112 	}
113 
114 	/**
115 	 * @param jobName name of the batch job
116 	 * @return Last job execution info
117 	 * @throws org.springframework.batch.core.launch.NoSuchJobException
118 	 */
119 	private JobExecutionInfo getLastExecution(String jobName) throws NoSuchJobException {
120 		Collection<JobExecution> executions = jobService.listJobExecutionsForJob(jobName, 0, 1);
121 		if (executions.size() > 0) {
122 			return new JobExecutionInfo(executions.iterator().next(), timeZone);
123 		}
124 		else {
125 			return null;
126 		}
127 	}
128 
129 }