1 | /* |
2 | * Copyright 2012-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 | 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 | * Simple map-based implementation of {@link StepRegistry}. Access to the map is |
33 | * synchronized, guarded by an internal lock. |
34 | * |
35 | * @author Sebastien Gerard |
36 | * @author Stephane Nicoll |
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 | } |