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