001/*
002 * Copyright 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.jsr.configuration.xml;
017
018import javax.batch.api.listener.JobListener;
019
020import org.springframework.batch.core.JobExecutionListener;
021import org.springframework.batch.core.JobParametersIncrementer;
022import org.springframework.batch.core.JobParametersValidator;
023import org.springframework.batch.core.explore.JobExplorer;
024import org.springframework.batch.core.job.flow.Flow;
025import org.springframework.batch.core.job.flow.FlowJob;
026import org.springframework.batch.core.jsr.JobListenerAdapter;
027import org.springframework.batch.core.jsr.job.flow.JsrFlowJob;
028import org.springframework.batch.core.repository.JobRepository;
029import org.springframework.beans.factory.FactoryBean;
030import org.springframework.beans.factory.SmartFactoryBean;
031import org.springframework.util.Assert;
032import org.springframework.util.StringUtils;
033
034/**
035 * This {@link FactoryBean} is used by the JSR-352 namespace parser to create
036 * {@link FlowJob} objects. It stores all of the properties that are
037 * configurable on the <job/>.
038 *
039 * @author Michael Minella
040 * @since 3.0
041 */
042public class JobFactoryBean implements SmartFactoryBean<FlowJob> {
043
044        private String name;
045
046        private Boolean restartable;
047
048        private JobRepository jobRepository;
049
050        private JobParametersValidator jobParametersValidator;
051
052        private JobExecutionListener[] jobExecutionListeners;
053
054        private JobParametersIncrementer jobParametersIncrementer;
055
056        private Flow flow;
057
058        private JobExplorer jobExplorer;
059
060        public JobFactoryBean(String name) {
061                this.name = name;
062        }
063
064        @Override
065        public final FlowJob getObject() throws Exception {
066                Assert.isTrue(StringUtils.hasText(name), "The job must have an id.");
067                JsrFlowJob flowJob = new JsrFlowJob(name);
068                flowJob.setJobExplorer(jobExplorer);
069
070                if (restartable != null) {
071                        flowJob.setRestartable(restartable);
072                }
073
074                if (jobRepository != null) {
075                        flowJob.setJobRepository(jobRepository);
076                }
077
078                if (jobParametersValidator != null) {
079                        flowJob.setJobParametersValidator(jobParametersValidator);
080                }
081
082                if (jobExecutionListeners != null) {
083                        flowJob.setJobExecutionListeners(jobExecutionListeners);
084                }
085
086                if (jobParametersIncrementer != null) {
087                        flowJob.setJobParametersIncrementer(jobParametersIncrementer);
088                }
089
090                if (flow != null) {
091                        flowJob.setFlow(flow);
092                }
093
094                flowJob.afterPropertiesSet();
095                return flowJob;
096        }
097
098        public void setJobExplorer(JobExplorer jobExplorer) {
099                this.jobExplorer = jobExplorer;
100        }
101
102        public void setRestartable(Boolean restartable) {
103                this.restartable = restartable;
104        }
105
106        public void setJobRepository(JobRepository jobRepository) {
107                this.jobRepository = jobRepository;
108        }
109
110        public void setJobParametersValidator(JobParametersValidator jobParametersValidator) {
111                this.jobParametersValidator = jobParametersValidator;
112        }
113
114        public JobRepository getJobRepository() {
115                return this.jobRepository;
116        }
117
118        public void setJobParametersIncrementer(JobParametersIncrementer jobParametersIncrementer) {
119                this.jobParametersIncrementer = jobParametersIncrementer;
120        }
121
122        public void setFlow(Flow flow) {
123                this.flow = flow;
124        }
125
126        @Override
127        public Class<FlowJob> getObjectType() {
128                return FlowJob.class;
129        }
130
131        @Override
132        public boolean isSingleton() {
133                return true;
134        }
135
136        @Override
137        public boolean isEagerInit() {
138                return true;
139        }
140
141        @Override
142        public boolean isPrototype() {
143                return false;
144        }
145
146        /**
147         * Addresses wrapping {@link JobListener} as needed to be used with
148         * the framework.
149         *
150         * @param jobListeners a list of all job listeners
151         */
152        public void setJobExecutionListeners(Object[] jobListeners) {
153                if(jobListeners != null) {
154                        JobExecutionListener[] listeners = new JobExecutionListener[jobListeners.length];
155
156                        for(int i = 0; i < jobListeners.length; i++) {
157                                Object curListener = jobListeners[i];
158                                if(curListener instanceof JobExecutionListener) {
159                                        listeners[i] = (JobExecutionListener) curListener;
160                                } else if(curListener instanceof JobListener){
161                                        listeners[i] = new JobListenerAdapter((JobListener) curListener);
162                                }
163                        }
164
165                        this.jobExecutionListeners = listeners;
166                }
167        }
168}