EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.core.repository.dao]

COVERAGE SUMMARY FOR SOURCE FILE [MapJobExecutionDao.java]

nameclass, %method, %block, %line, %
MapJobExecutionDao.java100% (2/2)100% (12/12)99%  (272/276)97%  (58/60)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MapJobExecutionDao$1100% (1/1)100% (2/2)93%  (26/28)86%  (6/7)
compare (JobExecution, JobExecution): int 100% (1/1)91%  (20/22)83%  (5/6)
MapJobExecutionDao$1 (MapJobExecutionDao): void 100% (1/1)100% (6/6)100% (1/1)
     
class MapJobExecutionDao100% (1/1)100% (10/10)99%  (246/248)98%  (53/54)
saveJobExecution (JobExecution): void 100% (1/1)96%  (24/25)99%  (6/6)
getLastJobExecution (JobInstance): JobExecution 100% (1/1)97%  (35/36)90%  (9/10)
MapJobExecutionDao (): void 100% (1/1)100% (14/14)100% (3/3)
clear (): void 100% (1/1)100% (4/4)100% (2/2)
copy (JobExecution): JobExecution 100% (1/1)100% (7/7)100% (2/2)
findJobExecutions (JobInstance): List 100% (1/1)100% (35/35)100% (7/7)
findRunningJobExecutions (String): Set 100% (1/1)100% (34/34)100% (7/7)
getJobExecution (Long): JobExecution 100% (1/1)100% (7/7)100% (1/1)
synchronizeStatus (JobExecution): void 100% (1/1)100% (21/21)100% (5/5)
updateJobExecution (JobExecution): void 100% (1/1)100% (65/65)100% (11/11)

1/*
2 * Copyright 2006-2013 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.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.HashSet;
23import java.util.List;
24import java.util.Set;
25import java.util.concurrent.ConcurrentHashMap;
26import java.util.concurrent.ConcurrentMap;
27import java.util.concurrent.atomic.AtomicLong;
28 
29import org.springframework.batch.core.JobExecution;
30import org.springframework.batch.core.JobInstance;
31import org.springframework.batch.support.SerializationUtils;
32import org.springframework.dao.OptimisticLockingFailureException;
33import org.springframework.util.Assert;
34 
35/**
36 * In-memory implementation of {@link JobExecutionDao}.
37 */
38public class MapJobExecutionDao implements JobExecutionDao {
39 
40        // JDK6 Make this into a ConcurrentSkipListMap: adds and removes tend to be very near the front or back
41        private final ConcurrentMap<Long, JobExecution> executionsById = new ConcurrentHashMap<Long, JobExecution>();
42 
43        private final AtomicLong currentId = new AtomicLong(0L);
44 
45        public void clear() {
46                executionsById.clear();
47        }
48 
49        private static JobExecution copy(JobExecution original) {
50                JobExecution copy = (JobExecution) SerializationUtils.deserialize(SerializationUtils.serialize(original));
51                return copy;
52        }
53 
54        @Override
55        public void saveJobExecution(JobExecution jobExecution) {
56                Assert.isTrue(jobExecution.getId() == null);
57                Long newId = currentId.getAndIncrement();
58                jobExecution.setId(newId);
59                jobExecution.incrementVersion();
60                executionsById.put(newId, copy(jobExecution));
61        }
62 
63        @Override
64        public List<JobExecution> findJobExecutions(JobInstance jobInstance) {
65                List<JobExecution> executions = new ArrayList<JobExecution>();
66                for (JobExecution exec : executionsById.values()) {
67                        if (exec.getJobInstance().equals(jobInstance)) {
68                                executions.add(copy(exec));
69                        }
70                }
71                Collections.sort(executions, new Comparator<JobExecution>() {
72 
73                        @Override
74                        public int compare(JobExecution e1, JobExecution e2) {
75                                long result = (e1.getId() - e2.getId());
76                                if (result > 0) {
77                                        return -1;
78                                }
79                                else if (result < 0) {
80                                        return 1;
81                                }
82                                else {
83                                        return 0;
84                                }
85                        }
86                });
87                return executions;
88        }
89 
90        @Override
91        public void updateJobExecution(JobExecution jobExecution) {
92                Long id = jobExecution.getId();
93                Assert.notNull(id, "JobExecution is expected to have an id (should be saved already)");
94                JobExecution persistedExecution = executionsById.get(id);
95                Assert.notNull(persistedExecution, "JobExecution must already be saved");
96 
97                synchronized (jobExecution) {
98                        if (!persistedExecution.getVersion().equals(jobExecution.getVersion())) {
99                                throw new OptimisticLockingFailureException("Attempt to update step execution id=" + id
100                                                + " with wrong version (" + jobExecution.getVersion() + "), where current version is "
101                                                + persistedExecution.getVersion());
102                        }
103                        jobExecution.incrementVersion();
104                        executionsById.put(id, copy(jobExecution));
105                }
106        }
107 
108        @Override
109        public JobExecution getLastJobExecution(JobInstance jobInstance) {
110                JobExecution lastExec = null;
111                for (JobExecution exec : executionsById.values()) {
112                        if (!exec.getJobInstance().equals(jobInstance)) {
113                                continue;
114                        }
115                        if (lastExec == null) {
116                                lastExec = exec;
117                        }
118                        if (lastExec.getCreateTime().before(exec.getCreateTime())) {
119                                lastExec = exec;
120                        }
121                }
122                return copy(lastExec);
123        }
124 
125        /*
126         * (non-Javadoc)
127         *
128         * @seeorg.springframework.batch.core.repository.dao.JobExecutionDao#
129         * findRunningJobExecutions(java.lang.String)
130         */
131        @Override
132        public Set<JobExecution> findRunningJobExecutions(String jobName) {
133                Set<JobExecution> result = new HashSet<JobExecution>();
134                for (JobExecution exec : executionsById.values()) {
135                        if (!exec.getJobInstance().getJobName().equals(jobName) || !exec.isRunning()) {
136                                continue;
137                        }
138                        result.add(copy(exec));
139                }
140                return result;
141        }
142 
143        /*
144         * (non-Javadoc)
145         *
146         * @see
147         * org.springframework.batch.core.repository.dao.JobExecutionDao#getJobExecution
148         * (java.lang.Long)
149         */
150        @Override
151        public JobExecution getJobExecution(Long executionId) {
152                return copy(executionsById.get(executionId));
153        }
154 
155        @Override
156        public void synchronizeStatus(JobExecution jobExecution) {
157                JobExecution saved = getJobExecution(jobExecution.getId());
158                if (saved.getVersion().intValue() != jobExecution.getVersion().intValue()) {
159                        jobExecution.upgradeStatus(saved.getStatus());
160                        jobExecution.setVersion(saved.getVersion());
161                }
162        }
163}

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