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