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.ArrayList; |
20 | import java.util.Collection; |
21 | import java.util.Collections; |
22 | import java.util.Comparator; |
23 | import java.util.List; |
24 | |
25 | import org.springframework.batch.core.JobExecution; |
26 | import org.springframework.batch.core.JobInstance; |
27 | import org.springframework.batch.core.JobParameters; |
28 | import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; |
29 | import org.springframework.util.Assert; |
30 | |
31 | /** |
32 | * In-memory implementation of {@link JobInstanceDao}. |
33 | */ |
34 | public class MapJobInstanceDao implements JobInstanceDao { |
35 | |
36 | private static Collection<JobInstance> jobInstances = TransactionAwareProxyFactory.createTransactionalSet(); |
37 | |
38 | private static long currentId = 0; |
39 | |
40 | public static void clear() { |
41 | jobInstances.clear(); |
42 | } |
43 | |
44 | public JobInstance createJobInstance(String jobName, JobParameters jobParameters) { |
45 | |
46 | Assert.state(getJobInstance(jobName, jobParameters) == null, "JobInstance must not already exist"); |
47 | |
48 | JobInstance jobInstance = new JobInstance(currentId++, jobParameters, jobName); |
49 | jobInstance.incrementVersion(); |
50 | jobInstances.add(jobInstance); |
51 | |
52 | return jobInstance; |
53 | } |
54 | |
55 | public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { |
56 | |
57 | for (JobInstance instance : jobInstances) { |
58 | if (instance.getJobName().equals(jobName) && instance.getJobParameters().equals(jobParameters)) { |
59 | return instance; |
60 | } |
61 | } |
62 | return null; |
63 | |
64 | } |
65 | |
66 | public JobInstance getJobInstance(Long instanceId) { |
67 | for (JobInstance instance : jobInstances) { |
68 | if (instance.getId().equals(instanceId)) { |
69 | return instance; |
70 | } |
71 | } |
72 | return null; |
73 | } |
74 | |
75 | public List<String> getJobNames() { |
76 | List<String> result = new ArrayList<String>(); |
77 | for (JobInstance instance : jobInstances) { |
78 | result.add(instance.getJobName()); |
79 | } |
80 | Collections.sort(result); |
81 | return result; |
82 | } |
83 | |
84 | public List<JobInstance> getJobInstances(String jobName, int start, int count) { |
85 | List<JobInstance> result = new ArrayList<JobInstance>(); |
86 | for (JobInstance instance : jobInstances) { |
87 | if (instance.getJobName().equals(jobName)) { |
88 | result.add(instance); |
89 | } |
90 | } |
91 | Collections.sort(result, new Comparator<JobInstance>() { |
92 | // sort by ID descending |
93 | public int compare(JobInstance o1, JobInstance o2) { |
94 | return Long.signum(o2.getId() - o1.getId()); |
95 | } |
96 | }); |
97 | if (start>=result.size()) { |
98 | start = result.size(); |
99 | } |
100 | if (start + count >=result.size()) { |
101 | count = result.size(); |
102 | } |
103 | return result.subList(start, count); |
104 | } |
105 | |
106 | public JobInstance getJobInstance(JobExecution jobExecution) { |
107 | return jobExecution.getJobInstance(); |
108 | } |
109 | |
110 | } |