| 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 | package org.springframework.batch.core.repository.dao; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collections; |
| 20 | import java.util.Comparator; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import org.apache.commons.lang.SerializationUtils; |
| 25 | import org.springframework.batch.core.Entity; |
| 26 | import org.springframework.batch.core.JobExecution; |
| 27 | import org.springframework.batch.core.StepExecution; |
| 28 | import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; |
| 29 | import org.springframework.dao.OptimisticLockingFailureException; |
| 30 | import org.springframework.util.Assert; |
| 31 | |
| 32 | /** |
| 33 | * In-memory implementation of {@link StepExecutionDao}. |
| 34 | */ |
| 35 | public class MapStepExecutionDao implements StepExecutionDao { |
| 36 | |
| 37 | private static Map<Long, Map<Long, StepExecution>> executionsByJobExecutionId = TransactionAwareProxyFactory |
| 38 | .createTransactionalMap(); |
| 39 | |
| 40 | private static Map<Long, StepExecution> executionsByStepExecutionId = TransactionAwareProxyFactory |
| 41 | .createTransactionalMap(); |
| 42 | |
| 43 | private static long currentId = 0; |
| 44 | |
| 45 | public static void clear() { |
| 46 | executionsByJobExecutionId.clear(); |
| 47 | executionsByStepExecutionId.clear(); |
| 48 | } |
| 49 | |
| 50 | private static StepExecution copy(StepExecution original) { |
| 51 | return (StepExecution) SerializationUtils.deserialize(SerializationUtils.serialize(original)); |
| 52 | } |
| 53 | |
| 54 | public void saveStepExecution(StepExecution stepExecution) { |
| 55 | |
| 56 | Assert.isTrue(stepExecution.getId() == null); |
| 57 | Assert.isTrue(stepExecution.getVersion() == null); |
| 58 | Assert.notNull(stepExecution.getJobExecutionId(), "JobExecution must be saved already."); |
| 59 | |
| 60 | Map<Long, StepExecution> executions = executionsByJobExecutionId.get(stepExecution.getJobExecutionId()); |
| 61 | if (executions == null) { |
| 62 | executions = TransactionAwareProxyFactory.createTransactionalMap(); |
| 63 | executionsByJobExecutionId.put(stepExecution.getJobExecutionId(), executions); |
| 64 | } |
| 65 | |
| 66 | stepExecution.setId(currentId++); |
| 67 | stepExecution.incrementVersion(); |
| 68 | StepExecution copy = copy(stepExecution); |
| 69 | executions.put(stepExecution.getId(), copy); |
| 70 | executionsByStepExecutionId.put(stepExecution.getId(), copy); |
| 71 | |
| 72 | } |
| 73 | |
| 74 | public void updateStepExecution(StepExecution stepExecution) { |
| 75 | |
| 76 | Assert.notNull(stepExecution.getJobExecutionId()); |
| 77 | |
| 78 | Map<Long, StepExecution> executions = executionsByJobExecutionId.get(stepExecution.getJobExecutionId()); |
| 79 | Assert.notNull(executions, "step executions for given job execution are expected to be already saved"); |
| 80 | |
| 81 | StepExecution persistedExecution = executionsByStepExecutionId.get(stepExecution.getId()); |
| 82 | Assert.notNull(persistedExecution, "step execution is expected to be already saved"); |
| 83 | |
| 84 | synchronized (stepExecution) { |
| 85 | if (!persistedExecution.getVersion().equals(stepExecution.getVersion())) { |
| 86 | throw new OptimisticLockingFailureException("Attempt to update step execution id=" |
| 87 | + stepExecution.getId() + " with wrong version (" + stepExecution.getVersion() |
| 88 | + "), where current version is " + persistedExecution.getVersion()); |
| 89 | } |
| 90 | |
| 91 | stepExecution.incrementVersion(); |
| 92 | StepExecution copy = copy(stepExecution); |
| 93 | executions.put(stepExecution.getId(), copy); |
| 94 | executionsByStepExecutionId.put(stepExecution.getId(), copy); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecutionId) { |
| 99 | return executionsByStepExecutionId.get(stepExecutionId); |
| 100 | } |
| 101 | |
| 102 | public void addStepExecutions(JobExecution jobExecution) { |
| 103 | Map<Long, StepExecution> executions = executionsByJobExecutionId.get(jobExecution.getId()); |
| 104 | if (executions == null || executions.isEmpty()) { |
| 105 | return; |
| 106 | } |
| 107 | List<StepExecution> result = new ArrayList<StepExecution>(executions.values()); |
| 108 | Collections.sort(result, new Comparator<Entity>() { |
| 109 | |
| 110 | public int compare(Entity o1, Entity o2) { |
| 111 | return Long.signum(o2.getId() - o1.getId()); |
| 112 | } |
| 113 | }); |
| 114 | |
| 115 | List<StepExecution> copy = new ArrayList<StepExecution>(result.size()); |
| 116 | for (StepExecution exec : result) { |
| 117 | copy.add(copy(exec)); |
| 118 | } |
| 119 | jobExecution.addStepExecutions(copy); |
| 120 | } |
| 121 | } |