| 1 | /* |
| 2 | * Copyright 2006-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.Collections; |
| 19 | import java.util.Set; |
| 20 | import java.util.concurrent.ConcurrentHashMap; |
| 21 | import java.util.concurrent.ConcurrentMap; |
| 22 | |
| 23 | import org.springframework.batch.core.Job; |
| 24 | import org.springframework.batch.core.configuration.DuplicateJobException; |
| 25 | import org.springframework.batch.core.configuration.JobFactory; |
| 26 | import org.springframework.batch.core.configuration.JobRegistry; |
| 27 | import org.springframework.batch.core.launch.NoSuchJobException; |
| 28 | import org.springframework.util.Assert; |
| 29 | |
| 30 | /** |
| 31 | * Simple, thread-safe, map-based implementation of {@link JobRegistry}. |
| 32 | * |
| 33 | * @author Dave Syer |
| 34 | * @author Robert Fischer |
| 35 | */ |
| 36 | public class MapJobRegistry implements JobRegistry { |
| 37 | |
| 38 | /** |
| 39 | * The map holding the registered job factories. |
| 40 | */ |
| 41 | // The "final" ensures that it is visible and initialized when the constructor resolves. |
| 42 | private final ConcurrentMap<String, JobFactory> map = new ConcurrentHashMap<String, JobFactory>(); |
| 43 | |
| 44 | @Override |
| 45 | public void register(JobFactory jobFactory) throws DuplicateJobException { |
| 46 | Assert.notNull(jobFactory); |
| 47 | String name = jobFactory.getJobName(); |
| 48 | Assert.notNull(name, "Job configuration must have a name."); |
| 49 | JobFactory previousValue = map.putIfAbsent(name, jobFactory); |
| 50 | if (previousValue != null) { |
| 51 | throw new DuplicateJobException("A job configuration with this name [" + name |
| 52 | + "] was already registered"); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void unregister(String name) { |
| 58 | Assert.notNull(name, "Job configuration must have a name."); |
| 59 | map.remove(name); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Job getJob(String name) throws NoSuchJobException { |
| 64 | JobFactory factory = map.get(name); |
| 65 | if (factory == null) { |
| 66 | throw new NoSuchJobException("No job configuration with the name [" + name + "] was registered"); |
| 67 | } else { |
| 68 | return factory.createJob(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Provides an unmodifiable view of the job names. |
| 74 | */ |
| 75 | @Override |
| 76 | public Set<String> getJobNames() { |
| 77 | return Collections.unmodifiableSet(map.keySet()); |
| 78 | } |
| 79 | |
| 80 | } |