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