EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[all classes][org.springframework.batch.core.repository.dao]

COVERAGE SUMMARY FOR SOURCE FILE [MapStepExecutionDao.java]

nameclass, %method, %block, %line, %
MapStepExecutionDao.java100% (1/1)100% (10/10)100% (184/184)100% (40/40)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MapStepExecutionDao100% (1/1)100% (10/10)100% (184/184)100% (40/40)
<static initializer> 100% (1/1)100% (7/7)100% (4/4)
MapStepExecutionDao (): void 100% (1/1)100% (3/3)100% (1/1)
clear (): void 100% (1/1)100% (5/5)100% (3/3)
copy (ExecutionContext): ExecutionContext 100% (1/1)100% (5/5)100% (1/1)
copy (StepExecution): StepExecution 100% (1/1)100% (5/5)100% (1/1)
findExecutionContext (StepExecution): ExecutionContext 100% (1/1)100% (7/7)100% (1/1)
getStepExecution (JobExecution, Step): StepExecution 100% (1/1)100% (17/17)100% (4/4)
saveOrUpdateExecutionContext (StepExecution): void 100% (1/1)100% (9/9)100% (2/2)
saveStepExecution (StepExecution): void 100% (1/1)100% (54/54)100% (11/11)
updateStepExecution (StepExecution): void 100% (1/1)100% (72/72)100% (12/12)

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 
17package org.springframework.batch.core.repository.dao;
18 
19import java.util.Map;
20 
21import org.apache.commons.lang.SerializationUtils;
22import org.springframework.batch.core.JobExecution;
23import org.springframework.batch.core.Step;
24import org.springframework.batch.core.StepExecution;
25import org.springframework.batch.item.ExecutionContext;
26import org.springframework.batch.support.transaction.TransactionAwareProxyFactory;
27import org.springframework.dao.OptimisticLockingFailureException;
28import org.springframework.util.Assert;
29 
30/**
31 * In-memory implementation of {@link StepExecutionDao}.
32 */
33public 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}

[all classes][org.springframework.batch.core.repository.dao]
EMMA 2.0.5312 (C) Vladimir Roubtsov