1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.core.configuration.support;
17
18 import java.util.Collections;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22
23 import org.springframework.batch.core.Job;
24 import org.springframework.batch.core.configuration.DuplicateJobException;
25 import org.springframework.batch.core.configuration.JobFactory;
26 import org.springframework.batch.core.configuration.JobRegistry;
27 import org.springframework.batch.core.launch.NoSuchJobException;
28 import org.springframework.util.Assert;
29
30
31
32
33
34
35
36 public class MapJobRegistry implements JobRegistry {
37
38
39
40
41
42 private final ConcurrentMap<String, JobFactory> map = new ConcurrentHashMap<String, JobFactory>();
43
44 @Override
45 public void register(JobFactory jobFactory) throws DuplicateJobException {
46 Assert.notNull(jobFactory);
47 String name = jobFactory.getJobName();
48 Assert.notNull(name, "Job configuration must have a name.");
49 JobFactory previousValue = map.putIfAbsent(name, jobFactory);
50 if (previousValue != null) {
51 throw new DuplicateJobException("A job configuration with this name [" + name
52 + "] was already registered");
53 }
54 }
55
56 @Override
57 public void unregister(String name) {
58 Assert.notNull(name, "Job configuration must have a name.");
59 map.remove(name);
60 }
61
62 @Override
63 public Job getJob(String name) throws NoSuchJobException {
64 JobFactory factory = map.get(name);
65 if (factory == null) {
66 throw new NoSuchJobException("No job configuration with the name [" + name + "] was registered");
67 } else {
68 return factory.createJob();
69 }
70 }
71
72
73
74
75 @Override
76 public Set<String> getJobNames() {
77 return Collections.unmodifiableSet(map.keySet());
78 }
79
80 }