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