| 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.repository.dao; |
| 18 | |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.springframework.batch.core.JobExecution; |
| 22 | import org.springframework.batch.core.Step; |
| 23 | import org.springframework.batch.core.StepExecution; |
| 24 | import org.springframework.batch.item.ExecutionContext; |
| 25 | import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; |
| 26 | import org.springframework.dao.OptimisticLockingFailureException; |
| 27 | import org.springframework.util.Assert; |
| 28 | |
| 29 | /** |
| 30 | * In-memory implementation of {@link StepExecutionDao}. |
| 31 | */ |
| 32 | public class MapStepExecutionDao implements StepExecutionDao { |
| 33 | |
| 34 | private static Map executionsByJobExecutionId; |
| 35 | |
| 36 | private static Map contextsByStepExecutionId; |
| 37 | |
| 38 | private static long currentId = 0; |
| 39 | |
| 40 | static { |
| 41 | executionsByJobExecutionId = TransactionAwareProxyFactory.createTransactionalMap(); |
| 42 | contextsByStepExecutionId = TransactionAwareProxyFactory.createTransactionalMap(); |
| 43 | } |
| 44 | |
| 45 | public static void clear() { |
| 46 | executionsByJobExecutionId.clear(); |
| 47 | } |
| 48 | |
| 49 | public ExecutionContext findExecutionContext(StepExecution stepExecution) { |
| 50 | return (ExecutionContext) contextsByStepExecutionId.get(stepExecution.getId()); |
| 51 | } |
| 52 | |
| 53 | public void saveStepExecution(StepExecution stepExecution) { |
| 54 | Assert.isTrue(stepExecution.getId() == null); |
| 55 | Assert.isTrue(stepExecution.getVersion() == null); |
| 56 | Assert.notNull(stepExecution.getJobExecutionId(), "JobExecution must be saved already."); |
| 57 | |
| 58 | Map executions = (Map) executionsByJobExecutionId.get(stepExecution.getJobExecutionId()); |
| 59 | if (executions == null) { |
| 60 | executions = TransactionAwareProxyFactory.createTransactionalMap(); |
| 61 | executionsByJobExecutionId.put(stepExecution.getJobExecutionId(), executions); |
| 62 | } |
| 63 | stepExecution.setId(new Long(currentId++)); |
| 64 | stepExecution.incrementVersion(); |
| 65 | executions.put(stepExecution.getStepName(), stepExecution); |
| 66 | } |
| 67 | |
| 68 | public void updateStepExecution(StepExecution stepExecution) { |
| 69 | |
| 70 | Assert.notNull(stepExecution.getJobExecutionId()); |
| 71 | |
| 72 | Map executions = (Map) executionsByJobExecutionId.get(stepExecution.getJobExecutionId()); |
| 73 | Assert.notNull(executions, "step executions for given job execution are expected to be already saved"); |
| 74 | |
| 75 | StepExecution persistedExecution = (StepExecution) executions.get(stepExecution.getStepName()); |
| 76 | Assert.notNull(persistedExecution, "step execution is expected to be already saved"); |
| 77 | |
| 78 | synchronized (stepExecution) { |
| 79 | if (!persistedExecution.getVersion().equals(stepExecution.getVersion())) { |
| 80 | throw new OptimisticLockingFailureException("Attempt to update step execution id=" |
| 81 | + stepExecution.getId() + " with wrong version (" + stepExecution.getVersion() |
| 82 | + "), where current version is " + persistedExecution.getVersion()); |
| 83 | } |
| 84 | |
| 85 | stepExecution.incrementVersion(); |
| 86 | executions.put(stepExecution.getStepName(), stepExecution); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public StepExecution getStepExecution(JobExecution jobExecution, Step step) { |
| 91 | Map executions = (Map) executionsByJobExecutionId.get(jobExecution.getId()); |
| 92 | if (executions == null) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | return (StepExecution) executions.get(step.getName()); |
| 97 | } |
| 98 | |
| 99 | public void saveOrUpdateExecutionContext(StepExecution stepExecution) { |
| 100 | contextsByStepExecutionId.put(stepExecution.getId(), stepExecution.getExecutionContext()); |
| 101 | } |
| 102 | |
| 103 | } |