EMMA Coverage Report (generated Fri Aug 21 15:59:46 BST 2009)
[all classes][org.springframework.batch.core.repository.dao]

COVERAGE SUMMARY FOR SOURCE FILE [MapStepExecutionDao.java]

nameclass, %method, %block, %line, %
MapStepExecutionDao.java100% (2/2)100% (10/10)100% (227/227)100% (51/51)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MapStepExecutionDao100% (1/1)100% (8/8)100% (212/212)100% (49/49)
<static initializer> 100% (1/1)100% (7/7)100% (6/6)
MapStepExecutionDao (): void 100% (1/1)100% (3/3)100% (1/1)
addStepExecutions (JobExecution): void 100% (1/1)100% (50/50)100% (10/10)
clear (): void 100% (1/1)100% (5/5)100% (3/3)
copy (StepExecution): StepExecution 100% (1/1)100% (5/5)100% (1/1)
getStepExecution (JobExecution, Long): StepExecution 100% (1/1)100% (5/5)100% (1/1)
saveStepExecution (StepExecution): void 100% (1/1)100% (60/60)100% (13/13)
updateStepExecution (StepExecution): void 100% (1/1)100% (77/77)100% (15/15)
     
class MapStepExecutionDao$1100% (1/1)100% (2/2)100% (15/15)100% (3/3)
MapStepExecutionDao$1 (MapStepExecutionDao): void 100% (1/1)100% (6/6)100% (2/2)
compare (Entity, Entity): int 100% (1/1)100% (9/9)100% (1/1)

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

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