delegating synchronization to ConcurrentHashMap instead of dealing that ourselves.

This commit is contained in:
Stéphane Nicoll
2012-12-07 18:43:18 +01:00
committed by Michael Minella
parent 00ee45854e
commit 25f732e06b

View File

@@ -9,6 +9,8 @@ import org.springframework.util.Assert;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Simple map-based implementation of {@link StepRegistry}. Access to the map is
@@ -19,7 +21,7 @@ import java.util.Map;
*/
public class MapStepRegistry implements StepRegistry {
private final Map<String, Map<String, Step>> map = new HashMap<String, Map<String, Step>>();
private final ConcurrentMap<String, Map<String, Step>> map = new ConcurrentHashMap<String, Map<String, Step>>();
public void register(String jobName, Collection<Step> steps) {
Assert.notNull(jobName, "The job name cannot be null.");
@@ -27,38 +29,30 @@ public class MapStepRegistry implements StepRegistry {
unregisterStepsFromJob(jobName);
synchronized (this.map) {
final Map<String, Step> jobSteps = new HashMap<String, Step>();
for (Step step : steps) {
jobSteps.put(step.getName(), step);
}
this.map.put(jobName, jobSteps);
final Map<String, Step> jobSteps = new HashMap<String, Step>();
for (Step step : steps) {
jobSteps.put(step.getName(), step);
}
this.map.put(jobName, jobSteps);
}
public void unregisterStepsFromJob(String jobName) {
Assert.notNull(jobName, "Job configuration must have a name.");
synchronized (map) {
map.remove(jobName);
}
map.remove(jobName);
}
public Step getStep(String jobName, String stepName) throws NoSuchJobException {
Assert.notNull(jobName, "The job name cannot be null.");
Assert.notNull(stepName, "The step name cannot be null.");
synchronized (map) {
if (!map.containsKey(jobName)) {
throw new NoSuchJobException("No job configuration with the name [" + jobName + "] was registered");
if (!map.containsKey(jobName)) {
throw new NoSuchJobException("No job configuration with the name [" + jobName + "] was registered");
} else {
final Map<String, Step> jobSteps = map.get(jobName);
if (jobSteps.containsKey(stepName)) {
return jobSteps.get(stepName);
} else {
final Map<String, Step> jobSteps = map.get(jobName);
if (jobSteps.containsKey(stepName)) {
return jobSteps.get(stepName);
} else {
throw new NoSuchStepException("The step called [" + stepName + "] does not exist in the job [" +
jobName + "]");
}
throw new NoSuchStepException("The step called [" + stepName + "] does not exist in the job [" +
jobName + "]");
}
}
}