001package org.springframework.batch.integration.partition;
002
003import java.util.Arrays;
004import java.util.Collection;
005
006import org.springframework.batch.core.Step;
007import org.springframework.batch.core.step.StepLocator;
008import org.springframework.beans.BeansException;
009import org.springframework.beans.factory.BeanFactory;
010import org.springframework.beans.factory.BeanFactoryAware;
011import org.springframework.beans.factory.ListableBeanFactory;
012import org.springframework.util.Assert;
013
014/**
015 * A {@link StepLocator} implementation that just looks in its enclosing bean
016 * factory for components of type {@link Step}.
017 * 
018 * @author Dave Syer
019 * 
020 */
021public class BeanFactoryStepLocator implements StepLocator, BeanFactoryAware {
022
023        private BeanFactory beanFactory;
024
025        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
026                this.beanFactory = beanFactory;
027        }
028
029        /**
030         * Look up a bean with the provided name of type {@link Step}.
031         * @see StepLocator#getStep(String)
032         */
033        public Step getStep(String stepName) {
034                return beanFactory.getBean(stepName, Step.class);
035        }
036
037        /**
038         * Look in the bean factory for all beans of type {@link Step}.
039         * @throws IllegalStateException if the {@link BeanFactory} is not listable
040         * @see StepLocator#getStepNames()
041         */
042        public Collection<String> getStepNames() {
043                Assert.state(beanFactory instanceof ListableBeanFactory, "BeanFactory is not listable.");
044                return Arrays.asList(((ListableBeanFactory) beanFactory).getBeanNamesForType(Step.class));
045        }
046
047}