| 1 | /* |
| 2 | * Copyright 2006-2007 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.Collections; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import org.springframework.batch.core.Job; |
| 25 | import org.springframework.batch.core.configuration.JobFactory; |
| 26 | import org.springframework.batch.core.configuration.JobRegistry; |
| 27 | import org.springframework.batch.core.configuration.ListableJobRegistry; |
| 28 | import org.springframework.batch.core.repository.DuplicateJobException; |
| 29 | import org.springframework.batch.core.repository.NoSuchJobException; |
| 30 | import org.springframework.util.Assert; |
| 31 | |
| 32 | /** |
| 33 | * Simple map-based implementation of {@link JobRegistry}. Access to the map is |
| 34 | * synchronized, guarded by an internal lock. |
| 35 | * |
| 36 | * @author Dave Syer |
| 37 | * |
| 38 | */ |
| 39 | public class MapJobRegistry implements ListableJobRegistry { |
| 40 | |
| 41 | private Map map = new HashMap(); |
| 42 | |
| 43 | /* |
| 44 | * (non-Javadoc) |
| 45 | * @see org.springframework.batch.container.common.configuration.JobConfigurationRegistry#registerJobConfiguration(org.springframework.batch.container.common.configuration.JobConfiguration) |
| 46 | */ |
| 47 | public void register(JobFactory jobFactory) throws DuplicateJobException { |
| 48 | Assert.notNull(jobFactory); |
| 49 | String name = jobFactory.getJobName(); |
| 50 | Assert.notNull(name, "Job configuration must have a name."); |
| 51 | synchronized (map) { |
| 52 | if (map.containsKey(name)) { |
| 53 | throw new DuplicateJobException("A job configuration with this name [" + name |
| 54 | + "] was already registered"); |
| 55 | } |
| 56 | map.put(name, jobFactory); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * (non-Javadoc) |
| 62 | * @see org.springframework.batch.container.common.configuration.JobConfigurationRegistry#unregister(org.springframework.batch.container.common.configuration.JobConfiguration) |
| 63 | */ |
| 64 | public void unregister(String name) { |
| 65 | Assert.notNull(name, "Job configuration must have a name."); |
| 66 | synchronized (map) { |
| 67 | map.remove(name); |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * (non-Javadoc) |
| 74 | * @see org.springframework.batch.container.common.configuration.JobConfigurationLocator#getJobConfiguration(java.lang.String) |
| 75 | */ |
| 76 | public Job getJob(String name) throws NoSuchJobException { |
| 77 | synchronized (map) { |
| 78 | if (!map.containsKey(name)) { |
| 79 | throw new NoSuchJobException("No job configuration with the name [" + name + "] was registered"); |
| 80 | } |
| 81 | return (Job) ((JobFactory) map.get(name)).createJob(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * (non-Javadoc) |
| 87 | * @see org.springframework.batch.container.common.configuration.ListableJobConfigurationRegistry#getJobConfigurations() |
| 88 | */ |
| 89 | public Collection getJobNames() { |
| 90 | synchronized (map) { |
| 91 | return Collections.unmodifiableCollection(new HashSet(map.keySet())); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } |