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.HashSet; |
20 | |
21 | import org.springframework.batch.core.Job; |
22 | import org.springframework.batch.core.configuration.DuplicateJobException; |
23 | import org.springframework.batch.core.configuration.JobLocator; |
24 | import org.springframework.batch.core.configuration.JobRegistry; |
25 | import org.springframework.beans.BeansException; |
26 | import org.springframework.beans.FatalBeanException; |
27 | import org.springframework.beans.factory.DisposableBean; |
28 | import org.springframework.beans.factory.InitializingBean; |
29 | import org.springframework.beans.factory.config.BeanPostProcessor; |
30 | import org.springframework.util.Assert; |
31 | |
32 | /** |
33 | * A {@link BeanPostProcessor} that registers {@link Job} beans |
34 | * with a {@link JobRegistry}. Include a bean of this type along |
35 | * with your job configuration, and use the same |
36 | * {@link JobRegistry} as a {@link JobLocator} when |
37 | * you need to locate a {@link JobLocator} to launch. |
38 | * |
39 | * @author Dave Syer |
40 | * |
41 | */ |
42 | public class JobRegistryBeanPostProcessor implements BeanPostProcessor, InitializingBean, DisposableBean { |
43 | |
44 | // It doesn't make sense for this to have a default value... |
45 | private JobRegistry jobConfigurationRegistry = null; |
46 | |
47 | private Collection<String> jobNames = new HashSet<String>(); |
48 | |
49 | /** |
50 | * Injection setter for {@link JobRegistry}. |
51 | * |
52 | * @param jobRegistry the jobConfigurationRegistry to set |
53 | */ |
54 | public void setJobRegistry(JobRegistry jobRegistry) { |
55 | this.jobConfigurationRegistry = jobRegistry; |
56 | } |
57 | |
58 | /** |
59 | * Make sure the registry is set before use. |
60 | * |
61 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
62 | */ |
63 | public void afterPropertiesSet() throws Exception { |
64 | Assert.notNull(jobConfigurationRegistry, "JobConfigurationRegistry must not be null"); |
65 | } |
66 | |
67 | /** |
68 | * De-register all the {@link Job} instances that were |
69 | * regsistered by this post processor. |
70 | * @see org.springframework.beans.factory.DisposableBean#destroy() |
71 | */ |
72 | public void destroy() throws Exception { |
73 | for (String name : jobNames) { |
74 | jobConfigurationRegistry.unregister(name); |
75 | } |
76 | jobNames.clear(); |
77 | } |
78 | |
79 | /** |
80 | * If the bean is an instance of {@link Job} then register it. |
81 | * @throws FatalBeanException if there is a |
82 | * {@link DuplicateJobException}. |
83 | * |
84 | * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, |
85 | * java.lang.String) |
86 | */ |
87 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
88 | if (bean instanceof Job) { |
89 | Job job = (Job) bean; |
90 | try { |
91 | jobConfigurationRegistry.register(new ReferenceJobFactory(job)); |
92 | jobNames.add(job.getName()); |
93 | } |
94 | catch (DuplicateJobException e) { |
95 | throw new FatalBeanException("Cannot register job configuration", e); |
96 | } |
97 | } |
98 | return bean; |
99 | } |
100 | |
101 | /** |
102 | * Do nothing. |
103 | * |
104 | * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, |
105 | * java.lang.String) |
106 | */ |
107 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
108 | return bean; |
109 | } |
110 | |
111 | } |