EMMA Coverage Report (generated Fri Aug 21 15:59:46 BST 2009)
[all classes][org.springframework.batch.core.step.item]

COVERAGE SUMMARY FOR SOURCE FILE [SimpleChunkProvider.java]

nameclass, %method, %block, %line, %
SimpleChunkProvider.java100% (2/2)100% (10/10)100% (118/118)100% (32/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SimpleChunkProvider100% (1/1)100% (8/8)100% (82/82)100% (24/24)
SimpleChunkProvider (ItemReader, RepeatOperations): void 100% (1/1)100% (19/19)100% (6/6)
doRead (): Object 100% (1/1)100% (20/20)100% (7/7)
getListener (): MulticasterBatchListener 100% (1/1)100% (3/3)100% (1/1)
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% (36/36)100% (9/9)
SimpleChunkProvider$1 (SimpleChunkProvider, StepContribution, Chunk): void 100% (1/1)100% (12/12)100% (2/2)
doInIteration (RepeatContext): RepeatStatus 100% (1/1)100% (24/24)100% (7/7)

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 
34 * chunk 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                        listener.afterRead(item);
92                        return item;
93                }
94                catch (Exception e) {
95                        listener.onReadError(e);
96                        throw e;
97                }
98        }
99 
100        public Chunk<I> provide(final StepContribution contribution) throws Exception {
101 
102                final Chunk<I> inputs = new Chunk<I>();
103                repeatOperations.iterate(new RepeatCallback() {
104 
105                        public RepeatStatus doInIteration(final RepeatContext context) throws Exception {
106                                I item = read(contribution, inputs);
107                                if (item == null) {
108                                        inputs.setEnd();
109                                        return RepeatStatus.FINISHED;
110                                }
111                                inputs.add(item);
112                                contribution.incrementReadCount();
113                                return RepeatStatus.CONTINUABLE;
114                        }
115 
116                });
117 
118                return inputs;
119 
120        }
121        
122        public void postProcess(StepContribution contribution, Chunk<I> chunk) {
123                // do nothing
124        }
125 
126        protected I read(StepContribution contribution, Chunk<I> chunk) throws Exception {
127                return doRead();
128        }
129 
130}

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