| 1 | /* |
| 2 | * Copyright 2006-2007 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, |
| 61 | JobExecutionDao jobExecutionDao, StepExecutionDao stepExecutionDao, |
| 62 | ExecutionContextDao ecDao) { |
| 63 | super(); |
| 64 | this.jobInstanceDao = jobInstanceDao; |
| 65 | this.jobExecutionDao = jobExecutionDao; |
| 66 | this.stepExecutionDao = stepExecutionDao; |
| 67 | this.ecDao = ecDao; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * (non-Javadoc) |
| 72 | * |
| 73 | * @see |
| 74 | * org.springframework.batch.core.explore.JobExplorer#findJobExecutions( |
| 75 | * org.springframework.batch.core.JobInstance) |
| 76 | */ |
| 77 | public List<JobExecution> getJobExecutions(JobInstance jobInstance) { |
| 78 | List<JobExecution> executions = jobExecutionDao |
| 79 | .findJobExecutions(jobInstance); |
| 80 | for (JobExecution jobExecution : executions) { |
| 81 | getJobExecutionDependencies(jobExecution); |
| 82 | } |
| 83 | return executions; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * (non-Javadoc) |
| 88 | * |
| 89 | * @see |
| 90 | * org.springframework.batch.core.explore.JobExplorer#findRunningJobExecutions |
| 91 | * (java.lang.String) |
| 92 | */ |
| 93 | public Set<JobExecution> findRunningJobExecutions(String jobName) { |
| 94 | Set<JobExecution> executions = jobExecutionDao |
| 95 | .findRunningJobExecutions(jobName); |
| 96 | for (JobExecution jobExecution : executions) { |
| 97 | getJobExecutionDependencies(jobExecution); |
| 98 | } |
| 99 | return executions; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * (non-Javadoc) |
| 104 | * |
| 105 | * @see |
| 106 | * org.springframework.batch.core.explore.JobExplorer#getJobExecution(java |
| 107 | * .lang.Long) |
| 108 | */ |
| 109 | public JobExecution getJobExecution(Long executionId) { |
| 110 | if (executionId == null) { |
| 111 | return null; |
| 112 | } |
| 113 | JobExecution jobExecution = jobExecutionDao |
| 114 | .getJobExecution(executionId); |
| 115 | if (jobExecution == null) { |
| 116 | return null; |
| 117 | } |
| 118 | getJobExecutionDependencies(jobExecution); |
| 119 | return jobExecution; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * (non-Javadoc) |
| 124 | * |
| 125 | * @see |
| 126 | * org.springframework.batch.core.explore.JobExplorer#getStepExecution(java |
| 127 | * .lang.Long) |
| 128 | */ |
| 129 | public StepExecution getStepExecution(Long jobExecutionId, Long executionId) { |
| 130 | JobExecution jobExecution = getJobExecution(jobExecutionId); |
| 131 | if (jobExecution == null) { |
| 132 | return null; |
| 133 | } |
| 134 | StepExecution stepExecution = stepExecutionDao.getStepExecution( |
| 135 | jobExecution, executionId); |
| 136 | getStepExecutionDependencies(stepExecution); |
| 137 | return stepExecution; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * (non-Javadoc) |
| 142 | * |
| 143 | * @see |
| 144 | * org.springframework.batch.core.explore.JobExplorer#getJobInstance(java |
| 145 | * .lang.Long) |
| 146 | */ |
| 147 | public JobInstance getJobInstance(Long instanceId) { |
| 148 | return jobInstanceDao.getJobInstance(instanceId); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * (non-Javadoc) |
| 153 | * |
| 154 | * @see |
| 155 | * org.springframework.batch.core.explore.JobExplorer#getLastJobInstances |
| 156 | * (java.lang.String, int) |
| 157 | */ |
| 158 | public List<JobInstance> getJobInstances(String jobName, int start, |
| 159 | int count) { |
| 160 | return jobInstanceDao.getJobInstances(jobName, start, count); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Find all dependencies for a JobExecution, including JobInstance (which |
| 165 | * requires JobParameters) plus StepExecutions |
| 166 | */ |
| 167 | private void getJobExecutionDependencies(JobExecution jobExecution) { |
| 168 | |
| 169 | JobInstance jobInstance = jobInstanceDao.getJobInstance(jobExecution); |
| 170 | stepExecutionDao.addStepExecutions(jobExecution); |
| 171 | jobExecution.setJobInstance(jobInstance); |
| 172 | jobExecution.setExecutionContext(ecDao |
| 173 | .getExecutionContext(jobExecution)); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | private void getStepExecutionDependencies(StepExecution stepExecution) { |
| 178 | if (stepExecution != null) { |
| 179 | stepExecution.setExecutionContext(ecDao |
| 180 | .getExecutionContext(stepExecution)); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | } |