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