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

COVERAGE SUMMARY FOR SOURCE FILE [SimpleChunkProvider.java]

nameclass, %method, %block, %line, %
SimpleChunkProvider.java100% (2/2)90%  (9/10)98%  (123/126)97%  (35/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SimpleChunkProvider100% (1/1)88%  (7/8)96%  (81/84)96%  (24/25)
getListener (): MulticasterBatchListener 0%   (0/1)0%   (0/3)0%   (0/1)
SimpleChunkProvider (ItemReader, RepeatOperations): void 100% (1/1)100% (19/19)100% (6/6)
doRead (): Object 100% (1/1)100% (22/22)100% (8/8)
postProcess (StepContribution, Chunk): void 100% (1/1)100% (1/1)100% (1/1)
provide (StepContribution): Chunk 100% (1/1)100% (16/16)100% (3/3)
read (StepContribution, Chunk): Object 100% (1/1)100% (3/3)100% (1/1)
registerListener (StepListener): void 100% (1/1)100% (5/5)100% (2/2)
setListeners (List): void 100% (1/1)100% (15/15)100% (3/3)
     
class SimpleChunkProvider$1100% (1/1)100% (2/2)100% (42/42)100% (12/12)
SimpleChunkProvider$1 (SimpleChunkProvider, StepContribution, Chunk): void 100% (1/1)100% (12/12)100% (1/1)
doInIteration (RepeatContext): RepeatStatus 100% (1/1)100% (30/30)100% (11/11)

1/*
2 * Copyright 2006-2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17package org.springframework.batch.core.step.item;
18 
19import java.util.List;
20 
21import org.apache.commons.logging.Log;
22import org.apache.commons.logging.LogFactory;
23import org.springframework.batch.core.StepContribution;
24import org.springframework.batch.core.StepListener;
25import org.springframework.batch.core.listener.MulticasterBatchListener;
26import org.springframework.batch.item.ItemReader;
27import org.springframework.batch.repeat.RepeatCallback;
28import org.springframework.batch.repeat.RepeatContext;
29import org.springframework.batch.repeat.RepeatOperations;
30import org.springframework.batch.repeat.RepeatStatus;
31 
32/**
33 * Simple implementation of the ChunkProvider interface that does basic chunk
34 * providing from an {@link ItemReader}.
35 * 
36 * @author Dave Syer
37 * @see ChunkOrientedTasklet
38 */
39public class SimpleChunkProvider<I> implements ChunkProvider<I> {
40 
41        protected final Log logger = LogFactory.getLog(getClass());
42 
43        protected final ItemReader<? extends I> itemReader;
44 
45        private final MulticasterBatchListener<I, ?> listener = new MulticasterBatchListener<I, Object>();
46 
47        private final RepeatOperations repeatOperations;
48 
49        public SimpleChunkProvider(ItemReader<? extends I> itemReader, RepeatOperations repeatOperations) {
50                this.itemReader = itemReader;
51                this.repeatOperations = repeatOperations;
52        }
53 
54        /**
55         * Register some {@link StepListener}s with the handler. Each will get the
56         * callbacks in the order specified at the correct stage.
57         * 
58         * @param listeners
59         */
60        public void setListeners(List<? extends StepListener> listeners) {
61                for (StepListener listener : listeners) {
62                        registerListener(listener);
63                }
64        }
65 
66        /**
67         * Register a listener for callbacks at the appropriate stages in a process.
68         * 
69         * @param listener a {@link StepListener}
70         */
71        public void registerListener(StepListener listener) {
72                this.listener.register(listener);
73        }
74 
75        /**
76         * @return the listener
77         */
78        protected MulticasterBatchListener<I, ?> getListener() {
79                return listener;
80        }
81 
82        /**
83         * Surrounds the read call with listener callbacks.
84         * @return item
85         * @throws Exception
86         */
87        protected final I doRead() throws Exception {
88                try {
89                        listener.beforeRead();
90                        I item = itemReader.read();
91                        if(item != null) {
92                                listener.afterRead(item);
93                        }
94                        return item;
95                }
96                catch (Exception e) {
97                        listener.onReadError(e);
98                        throw e;
99                }
100        }
101 
102        public Chunk<I> provide(final StepContribution contribution) throws Exception {
103 
104                final Chunk<I> inputs = new Chunk<I>();
105                repeatOperations.iterate(new RepeatCallback() {
106 
107                        public RepeatStatus doInIteration(final RepeatContext context) throws Exception {
108                                I item = null;
109                                try {
110                                        item = read(contribution, inputs);
111                                }
112                                catch (SkipOverflowException e) {
113                                        // read() tells us about an excess of skips by throwing an
114                                        // exception
115                                        return RepeatStatus.FINISHED;
116                                }
117                                if (item == null) {
118                                        inputs.setEnd();
119                                        return RepeatStatus.FINISHED;
120                                }
121                                inputs.add(item);
122                                contribution.incrementReadCount();
123                                return RepeatStatus.CONTINUABLE;
124                        }
125 
126                });
127 
128                return inputs;
129 
130        }
131 
132        public void postProcess(StepContribution contribution, Chunk<I> chunk) {
133                // do nothing
134        }
135 
136        /**
137         * Delegates to {@link #doRead()}. Subclasses can add additional behaviour
138         * (e.g. exception handling).
139         * 
140         * @param contribution the current step execution contribution
141         * @param chunk the current chunk
142         * @return a new item for processing
143         * 
144         * @throws SkipOverflowException if specifically the chunk is accumulating
145         * too much data (e.g. skips) and it wants to force a commit.
146         * 
147         * @throws Exception if there is a generic issue
148         */
149        protected I read(StepContribution contribution, Chunk<I> chunk) throws SkipOverflowException, Exception {
150                return doRead();
151        }
152 
153}

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