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