| 1 | package org.springframework.batch.item.file; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.concurrent.atomic.AtomicInteger; |
| 5 | |
| 6 | import org.springframework.batch.item.ExecutionContext; |
| 7 | import org.springframework.batch.item.ItemReader; |
| 8 | import org.springframework.batch.item.ItemStreamException; |
| 9 | import org.springframework.batch.item.support.AbstractItemStreamItemReader; |
| 10 | import org.springframework.core.io.Resource; |
| 11 | import org.springframework.core.io.support.ResourceArrayPropertyEditor; |
| 12 | |
| 13 | /** |
| 14 | * {@link ItemReader} which produces {@link Resource} instances from an array. |
| 15 | * This can be used conveniently with a configuration entry that injects a |
| 16 | * pattern (e.g. <code>mydir/*.txt</code>, which can then be converted by Spring |
| 17 | * to an array of Resources by the ApplicationContext. |
| 18 | * |
| 19 | * <br/> |
| 20 | * <br/> |
| 21 | * |
| 22 | * Thread safe between calls to {@link #open(ExecutionContext)}. The |
| 23 | * {@link ExecutionContext} is not accurate in a multi-threaded environment, so |
| 24 | * do not rely on that data for restart (i.e. always open with a fresh context). |
| 25 | * |
| 26 | * @author Dave Syer |
| 27 | * |
| 28 | * @see ResourceArrayPropertyEditor |
| 29 | * |
| 30 | * @since 2.1 |
| 31 | */ |
| 32 | public class ResourcesItemReader extends AbstractItemStreamItemReader<Resource> { |
| 33 | |
| 34 | private Resource[] resources = new Resource[0]; |
| 35 | |
| 36 | private AtomicInteger counter = new AtomicInteger(0); |
| 37 | |
| 38 | public ResourcesItemReader() { |
| 39 | /* |
| 40 | * Initialize the name for the key in the execution context. |
| 41 | */ |
| 42 | this.setExecutionContextName(getClass().getName()); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * The resources to serve up as items. Hint: use a pattern to configure. |
| 47 | * |
| 48 | * @param resources the resources |
| 49 | */ |
| 50 | public void setResources(Resource[] resources) { |
| 51 | this.resources = Arrays.asList(resources).toArray(new Resource[resources.length]); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Increments a counter and returns the next {@link Resource} instance from |
| 56 | * the input, or null if none remain. |
| 57 | */ |
| 58 | @Override |
| 59 | public synchronized Resource read() throws Exception { |
| 60 | int index = counter.incrementAndGet() - 1; |
| 61 | if (index >= resources.length) { |
| 62 | return null; |
| 63 | } |
| 64 | return resources[index]; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void open(ExecutionContext executionContext) throws ItemStreamException { |
| 69 | super.open(executionContext); |
| 70 | counter.set(executionContext.getInt(getExecutionContextKey("COUNT"), 0)); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void update(ExecutionContext executionContext) throws ItemStreamException { |
| 75 | super.update(executionContext); |
| 76 | executionContext.putInt(getExecutionContextKey("COUNT"), counter.get()); |
| 77 | } |
| 78 | |
| 79 | } |