EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.item.file]

COVERAGE SUMMARY FOR SOURCE FILE [ResourcesItemReader.java]

nameclass, %method, %block, %line, %
ResourcesItemReader.java100% (1/1)83%  (5/6)98%  (65/66)94%  (15/16)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ResourcesItemReader100% (1/1)83%  (5/6)98%  (65/66)94%  (15/16)
close (): void 0%   (0/1)0%   (0/1)0%   (0/1)
ResourcesItemReader (): void 100% (1/1)100% (18/18)100% (5/5)
open (ExecutionContext): void 100% (1/1)100% (10/10)100% (2/2)
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% (9/9)100% (2/2)

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

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