001/*
002 * Copyright 2006-2018 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.Collections;
019import java.util.Set;
020import java.util.concurrent.ConcurrentHashMap;
021import java.util.concurrent.ConcurrentMap;
022
023import org.springframework.batch.core.Job;
024import org.springframework.batch.core.configuration.DuplicateJobException;
025import org.springframework.batch.core.configuration.JobFactory;
026import org.springframework.batch.core.configuration.JobRegistry;
027import org.springframework.batch.core.launch.NoSuchJobException;
028import org.springframework.util.Assert;
029
030/**
031 * Simple, thread-safe, map-based implementation of {@link JobRegistry}.
032 *
033 * @author Dave Syer
034 * @author Robert Fischer
035 * @author Mahmoud Ben Hassine
036 */
037public class MapJobRegistry implements JobRegistry {
038
039        /**
040         * The map holding the registered job factories.
041         */
042        // The "final" ensures that it is visible and initialized when the constructor resolves.
043        private final ConcurrentMap<String, JobFactory> map = new ConcurrentHashMap<String, JobFactory>();
044
045        @Override
046        public void register(JobFactory jobFactory) throws DuplicateJobException {
047                Assert.notNull(jobFactory, "jobFactory is null");
048                String name = jobFactory.getJobName();
049                Assert.notNull(name, "Job configuration must have a name.");
050                JobFactory previousValue = map.putIfAbsent(name, jobFactory);
051                if (previousValue != null) {
052                        throw new DuplicateJobException("A job configuration with this name [" + name
053                                        + "] was already registered");
054                }
055        }
056
057        @Override
058        public void unregister(String name) {
059                Assert.notNull(name, "Job configuration must have a name.");
060                map.remove(name);
061        }
062
063        @Override
064        public Job getJob(String name) throws NoSuchJobException {
065                JobFactory factory = map.get(name);
066                if (factory == null) {
067                        throw new NoSuchJobException("No job configuration with the name [" + name + "] was registered");
068                } else {
069                        return factory.createJob();
070                }
071        }
072
073        /**
074         * Provides an unmodifiable view of the job names.
075         */
076        @Override
077        public Set<String> getJobNames() {
078                return Collections.unmodifiableSet(map.keySet());
079        }
080
081}