| 1 | package org.springframework.batch.item.support; |
| 2 | |
| 3 | import org.springframework.batch.item.ClearFailedException; |
| 4 | import org.springframework.batch.item.FlushFailedException; |
| 5 | import org.springframework.batch.item.ItemWriter; |
| 6 | import org.springframework.beans.factory.InitializingBean; |
| 7 | import org.springframework.util.Assert; |
| 8 | |
| 9 | /** |
| 10 | * Simple wrapper around {@link ItemWriter}. |
| 11 | * |
| 12 | * @author Dave Syer |
| 13 | * @author Robert Kasanicky |
| 14 | */ |
| 15 | public class DelegatingItemWriter implements ItemWriter, InitializingBean { |
| 16 | |
| 17 | private ItemWriter delegate; |
| 18 | |
| 19 | /** |
| 20 | * Default constructor. |
| 21 | */ |
| 22 | public DelegatingItemWriter() { |
| 23 | super(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @param itemWriter |
| 28 | */ |
| 29 | public DelegatingItemWriter(ItemWriter itemWriter) { |
| 30 | this(); |
| 31 | this.delegate = itemWriter; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Calls {@link #doProcess(Object)} and then writes the result to the |
| 36 | * delegate {@link ItemWriter}. |
| 37 | * @throws Exception |
| 38 | * |
| 39 | * @see ItemWriter#write(Object) |
| 40 | */ |
| 41 | public void write(Object item) throws Exception { |
| 42 | Object result = doProcess(item); |
| 43 | delegate.write(result); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * By default returns the argument. This method is an extension point meant |
| 48 | * to be overridden by subclasses that implement processing logic. |
| 49 | * @throws Exception |
| 50 | */ |
| 51 | protected Object doProcess(Object item) throws Exception { |
| 52 | return item; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Setter for {@link ItemWriter}. |
| 57 | */ |
| 58 | public void setDelegate(ItemWriter writer) { |
| 59 | this.delegate = writer; |
| 60 | } |
| 61 | |
| 62 | public void afterPropertiesSet() throws Exception { |
| 63 | Assert.notNull(delegate); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Delegates to {@link ItemWriter#clear()} |
| 68 | */ |
| 69 | public void clear() throws ClearFailedException { |
| 70 | delegate.clear(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Delegates to {@link ItemWriter#flush()} |
| 75 | */ |
| 76 | public void flush() throws FlushFailedException { |
| 77 | delegate.flush(); |
| 78 | } |
| 79 | |
| 80 | } |