| 1 | package org.springframework.batch.core.repository.dao; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import org.apache.commons.lang.SerializationUtils; |
| 9 | import org.springframework.batch.core.JobExecution; |
| 10 | import org.springframework.batch.core.JobInstance; |
| 11 | import org.springframework.batch.item.ExecutionContext; |
| 12 | import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; |
| 13 | import org.springframework.util.Assert; |
| 14 | |
| 15 | /** |
| 16 | * In-memory implementation of {@link JobExecutionDao}. |
| 17 | * |
| 18 | */ |
| 19 | public class MapJobExecutionDao implements JobExecutionDao { |
| 20 | |
| 21 | private static Map executionsById = TransactionAwareProxyFactory.createTransactionalMap(); |
| 22 | |
| 23 | private static Map contextsByJobExecutionId = TransactionAwareProxyFactory.createTransactionalMap(); |
| 24 | |
| 25 | private static long currentId = 0; |
| 26 | |
| 27 | public static void clear() { |
| 28 | executionsById.clear(); |
| 29 | contextsByJobExecutionId.clear(); |
| 30 | } |
| 31 | |
| 32 | private static final JobExecution copy(JobExecution original) { |
| 33 | return (JobExecution) SerializationUtils.deserialize(SerializationUtils.serialize(original)); |
| 34 | } |
| 35 | |
| 36 | private static final ExecutionContext copy(ExecutionContext original) { |
| 37 | return (ExecutionContext) SerializationUtils.deserialize(SerializationUtils.serialize(original)); |
| 38 | } |
| 39 | |
| 40 | public int getJobExecutionCount(JobInstance jobInstance) { |
| 41 | int count = 0; |
| 42 | for (Iterator iterator = executionsById.values().iterator(); iterator.hasNext();) { |
| 43 | JobExecution exec = (JobExecution) iterator.next(); |
| 44 | if (exec.getJobInstance().equals(jobInstance)) { |
| 45 | count++; |
| 46 | } |
| 47 | } |
| 48 | return count; |
| 49 | } |
| 50 | |
| 51 | public void saveJobExecution(JobExecution jobExecution) { |
| 52 | Assert.isTrue(jobExecution.getId() == null); |
| 53 | Long newId = new Long(currentId++); |
| 54 | jobExecution.setId(newId); |
| 55 | jobExecution.incrementVersion(); |
| 56 | executionsById.put(newId, copy(jobExecution)); |
| 57 | } |
| 58 | |
| 59 | public List findJobExecutions(JobInstance jobInstance) { |
| 60 | List executions = new ArrayList(); |
| 61 | for (Iterator iterator = executionsById.values().iterator(); iterator.hasNext();) { |
| 62 | JobExecution exec = (JobExecution) iterator.next(); |
| 63 | if (exec.getJobInstance().equals(jobInstance)) { |
| 64 | executions.add(copy(exec)); |
| 65 | } |
| 66 | } |
| 67 | return executions; |
| 68 | } |
| 69 | |
| 70 | public void updateJobExecution(JobExecution jobExecution) { |
| 71 | Long id = jobExecution.getId(); |
| 72 | Assert.notNull(id, "JobExecution is expected to have an id (should be saved already)"); |
| 73 | Assert.notNull(executionsById.get(id), "JobExecution must already be saved"); |
| 74 | jobExecution.incrementVersion(); |
| 75 | executionsById.put(id, copy(jobExecution)); |
| 76 | } |
| 77 | |
| 78 | public JobExecution getLastJobExecution(JobInstance jobInstance) { |
| 79 | JobExecution lastExec = null; |
| 80 | for (Iterator iterator = executionsById.values().iterator(); iterator.hasNext();) { |
| 81 | JobExecution exec = (JobExecution) iterator.next(); |
| 82 | if (!exec.getJobInstance().equals(jobInstance)) { |
| 83 | continue; |
| 84 | } |
| 85 | if (lastExec == null) { |
| 86 | lastExec = exec; |
| 87 | } |
| 88 | if (lastExec.getCreateTime().before(exec.getCreateTime())) { |
| 89 | lastExec = exec; |
| 90 | } |
| 91 | } |
| 92 | if (lastExec == null) { |
| 93 | return null; |
| 94 | } |
| 95 | else { |
| 96 | return copy(lastExec); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public ExecutionContext findExecutionContext(JobExecution jobExecution) { |
| 101 | return copy((ExecutionContext) contextsByJobExecutionId.get(jobExecution.getId())); |
| 102 | } |
| 103 | |
| 104 | public void saveOrUpdateExecutionContext(JobExecution jobExecution) { |
| 105 | contextsByJobExecutionId.put(jobExecution.getId(), copy(jobExecution.getExecutionContext())); |
| 106 | |
| 107 | } |
| 108 | } |