| 1 | package org.springframework.batch.item.support; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.springframework.batch.item.ClearFailedException; |
| 7 | import org.springframework.batch.item.FlushFailedException; |
| 8 | import org.springframework.batch.item.ItemWriter; |
| 9 | |
| 10 | /** |
| 11 | * Calls a collection of ItemWriters in fixed-order sequence. |
| 12 | * |
| 13 | * The implementation is thread-safe if all delegates are thread-safe. |
| 14 | * |
| 15 | * @author Robert Kasanicky |
| 16 | */ |
| 17 | public class CompositeItemWriter implements ItemWriter { |
| 18 | |
| 19 | private List delegates; |
| 20 | |
| 21 | public void setDelegates(List delegates) { |
| 22 | this.delegates = delegates; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Calls injected ItemProcessors in order. |
| 27 | */ |
| 28 | public void write(Object data) throws Exception { |
| 29 | for (Iterator iterator = delegates.listIterator(); iterator.hasNext();) { |
| 30 | ((ItemWriter) iterator.next()).write(data); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public void clear() throws ClearFailedException { |
| 35 | for (Iterator iterator = delegates.listIterator(); iterator.hasNext();) { |
| 36 | ((ItemWriter) iterator.next()).clear(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public void flush() throws FlushFailedException { |
| 41 | for (Iterator iterator = delegates.listIterator(); iterator.hasNext();) { |
| 42 | ((ItemWriter) iterator.next()).flush(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | } |