View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.item.support;
18  
19  import java.util.List;
20  
21  import org.springframework.batch.item.ExecutionContext;
22  import org.springframework.batch.item.ItemStream;
23  import org.springframework.batch.item.ItemStreamException;
24  import org.springframework.batch.item.ItemStreamWriter;
25  import org.springframework.batch.item.ItemWriter;
26  import org.springframework.beans.factory.InitializingBean;
27  import org.springframework.util.Assert;
28  
29  /**
30   * Calls a collection of {@link ItemWriter}s in fixed-order sequence.<br/>
31   * <br/>
32   * 
33   * The implementation is thread-safe if all delegates are thread-safe.
34   * 
35   * @author Robert Kasanicky
36   * @author Dave Syer
37   */
38  public class CompositeItemWriter<T> implements ItemStreamWriter<T>, InitializingBean {
39  
40  	private List<ItemWriter<? super T>> delegates;
41  
42  	private boolean ignoreItemStream = false;
43  
44  	public void setIgnoreItemStream(boolean ignoreItemStream) {
45  		this.ignoreItemStream = ignoreItemStream;
46  	}
47  
48      @Override
49  	public void write(List<? extends T> item) throws Exception {
50  		for (ItemWriter<? super T> writer : delegates) {
51  			writer.write(item);
52  		}
53  	}
54  
55      @Override
56  	public void afterPropertiesSet() throws Exception {
57  		Assert.notNull(delegates, "The 'delgates' may not be null");
58  		Assert.notEmpty(delegates, "The 'delgates' may not be empty");
59  	}
60  
61  	public void setDelegates(List<ItemWriter<? super T>> delegates) {
62  		this.delegates = delegates;
63  	}
64  
65      @Override
66  	public void close() throws ItemStreamException {
67  		for (ItemWriter<? super T> writer : delegates) {
68  			if (!ignoreItemStream && (writer instanceof ItemStream)) {
69  				((ItemStream) writer).close();
70  			}
71  		}
72  	}
73  
74      @Override
75  	public void open(ExecutionContext executionContext) throws ItemStreamException {
76  		for (ItemWriter<? super T> writer : delegates) {
77  			if (!ignoreItemStream && (writer instanceof ItemStream)) {
78  				((ItemStream) writer).open(executionContext);
79  			}
80  		}
81  	}
82  
83      @Override
84  	public void update(ExecutionContext executionContext) throws ItemStreamException {
85  		for (ItemWriter<? super T> writer : delegates) {
86  			if (!ignoreItemStream && (writer instanceof ItemStream)) {
87  				((ItemStream) writer).update(executionContext);
88  			}
89  		}
90  	}
91  
92  }