001/*
002 * Copyright 2002-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.listener;
017
018import org.springframework.batch.core.StepListener;
019
020/**
021 * This {@link AbstractListenerFactoryBean} implementation is used to create a
022 * {@link StepListener}.
023 *
024 * @author Lucas Ward
025 * @author Dan Garrette
026 * @since 2.0
027 * @see AbstractListenerFactoryBean
028 * @see StepListenerMetaData
029 */
030public class StepListenerFactoryBean extends AbstractListenerFactoryBean<StepListener> {
031
032        @Override
033        protected ListenerMetaData getMetaDataFromPropertyName(String propertyName) {
034                return StepListenerMetaData.fromPropertyName(propertyName);
035        }
036
037        @Override
038        protected ListenerMetaData[] getMetaDataValues() {
039                return StepListenerMetaData.values();
040        }
041
042        @Override
043        protected Class<?> getDefaultListenerClass() {
044                return StepListener.class;
045        }
046
047        @Override
048        public Class<StepListener> getObjectType() {
049                return StepListener.class;
050        }
051
052        /**
053         * Convenience method to wrap any object and expose the appropriate
054         * {@link StepListener} interfaces.
055         *
056         * @param delegate a delegate object
057         * @return a StepListener instance constructed from the delegate
058         */
059        public static StepListener getListener(Object delegate) {
060                StepListenerFactoryBean factory = new StepListenerFactoryBean();
061                factory.setDelegate(delegate);
062                return (StepListener) factory.getObject();
063        }
064
065        /**
066         * Convenience method to check whether the given object is or can be made
067         * into a {@link StepListener}.
068         *
069         * @param delegate the object to check
070         * @return true if the delegate is an instance of any of the
071         *         {@link StepListener} interfaces, or contains the marker
072         *         annotations
073         */
074        public static boolean isListener(Object delegate) {
075                return AbstractListenerFactoryBean.isListener(delegate, StepListener.class, StepListenerMetaData.values());
076        }
077}