View Javadoc

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