001/*
002 * Copyright 2006-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.configuration.xml;
017
018import org.springframework.batch.core.JobExecutionListener;
019import org.springframework.batch.core.JobParametersIncrementer;
020import org.springframework.batch.core.JobParametersValidator;
021import org.springframework.batch.core.job.flow.Flow;
022import org.springframework.batch.core.job.flow.FlowJob;
023import org.springframework.batch.core.repository.JobRepository;
024import org.springframework.beans.factory.FactoryBean;
025import org.springframework.beans.factory.SmartFactoryBean;
026import org.springframework.util.Assert;
027import org.springframework.util.StringUtils;
028
029/**
030 * This {@link FactoryBean} is used by the batch namespace parser to create
031 * {@link FlowJob} objects. It stores all of the properties that are
032 * configurable on the <job/>.
033 *
034 * @author Dan Garrette
035 * @author Dave Syer
036 * @since 2.0.1
037 */
038public class JobParserJobFactoryBean implements SmartFactoryBean<FlowJob> {
039
040        private String name;
041
042        private Boolean restartable;
043
044        private JobRepository jobRepository;
045
046        private JobParametersValidator jobParametersValidator;
047
048        private JobExecutionListener[] jobExecutionListeners;
049
050        private JobParametersIncrementer jobParametersIncrementer;
051
052        private Flow flow;
053
054        public JobParserJobFactoryBean(String name) {
055                this.name = name;
056        }
057
058        @Override
059        public final FlowJob getObject() throws Exception {
060                Assert.isTrue(StringUtils.hasText(name), "The job must have an id.");
061                FlowJob flowJob = new FlowJob(name);
062
063                if (restartable != null) {
064                        flowJob.setRestartable(restartable);
065                }
066
067                if (jobRepository != null) {
068                        flowJob.setJobRepository(jobRepository);
069                }
070
071                if (jobParametersValidator != null) {
072                        flowJob.setJobParametersValidator(jobParametersValidator);
073                }
074
075                if (jobExecutionListeners != null) {
076                        flowJob.setJobExecutionListeners(jobExecutionListeners);
077                }
078
079                if (jobParametersIncrementer != null) {
080                        flowJob.setJobParametersIncrementer(jobParametersIncrementer);
081                }
082
083                if (flow != null) {
084                        flowJob.setFlow(flow);
085                }
086
087                flowJob.afterPropertiesSet();
088                return flowJob;
089        }
090
091        public void setRestartable(Boolean restartable) {
092                this.restartable = restartable;
093        }
094
095        public void setJobRepository(JobRepository jobRepository) {
096                this.jobRepository = jobRepository;
097        }
098
099        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}