001/*
002 * Copyright 2006-2007 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.repeat.listener;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021
022import org.springframework.batch.repeat.RepeatStatus;
023import org.springframework.batch.repeat.RepeatContext;
024import org.springframework.batch.repeat.RepeatListener;
025
026/**
027 * Allows a user to register one or more RepeatListeners to be notified on batch events.
028 *
029 * @author Dave Syer
030 * 
031 */
032public class CompositeRepeatListener implements RepeatListener {
033
034        private List<RepeatListener> listeners = new ArrayList<RepeatListener>();
035
036        /**
037         * Public setter for the listeners.
038         * 
039         * @param listeners array of RepeatListeners to be used by the CompositeRepeatListener.
040         */
041        public void setListeners(RepeatListener[] listeners) {
042                this.listeners = Arrays.asList(listeners);
043        }
044
045        /**
046         * Register additional listener.
047         * 
048         * @param listener the RepeatListener to be added to the list of listeners to be notified.
049         */
050        public void register(RepeatListener listener) {
051                if (!listeners.contains(listener)) {
052                        listeners.add(listener);
053                }
054        }
055
056        /* (non-Javadoc)
057         * @see org.springframework.batch.repeat.RepeatListener#after(org.springframework.batch.repeat.RepeatContext, org.springframework.batch.repeat.ExitStatus)
058         */
059    @Override
060        public void after(RepeatContext context, RepeatStatus result) {
061                for (RepeatListener listener : listeners) {
062                        listener.after(context, result);
063                }
064        }
065
066        /* (non-Javadoc)
067         * @see org.springframework.batch.repeat.RepeatListener#before(org.springframework.batch.repeat.RepeatContext)
068         */
069    @Override
070        public void before(RepeatContext context) {
071                for (RepeatListener listener : listeners) {
072                        listener.before(context);
073                }
074        }
075
076        /* (non-Javadoc)
077         * @see org.springframework.batch.repeat.RepeatListener#close(org.springframework.batch.repeat.RepeatContext)
078         */
079    @Override
080        public void close(RepeatContext context) {
081                for (RepeatListener listener : listeners) {
082                        listener.close(context);
083                }
084        }
085
086        /* (non-Javadoc)
087         * @see org.springframework.batch.repeat.RepeatListener#onError(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)
088         */
089    @Override
090        public void onError(RepeatContext context, Throwable e) {
091                for (RepeatListener listener : listeners) {
092                        listener.onError(context, e);
093                }
094        }
095
096        /* (non-Javadoc)
097         * @see org.springframework.batch.repeat.RepeatListener#open(org.springframework.batch.repeat.RepeatContext)
098         */
099    @Override
100        public void open(RepeatContext context) {
101                for (RepeatListener listener : listeners) {
102                        listener.open(context);
103                }
104        }
105
106}