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.StepExecution; |
24 | import org.springframework.batch.item.ExecutionContext; |
25 | import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; |
26 | |
27 | /** |
28 | * In-memory implementation of {@link ExecutionContextDao} backed by static |
29 | * maps. |
30 | * |
31 | * @author Robert Kasanicky |
32 | */ |
33 | public class MapExecutionContextDao implements ExecutionContextDao { |
34 | |
35 | private static Map<Long, ExecutionContext> contextsByStepExecutionId = TransactionAwareProxyFactory |
36 | .createTransactionalMap(); |
37 | |
38 | private static Map<Long, ExecutionContext> contextsByJobExecutionId = TransactionAwareProxyFactory |
39 | .createTransactionalMap(); |
40 | |
41 | public static void clear() { |
42 | contextsByJobExecutionId.clear(); |
43 | contextsByStepExecutionId.clear(); |
44 | } |
45 | |
46 | private static ExecutionContext copy(ExecutionContext original) { |
47 | return (ExecutionContext) SerializationUtils.deserialize(SerializationUtils.serialize(original)); |
48 | } |
49 | |
50 | public ExecutionContext getExecutionContext(StepExecution stepExecution) { |
51 | return copy(contextsByStepExecutionId.get(stepExecution.getId())); |
52 | } |
53 | |
54 | public void updateExecutionContext(StepExecution stepExecution) { |
55 | contextsByStepExecutionId.put(stepExecution.getId(), copy(stepExecution.getExecutionContext())); |
56 | } |
57 | |
58 | public ExecutionContext getExecutionContext(JobExecution jobExecution) { |
59 | return copy(contextsByJobExecutionId.get(jobExecution.getId())); |
60 | } |
61 | |
62 | public void updateExecutionContext(JobExecution jobExecution) { |
63 | contextsByJobExecutionId.put(jobExecution.getId(), copy(jobExecution.getExecutionContext())); |
64 | } |
65 | |
66 | public void saveExecutionContext(JobExecution jobExecution) { |
67 | updateExecutionContext(jobExecution); |
68 | } |
69 | |
70 | public void saveExecutionContext(StepExecution stepExecution) { |
71 | updateExecutionContext(stepExecution); |
72 | } |
73 | |
74 | } |