1 | /* |
2 | * Copyright 2006-2013 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.xml; |
17 | |
18 | import org.springframework.batch.core.JobExecutionListener; |
19 | import org.springframework.batch.core.JobParametersIncrementer; |
20 | import org.springframework.batch.core.JobParametersValidator; |
21 | import org.springframework.batch.core.job.flow.Flow; |
22 | import org.springframework.batch.core.job.flow.FlowJob; |
23 | import org.springframework.batch.core.repository.JobRepository; |
24 | import org.springframework.beans.factory.FactoryBean; |
25 | import org.springframework.beans.factory.SmartFactoryBean; |
26 | import org.springframework.util.Assert; |
27 | import org.springframework.util.StringUtils; |
28 | |
29 | /** |
30 | * This {@link FactoryBean} is used by the batch namespace parser to create |
31 | * {@link FlowJob} objects. It stores all of the properties that are |
32 | * configurable on the <job/>. |
33 | * |
34 | * @author Dan Garrette |
35 | * @author Dave Syer |
36 | * @since 2.0.1 |
37 | */ |
38 | class JobParserJobFactoryBean implements SmartFactoryBean { |
39 | |
40 | private String name; |
41 | |
42 | private Boolean restartable; |
43 | |
44 | private JobRepository jobRepository; |
45 | |
46 | private JobParametersValidator jobParametersValidator; |
47 | |
48 | private JobExecutionListener[] jobExecutionListeners; |
49 | |
50 | private JobParametersIncrementer jobParametersIncrementer; |
51 | |
52 | private Flow flow; |
53 | |
54 | public JobParserJobFactoryBean(String name) { |
55 | this.name = name; |
56 | } |
57 | |
58 | @Override |
59 | public final Object getObject() throws Exception { |
60 | Assert.isTrue(StringUtils.hasText(name), "The job must have an id."); |
61 | FlowJob flowJob = new FlowJob(name); |
62 | |
63 | if (restartable != null) { |
64 | flowJob.setRestartable(restartable); |
65 | } |
66 | |
67 | if (jobRepository != null) { |
68 | flowJob.setJobRepository(jobRepository); |
69 | } |
70 | |
71 | if (jobParametersValidator != null) { |
72 | flowJob.setJobParametersValidator(jobParametersValidator); |
73 | } |
74 | |
75 | if (jobExecutionListeners != null) { |
76 | flowJob.setJobExecutionListeners(jobExecutionListeners); |
77 | } |
78 | |
79 | if (jobParametersIncrementer != null) { |
80 | flowJob.setJobParametersIncrementer(jobParametersIncrementer); |
81 | } |
82 | |
83 | if (flow != null) { |
84 | flowJob.setFlow(flow); |
85 | } |
86 | |
87 | flowJob.afterPropertiesSet(); |
88 | return flowJob; |
89 | } |
90 | |
91 | public void setRestartable(Boolean restartable) { |
92 | this.restartable = restartable; |
93 | } |
94 | |
95 | public void setJobRepository(JobRepository jobRepository) { |
96 | this.jobRepository = jobRepository; |
97 | } |
98 | |
99 | public void setJobParametersValidator(JobParametersValidator jobParametersValidator) { |
100 | this.jobParametersValidator = jobParametersValidator; |
101 | } |
102 | |
103 | public JobRepository getJobRepository() { |
104 | return this.jobRepository; |
105 | } |
106 | |
107 | public void setJobExecutionListeners(JobExecutionListener[] jobExecutionListeners) { |
108 | this.jobExecutionListeners = jobExecutionListeners; |
109 | } |
110 | |
111 | public void setJobParametersIncrementer(JobParametersIncrementer jobParametersIncrementer) { |
112 | this.jobParametersIncrementer = jobParametersIncrementer; |
113 | } |
114 | |
115 | public void setFlow(Flow flow) { |
116 | this.flow = flow; |
117 | } |
118 | |
119 | @Override |
120 | public Class<FlowJob> getObjectType() { |
121 | return FlowJob.class; |
122 | } |
123 | |
124 | @Override |
125 | public boolean isSingleton() { |
126 | return true; |
127 | } |
128 | |
129 | @Override |
130 | public boolean isEagerInit() { |
131 | return true; |
132 | } |
133 | |
134 | @Override |
135 | public boolean isPrototype() { |
136 | return false; |
137 | } |
138 | |
139 | } |