1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.core.configuration.support;
17
18 import java.util.Collection;
19 import java.util.HashSet;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.springframework.batch.core.Job;
24 import org.springframework.batch.core.configuration.DuplicateJobException;
25 import org.springframework.batch.core.configuration.JobLocator;
26 import org.springframework.batch.core.configuration.JobRegistry;
27 import org.springframework.beans.BeansException;
28 import org.springframework.beans.FatalBeanException;
29 import org.springframework.beans.factory.BeanFactory;
30 import org.springframework.beans.factory.BeanFactoryAware;
31 import org.springframework.beans.factory.DisposableBean;
32 import org.springframework.beans.factory.InitializingBean;
33 import org.springframework.beans.factory.config.BeanDefinition;
34 import org.springframework.beans.factory.config.BeanPostProcessor;
35 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
36 import org.springframework.util.Assert;
37
38
39
40
41
42
43
44
45
46
47 public class JobRegistryBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware, InitializingBean,
48 DisposableBean {
49
50 private static Log logger = LogFactory.getLog(JobRegistryBeanPostProcessor.class);
51
52
53 private JobRegistry jobRegistry = null;
54
55 private Collection<String> jobNames = new HashSet<String>();
56
57 private String groupName = null;
58
59 private DefaultListableBeanFactory beanFactory;
60
61
62
63
64
65
66
67
68
69
70
71 public void setGroupName(String groupName) {
72 this.groupName = groupName;
73 }
74
75
76
77
78
79
80 public void setJobRegistry(JobRegistry jobRegistry) {
81 this.jobRegistry = jobRegistry;
82 }
83
84
85
86
87
88
89
90
91 @Override
92 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
93 if (beanFactory instanceof DefaultListableBeanFactory) {
94 this.beanFactory = (DefaultListableBeanFactory) beanFactory;
95 }
96 }
97
98
99
100
101
102
103 @Override
104 public void afterPropertiesSet() throws Exception {
105 Assert.notNull(jobRegistry, "JobRegistry must not be null");
106 }
107
108
109
110
111
112
113 @Override
114 public void destroy() throws Exception {
115 for (String name : jobNames) {
116 logger.debug("Unregistering job: " + name);
117 jobRegistry.unregister(name);
118 }
119 jobNames.clear();
120 }
121
122
123
124
125
126
127
128
129 @Override
130 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
131 if (bean instanceof Job) {
132 Job job = (Job) bean;
133 try {
134 String groupName = this.groupName;
135 if (beanFactory != null && beanFactory.containsBean(beanName)) {
136 groupName = getGroupName(beanFactory.getBeanDefinition(beanName), job);
137 }
138 job = groupName==null ? job : new GroupAwareJob(groupName, job);
139 ReferenceJobFactory jobFactory = new ReferenceJobFactory(job);
140 String name = jobFactory.getJobName();
141 logger.debug("Registering job: " + name);
142 jobRegistry.register(jobFactory);
143 jobNames.add(name);
144 }
145 catch (DuplicateJobException e) {
146 throw new FatalBeanException("Cannot register job configuration", e);
147 }
148 return job;
149 }
150 return bean;
151 }
152
153
154
155
156
157
158
159
160
161
162 protected String getGroupName(BeanDefinition beanDefinition, Job job) {
163 return groupName;
164 }
165
166
167
168
169
170
171
172 @Override
173 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
174 return bean;
175 }
176 }