001/*
002 * Copyright 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 */
016
017package org.springframework.batch.integration.partition;
018
019import org.springframework.batch.core.explore.JobExplorer;
020import org.springframework.batch.core.repository.JobRepository;
021import org.springframework.beans.BeansException;
022import org.springframework.beans.factory.BeanFactory;
023import org.springframework.beans.factory.BeanFactoryAware;
024import org.springframework.transaction.PlatformTransactionManager;
025
026/**
027 * Convenient factory for a {@link RemotePartitioningWorkerStepBuilder} which sets
028 * the {@link JobRepository}, {@link JobExplorer}, {@link BeanFactory} and
029 * {@link PlatformTransactionManager} automatically.
030 *
031 * @since 4.1
032 * @author Mahmoud Ben Hassine
033 */
034public class RemotePartitioningWorkerStepBuilderFactory implements BeanFactoryAware {
035
036        private BeanFactory beanFactory;
037        final private JobExplorer jobExplorer;
038        final private JobRepository jobRepository;
039        final private PlatformTransactionManager transactionManager;
040
041
042        /**
043         * Create a new {@link RemotePartitioningWorkerStepBuilderFactory}.
044         * @param jobRepository the job repository to use
045         * @param jobExplorer the job explorer to use
046         * @param transactionManager the transaction manager to use
047         */
048        public RemotePartitioningWorkerStepBuilderFactory(JobRepository jobRepository,
049                JobExplorer jobExplorer,
050                PlatformTransactionManager transactionManager) {
051
052                this.jobExplorer = jobExplorer;
053                this.jobRepository = jobRepository;
054                this.transactionManager = transactionManager;
055        }
056
057        @Override
058        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
059                this.beanFactory = beanFactory;
060        }
061
062        /**
063         * Creates a {@link RemotePartitioningWorkerStepBuilder} and initializes its job
064         * repository, job explorer, bean factory and transaction manager.
065         * @param name the name of the step
066         * @return a {@link RemotePartitioningWorkerStepBuilder}
067         */
068        public RemotePartitioningWorkerStepBuilder get(String name) {
069                return new RemotePartitioningWorkerStepBuilder(name)
070                                .repository(this.jobRepository)
071                                .jobExplorer(this.jobExplorer)
072                                .beanFactory(this.beanFactory)
073                                .transactionManager(this.transactionManager);
074        }
075
076}