001/* 002 * Copyright 2012-2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.core.configuration.support; 017 018import java.util.Collection; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.concurrent.ConcurrentHashMap; 022import java.util.concurrent.ConcurrentMap; 023 024import org.springframework.batch.core.Step; 025import org.springframework.batch.core.configuration.DuplicateJobException; 026import org.springframework.batch.core.configuration.StepRegistry; 027import org.springframework.batch.core.launch.NoSuchJobException; 028import org.springframework.batch.core.step.NoSuchStepException; 029import org.springframework.util.Assert; 030 031/** 032 * Simple map-based implementation of {@link StepRegistry}. Access to the map is 033 * synchronized, guarded by an internal lock. 034 * 035 * @author Sebastien Gerard 036 * @author Stephane Nicoll 037 */ 038public class MapStepRegistry implements StepRegistry { 039 040 private final ConcurrentMap<String, Map<String, Step>> map = new ConcurrentHashMap<String, Map<String, Step>>(); 041 042 @Override 043 public void register(String jobName, Collection<Step> steps) throws DuplicateJobException { 044 Assert.notNull(jobName, "The job name cannot be null."); 045 Assert.notNull(steps, "The job steps cannot be null."); 046 047 048 final Map<String, Step> jobSteps = new HashMap<String, Step>(); 049 for (Step step : steps) { 050 jobSteps.put(step.getName(), step); 051 } 052 final Object previousValue = map.putIfAbsent(jobName, jobSteps); 053 if (previousValue != null) { 054 throw new DuplicateJobException("A job configuration with this name [" + jobName 055 + "] was already registered"); 056 } 057 } 058 059 @Override 060 public void unregisterStepsFromJob(String jobName) { 061 Assert.notNull(jobName, "Job configuration must have a name."); 062 map.remove(jobName); 063 } 064 065 @Override 066 public Step getStep(String jobName, String stepName) throws NoSuchJobException { 067 Assert.notNull(jobName, "The job name cannot be null."); 068 Assert.notNull(stepName, "The step name cannot be null."); 069 if (!map.containsKey(jobName)) { 070 throw new NoSuchJobException("No job configuration with the name [" + jobName + "] was registered"); 071 } else { 072 final Map<String, Step> jobSteps = map.get(jobName); 073 if (jobSteps.containsKey(stepName)) { 074 return jobSteps.get(stepName); 075 } else { 076 throw new NoSuchStepException("The step called [" + stepName + "] does not exist in the job [" + 077 jobName + "]"); 078 } 079 } 080 } 081 082}