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 */
016
017package org.springframework.batch.item.support;
018
019import org.springframework.batch.item.ExecutionContext;
020import org.springframework.batch.item.ItemStream;
021import org.springframework.batch.item.ItemStreamException;
022import org.springframework.batch.item.ItemStreamWriter;
023import org.springframework.batch.item.ItemWriter;
024import org.springframework.beans.factory.InitializingBean;
025import org.springframework.util.Assert;
026
027import java.util.List;
028
029/**
030 * Calls a collection of {@link ItemWriter}s in fixed-order sequence.<br>
031 * <br>
032 * 
033 * The implementation is thread-safe if all delegates are thread-safe.
034 * 
035 * @author Robert Kasanicky
036 * @author Dave Syer
037 */
038public class CompositeItemWriter<T> implements ItemStreamWriter<T>, InitializingBean {
039
040        private List<ItemWriter<? super T>> delegates;
041
042        private boolean ignoreItemStream = false;
043
044        /**
045         * Establishes the policy whether to call the open, close, or update methods for the
046         * item writer delegates associated with the CompositeItemWriter.
047         * 
048         * @param ignoreItemStream if false the delegates' open, close, or update methods will
049         * be called when the corresponding methods on the CompositeItemWriter are called. If
050         * true the delegates' open, close, nor update methods will not be called (default is false).
051         */
052        public void setIgnoreItemStream(boolean ignoreItemStream) {
053                this.ignoreItemStream = ignoreItemStream;
054        }
055
056    @Override
057        public void write(List<? extends T> item) throws Exception {
058                for (ItemWriter<? super T> writer : delegates) {
059                        writer.write(item);
060                }
061        }
062
063    @Override
064        public void afterPropertiesSet() throws Exception {
065                Assert.notNull(delegates, "The 'delegates' may not be null");
066                Assert.notEmpty(delegates, "The 'delegates' may not be empty");
067        }
068
069        /**
070         * The list of item writers to use as delegates. Items are written to each of the
071         * delegates.
072         *
073         * @param delegates the list of delegates to use.  The delegates list must not be null nor be empty.
074         */
075        public void setDelegates(List<ItemWriter<? super T>> delegates) {
076                this.delegates = delegates;
077        }
078
079    @Override
080        public void close() throws ItemStreamException {
081                for (ItemWriter<? super T> writer : delegates) {
082                        if (!ignoreItemStream && (writer instanceof ItemStream)) {
083                                ((ItemStream) writer).close();
084                        }
085                }
086        }
087
088    @Override
089        public void open(ExecutionContext executionContext) throws ItemStreamException {
090                for (ItemWriter<? super T> writer : delegates) {
091                        if (!ignoreItemStream && (writer instanceof ItemStream)) {
092                                ((ItemStream) writer).open(executionContext);
093                        }
094                }
095        }
096
097    @Override
098        public void update(ExecutionContext executionContext) throws ItemStreamException {
099                for (ItemWriter<? super T> writer : delegates) {
100                        if (!ignoreItemStream && (writer instanceof ItemStream)) {
101                                ((ItemStream) writer).update(executionContext);
102                        }
103                }
104        }
105
106}