001/*
002 * Copyright 2012-2018 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.annotation;
017
018import javax.annotation.PostConstruct;
019import javax.sql.DataSource;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024import org.springframework.batch.core.configuration.BatchConfigurationException;
025import org.springframework.batch.core.explore.JobExplorer;
026import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
027import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
028import org.springframework.batch.core.launch.JobLauncher;
029import org.springframework.batch.core.launch.support.SimpleJobLauncher;
030import org.springframework.batch.core.repository.JobRepository;
031import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
032import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
033import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
034import org.springframework.beans.factory.annotation.Autowired;
035import org.springframework.jdbc.datasource.DataSourceTransactionManager;
036import org.springframework.stereotype.Component;
037import org.springframework.transaction.PlatformTransactionManager;
038
039@Component
040public class DefaultBatchConfigurer implements BatchConfigurer {
041        private static final Log logger = LogFactory.getLog(DefaultBatchConfigurer.class);
042
043        private DataSource dataSource;
044        private PlatformTransactionManager transactionManager;
045        private JobRepository jobRepository;
046        private JobLauncher jobLauncher;
047        private JobExplorer jobExplorer;
048
049        /**
050         * Sets the dataSource.  If the {@link DataSource} has been set once, all future
051         * values are passed are ignored (to prevent {@code}@Autowired{@code} from overwriting
052         * the value).
053         *
054         * @param dataSource
055         */
056        @Autowired(required = false)
057        public void setDataSource(DataSource dataSource) {
058                if(this.dataSource == null) {
059                        this.dataSource = dataSource;
060                }
061
062                if(getTransactionManager() == null) {
063                        logger.warn("No transaction manager was provided, using a DataSourceTransactionManager");
064                        this.transactionManager = new DataSourceTransactionManager(this.dataSource);
065                }
066        }
067
068        protected DefaultBatchConfigurer() {}
069
070        public DefaultBatchConfigurer(DataSource dataSource) {
071                setDataSource(dataSource);
072        }
073
074        @Override
075        public JobRepository getJobRepository() {
076                return jobRepository;
077        }
078
079        @Override
080        public PlatformTransactionManager getTransactionManager() {
081                return transactionManager;
082        }
083
084        @Override
085        public JobLauncher getJobLauncher() {
086                return jobLauncher;
087        }
088
089        @Override
090        public JobExplorer getJobExplorer() {
091                return jobExplorer;
092        }
093
094        @PostConstruct
095        public void initialize() {
096                try {
097                        if(dataSource == null) {
098                                logger.warn("No datasource was provided...using a Map based JobRepository");
099
100                                if(getTransactionManager() == null) {
101                                        logger.warn("No transaction manager was provided, using a ResourcelessTransactionManager");
102                                        this.transactionManager = new ResourcelessTransactionManager();
103                                }
104
105                                MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(getTransactionManager());
106                                jobRepositoryFactory.afterPropertiesSet();
107                                this.jobRepository = jobRepositoryFactory.getObject();
108
109                                MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
110                                jobExplorerFactory.afterPropertiesSet();
111                                this.jobExplorer = jobExplorerFactory.getObject();
112                        } else {
113                                this.jobRepository = createJobRepository();
114                                this.jobExplorer = createJobExplorer();
115                        }
116
117                        this.jobLauncher = createJobLauncher();
118                } catch (Exception e) {
119                        throw new BatchConfigurationException(e);
120                }
121        }
122
123        protected JobLauncher createJobLauncher() throws Exception {
124                SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
125                jobLauncher.setJobRepository(jobRepository);
126                jobLauncher.afterPropertiesSet();
127                return jobLauncher;
128        }
129
130        protected JobRepository createJobRepository() throws Exception {
131                JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
132                factory.setDataSource(dataSource);
133                factory.setTransactionManager(getTransactionManager());
134                factory.afterPropertiesSet();
135                return factory.getObject();
136        }
137
138        protected JobExplorer createJobExplorer() throws Exception {
139                JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
140                jobExplorerFactoryBean.setDataSource(this.dataSource);
141                jobExplorerFactoryBean.afterPropertiesSet();
142                return jobExplorerFactoryBean.getObject();
143        }
144}