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 */
016package org.springframework.batch.core.listener;
017
018import java.util.Iterator;
019import java.util.List;
020
021import org.springframework.batch.core.ChunkListener;
022import org.springframework.batch.core.scope.context.ChunkContext;
023import org.springframework.core.Ordered;
024
025/**
026 * @author Lucas Ward
027 *
028 */
029public class CompositeChunkListener implements ChunkListener {
030
031        private OrderedComposite<ChunkListener> listeners = new OrderedComposite<ChunkListener>();
032
033        /**
034         * Public setter for the listeners.
035         *
036         * @param listeners list of {@link ChunkListener}.
037         */
038        public void setListeners(List<? extends ChunkListener> listeners) {
039                this.listeners.setItems(listeners);
040        }
041
042        /**
043         * Register additional listener.
044         *
045         * @param chunkListener instance of {@link ChunkListener}.
046         */
047        public void register(ChunkListener chunkListener) {
048                listeners.add(chunkListener);
049        }
050
051        /**
052         * Call the registered listeners in reverse order.
053         *
054         * @see org.springframework.batch.core.ChunkListener#afterChunk(ChunkContext context)
055         */
056        @Override
057        public void afterChunk(ChunkContext context) {
058                for (Iterator<ChunkListener> iterator = listeners.reverse(); iterator.hasNext();) {
059                        ChunkListener listener = iterator.next();
060                        listener.afterChunk(context);
061                }
062        }
063
064        /**
065         * Call the registered listeners in order, respecting and prioritizing those
066         * that implement {@link Ordered}.
067         *
068         * @see org.springframework.batch.core.ChunkListener#beforeChunk(ChunkContext context)
069         */
070        @Override
071        public void beforeChunk(ChunkContext context) {
072                for (Iterator<ChunkListener> iterator = listeners.iterator(); iterator.hasNext();) {
073                        ChunkListener listener = iterator.next();
074                        listener.beforeChunk(context);
075                }
076        }
077
078        /**
079         * Call the registered listeners in reverse order.
080         *
081         * @see org.springframework.batch.core.ChunkListener#afterChunkError(ChunkContext context)
082         */
083        @Override
084        public void afterChunkError(ChunkContext context) {
085                for (Iterator<ChunkListener> iterator = listeners.reverse(); iterator.hasNext();) {
086                        ChunkListener listener = iterator.next();
087                        listener.afterChunkError(context);
088                }
089        }
090}