EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.item.file]

COVERAGE SUMMARY FOR SOURCE FILE [ResourcesItemReader.java]

nameclass, %method, %block, %line, %
ResourcesItemReader.java100% (1/1)100% (5/5)100% (71/71)100% (17/17)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ResourcesItemReader100% (1/1)100% (5/5)100% (71/71)100% (17/17)
ResourcesItemReader (): void 100% (1/1)100% (18/18)100% (5/5)
open (ExecutionContext): void 100% (1/1)100% (13/13)100% (3/3)
read (): Resource 100% (1/1)100% (18/18)100% (4/4)
setResources (Resource []): void 100% (1/1)100% (10/10)100% (2/2)
update (ExecutionContext): void 100% (1/1)100% (12/12)100% (3/3)

1package org.springframework.batch.item.file;
2 
3import java.util.Arrays;
4import java.util.concurrent.atomic.AtomicInteger;
5 
6import org.springframework.batch.item.ExecutionContext;
7import org.springframework.batch.item.ItemReader;
8import org.springframework.batch.item.ItemStreamException;
9import org.springframework.batch.item.support.AbstractItemStreamItemReader;
10import org.springframework.core.io.Resource;
11import 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 */
32public 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}

[all classes][org.springframework.batch.item.file]
EMMA 2.0.5312 (C) Vladimir Roubtsov