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.Collection;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ConcurrentMap;
23
24 import org.springframework.batch.core.Step;
25 import org.springframework.batch.core.configuration.DuplicateJobException;
26 import org.springframework.batch.core.configuration.StepRegistry;
27 import org.springframework.batch.core.launch.NoSuchJobException;
28 import org.springframework.batch.core.step.NoSuchStepException;
29 import org.springframework.util.Assert;
30
31
32
33
34
35
36
37
38 public class MapStepRegistry implements StepRegistry {
39
40 private final ConcurrentMap<String, Map<String, Step>> map = new ConcurrentHashMap<String, Map<String, Step>>();
41
42 @Override
43 public void register(String jobName, Collection<Step> steps) throws DuplicateJobException {
44 Assert.notNull(jobName, "The job name cannot be null.");
45 Assert.notNull(steps, "The job steps cannot be null.");
46
47
48 final Map<String, Step> jobSteps = new HashMap<String, Step>();
49 for (Step step : steps) {
50 jobSteps.put(step.getName(), step);
51 }
52 final Object previousValue = map.putIfAbsent(jobName, jobSteps);
53 if (previousValue != null) {
54 throw new DuplicateJobException("A job configuration with this name [" + jobName
55 + "] was already registered");
56 }
57 }
58
59 @Override
60 public void unregisterStepsFromJob(String jobName) {
61 Assert.notNull(jobName, "Job configuration must have a name.");
62 map.remove(jobName);
63 }
64
65 @Override
66 public Step getStep(String jobName, String stepName) throws NoSuchJobException {
67 Assert.notNull(jobName, "The job name cannot be null.");
68 Assert.notNull(stepName, "The step name cannot be null.");
69 if (!map.containsKey(jobName)) {
70 throw new NoSuchJobException("No job configuration with the name [" + jobName + "] was registered");
71 } else {
72 final Map<String, Step> jobSteps = map.get(jobName);
73 if (jobSteps.containsKey(stepName)) {
74 return jobSteps.get(stepName);
75 } else {
76 throw new NoSuchStepException("The step called [" + stepName + "] does not exist in the job [" +
77 jobName + "]");
78 }
79 }
80 }
81
82 }