View Javadoc

1   /*
2    * Copyright 2006-2013 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.core.explore.support;
18  
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.springframework.batch.core.JobExecution;
23  import org.springframework.batch.core.JobInstance;
24  import org.springframework.batch.core.StepExecution;
25  import org.springframework.batch.core.explore.JobExplorer;
26  import org.springframework.batch.core.repository.dao.ExecutionContextDao;
27  import org.springframework.batch.core.repository.dao.JobExecutionDao;
28  import org.springframework.batch.core.repository.dao.JobInstanceDao;
29  import org.springframework.batch.core.repository.dao.StepExecutionDao;
30  
31  /**
32   * Implementation of {@link JobExplorer} using the injected DAOs.
33   *
34   * @author Dave Syer
35   * @author Lucas Ward
36   *
37   * @see JobExplorer
38   * @see JobInstanceDao
39   * @see JobExecutionDao
40   * @see StepExecutionDao
41   * @since 2.0
42   */
43  public class SimpleJobExplorer implements JobExplorer {
44  
45  	private JobInstanceDao jobInstanceDao;
46  
47  	private JobExecutionDao jobExecutionDao;
48  
49  	private StepExecutionDao stepExecutionDao;
50  
51  	private ExecutionContextDao ecDao;
52  
53  	/**
54  	 * Provide default constructor with low visibility in case user wants to use
55  	 * use aop:proxy-target-class="true" for AOP interceptor.
56  	 */
57  	SimpleJobExplorer() {
58  	}
59  
60  	public SimpleJobExplorer(JobInstanceDao jobInstanceDao, JobExecutionDao jobExecutionDao,
61  			StepExecutionDao stepExecutionDao, ExecutionContextDao ecDao) {
62  		super();
63  		this.jobInstanceDao = jobInstanceDao;
64  		this.jobExecutionDao = jobExecutionDao;
65  		this.stepExecutionDao = stepExecutionDao;
66  		this.ecDao = ecDao;
67  	}
68  
69  	/*
70  	 * (non-Javadoc)
71  	 *
72  	 * @see
73  	 * org.springframework.batch.core.explore.JobExplorer#findJobExecutions(
74  	 * org.springframework.batch.core.JobInstance)
75  	 */
76  	@Override
77  	public List<JobExecution> getJobExecutions(JobInstance jobInstance) {
78  		List<JobExecution> executions = jobExecutionDao.findJobExecutions(jobInstance);
79  		for (JobExecution jobExecution : executions) {
80  			getJobExecutionDependencies(jobExecution);
81  			for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
82  				getStepExecutionDependencies(stepExecution);
83  			}
84  		}
85  		return executions;
86  	}
87  
88  	/*
89  	 * (non-Javadoc)
90  	 *
91  	 * @see
92  	 * org.springframework.batch.core.explore.JobExplorer#findRunningJobExecutions
93  	 * (java.lang.String)
94  	 */
95  	@Override
96  	public Set<JobExecution> findRunningJobExecutions(String jobName) {
97  		Set<JobExecution> executions = jobExecutionDao.findRunningJobExecutions(jobName);
98  		for (JobExecution jobExecution : executions) {
99  			getJobExecutionDependencies(jobExecution);
100 			for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
101 				getStepExecutionDependencies(stepExecution);
102 			}
103 		}
104 		return executions;
105 	}
106 
107 	/*
108 	 * (non-Javadoc)
109 	 *
110 	 * @see
111 	 * org.springframework.batch.core.explore.JobExplorer#getJobExecution(java
112 	 * .lang.Long)
113 	 */
114 	@Override
115 	public JobExecution getJobExecution(Long executionId) {
116 		if (executionId == null) {
117 			return null;
118 		}
119 		JobExecution jobExecution = jobExecutionDao.getJobExecution(executionId);
120 		if (jobExecution == null) {
121 			return null;
122 		}
123 		getJobExecutionDependencies(jobExecution);
124 		for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
125 			getStepExecutionDependencies(stepExecution);
126 		}
127 		return jobExecution;
128 	}
129 
130 	/*
131 	 * (non-Javadoc)
132 	 *
133 	 * @see
134 	 * org.springframework.batch.core.explore.JobExplorer#getStepExecution(java
135 	 * .lang.Long)
136 	 */
137 	@Override
138 	public StepExecution getStepExecution(Long jobExecutionId, Long executionId) {
139 		JobExecution jobExecution = jobExecutionDao.getJobExecution(jobExecutionId);
140 		if (jobExecution == null) {
141 			return null;
142 		}
143 		StepExecution stepExecution = stepExecutionDao.getStepExecution(jobExecution, executionId);
144 		getStepExecutionDependencies(stepExecution);
145 		return stepExecution;
146 	}
147 
148 	/*
149 	 * (non-Javadoc)
150 	 *
151 	 * @see
152 	 * org.springframework.batch.core.explore.JobExplorer#getJobInstance(java
153 	 * .lang.Long)
154 	 */
155 	@Override
156 	public JobInstance getJobInstance(Long instanceId) {
157 		return jobInstanceDao.getJobInstance(instanceId);
158 	}
159 
160 	/*
161 	 * (non-Javadoc)
162 	 *
163 	 * @see
164 	 * org.springframework.batch.core.explore.JobExplorer#getLastJobInstances
165 	 * (java.lang.String, int)
166 	 */
167 	@Override
168 	public List<JobInstance> getJobInstances(String jobName, int start, int count) {
169 		return jobInstanceDao.getJobInstances(jobName, start, count);
170 	}
171 
172 	/*
173 	 * (non-Javadoc)
174 	 *
175 	 * @see org.springframework.batch.core.explore.JobExplorer#getJobNames()
176 	 */
177 	@Override
178 	public List<String> getJobNames() {
179 		return jobInstanceDao.getJobNames();
180 	}
181 
182 	/*
183 	 * Find all dependencies for a JobExecution, including JobInstance (which
184 	 * requires JobParameters) plus StepExecutions
185 	 */
186 	private void getJobExecutionDependencies(JobExecution jobExecution) {
187 
188 		JobInstance jobInstance = jobInstanceDao.getJobInstance(jobExecution);
189 		stepExecutionDao.addStepExecutions(jobExecution);
190 		jobExecution.setJobInstance(jobInstance);
191 		jobExecution.setExecutionContext(ecDao.getExecutionContext(jobExecution));
192 
193 	}
194 
195 	private void getStepExecutionDependencies(StepExecution stepExecution) {
196 		if (stepExecution != null) {
197 			stepExecution.setExecutionContext(ecDao.getExecutionContext(stepExecution));
198 		}
199 	}
200 
201 }