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 */
016
017package org.springframework.batch.core.explore.support;
018
019import org.springframework.batch.core.explore.JobExplorer;
020import org.springframework.batch.core.repository.dao.ExecutionContextDao;
021import org.springframework.batch.core.repository.dao.JobExecutionDao;
022import org.springframework.batch.core.repository.dao.JobInstanceDao;
023import org.springframework.batch.core.repository.dao.StepExecutionDao;
024import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
025import org.springframework.beans.factory.FactoryBean;
026import org.springframework.beans.factory.InitializingBean;
027import org.springframework.util.Assert;
028
029/**
030 * A {@link FactoryBean} that automates the creation of a
031 * {@link SimpleJobExplorer} using in-memory DAO implementations.
032 *
033 * @author Dave Syer
034 * @since 2.0
035 */
036public class MapJobExplorerFactoryBean extends AbstractJobExplorerFactoryBean implements InitializingBean {
037
038        private MapJobRepositoryFactoryBean repositoryFactory;
039
040        /**
041         * Create an instance with the provided {@link MapJobRepositoryFactoryBean}
042         * as a source of Dao instances.
043         * @param repositoryFactory provides the used {@link org.springframework.batch.core.repository.JobRepository}
044         */
045        public MapJobExplorerFactoryBean(MapJobRepositoryFactoryBean repositoryFactory) {
046                this.repositoryFactory = repositoryFactory;
047        }
048
049        /**
050         * Create a factory with no {@link MapJobRepositoryFactoryBean}. It must be
051         * injected as a property.
052         */
053        public MapJobExplorerFactoryBean() {
054        }
055
056        /**
057         * The repository factory that can be used to create daos for the explorer.
058         *
059         * @param repositoryFactory a {@link MapJobExplorerFactoryBean}
060         */
061        public void setRepositoryFactory(MapJobRepositoryFactoryBean repositoryFactory) {
062                this.repositoryFactory = repositoryFactory;
063        }
064
065        /**
066         * @throws Exception thrown if error occurs.
067         *
068         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
069         */
070        @Override
071        public void afterPropertiesSet() throws Exception {
072                Assert.state(repositoryFactory != null, "A MapJobRepositoryFactoryBean must be provided");
073                repositoryFactory.afterPropertiesSet();
074        }
075
076        @Override
077        protected JobExecutionDao createJobExecutionDao() throws Exception {
078                return repositoryFactory.getJobExecutionDao();
079        }
080
081        @Override
082        protected JobInstanceDao createJobInstanceDao() throws Exception {
083                return repositoryFactory.getJobInstanceDao();
084        }
085
086        @Override
087        protected StepExecutionDao createStepExecutionDao() throws Exception {
088                return repositoryFactory.getStepExecutionDao();
089        }
090
091        @Override
092        protected ExecutionContextDao createExecutionContextDao() throws Exception {
093                return repositoryFactory.getExecutionContextDao();
094        }
095
096        @Override
097        public JobExplorer getObject() throws Exception {
098                return new SimpleJobExplorer(createJobInstanceDao(), createJobExecutionDao(), createStepExecutionDao(),
099                                createExecutionContextDao());
100        }
101
102}