| 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.partition.support; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collection; |
| 21 | |
| 22 | import org.springframework.batch.core.StepExecution; |
| 23 | import org.springframework.batch.core.explore.JobExplorer; |
| 24 | import org.springframework.beans.factory.InitializingBean; |
| 25 | import org.springframework.util.Assert; |
| 26 | |
| 27 | /** |
| 28 | * Convenience class for aggregating a set of {@link StepExecution} instances |
| 29 | * when the input comes from remote steps, so the data need to be refreshed from |
| 30 | * the repository. |
| 31 | * |
| 32 | * @author Dave Syer |
| 33 | * @since 2.1 |
| 34 | */ |
| 35 | public class RemoteStepExecutionAggregator implements StepExecutionAggregator, InitializingBean { |
| 36 | |
| 37 | private StepExecutionAggregator delegate = new DefaultStepExecutionAggregator(); |
| 38 | |
| 39 | private JobExplorer jobExplorer; |
| 40 | |
| 41 | /** |
| 42 | * Create a new instance (useful for configuration purposes). |
| 43 | */ |
| 44 | public RemoteStepExecutionAggregator() { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Create a new instance with a job explorer that can be used to refresh the |
| 49 | * data when aggregating. |
| 50 | * |
| 51 | * @param jobExplorer the {@link JobExplorer} to use |
| 52 | */ |
| 53 | public RemoteStepExecutionAggregator(JobExplorer jobExplorer) { |
| 54 | super(); |
| 55 | this.jobExplorer = jobExplorer; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param jobExplorer the jobExplorer to set |
| 60 | */ |
| 61 | public void setJobExplorer(JobExplorer jobExplorer) { |
| 62 | this.jobExplorer = jobExplorer; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param delegate the delegate to set |
| 67 | */ |
| 68 | public void setDelegate(StepExecutionAggregator delegate) { |
| 69 | this.delegate = delegate; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @throws Exception if the job explorer is not provided |
| 74 | */ |
| 75 | @Override |
| 76 | public void afterPropertiesSet() throws Exception { |
| 77 | Assert.state(jobExplorer != null, "A JobExplorer must be provided"); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Aggregates the input executions into the result {@link StepExecution} |
| 82 | * delegating to the delegate aggregator once the input has been refreshed |
| 83 | * from the {@link JobExplorer}. |
| 84 | * |
| 85 | * @see StepExecutionAggregator #aggregate(StepExecution, Collection) |
| 86 | */ |
| 87 | @Override |
| 88 | public void aggregate(StepExecution result, Collection<StepExecution> executions) { |
| 89 | Assert.notNull(result, "To aggregate into a result it must be non-null."); |
| 90 | if (executions == null) { |
| 91 | return; |
| 92 | } |
| 93 | Collection<StepExecution> updates = new ArrayList<StepExecution>(); |
| 94 | for (StepExecution stepExecution : executions) { |
| 95 | Long id = stepExecution.getId(); |
| 96 | Assert.state(id != null, "StepExecution has null id. It must be saved first: " + stepExecution); |
| 97 | StepExecution update = jobExplorer.getStepExecution(stepExecution.getJobExecutionId(), id); |
| 98 | Assert.state(update != null, "Could not reload StepExecution from JobRepository: " + stepExecution); |
| 99 | updates.add(update); |
| 100 | } |
| 101 | delegate.aggregate(result, updates); |
| 102 | } |
| 103 | |
| 104 | } |