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 | |
17 | package org.springframework.batch.repeat.callback; |
18 | |
19 | import org.springframework.batch.item.ItemReader; |
20 | import org.springframework.batch.item.ItemWriter; |
21 | import org.springframework.batch.repeat.ExitStatus; |
22 | import org.springframework.batch.repeat.RepeatCallback; |
23 | import org.springframework.batch.repeat.RepeatContext; |
24 | |
25 | /** |
26 | * Simple wrapper for two business interfaces: get the next item from a |
27 | * reader and apply the given writer to the result (if not null). |
28 | * |
29 | * @author Dave Syer |
30 | * |
31 | */ |
32 | public class ItemReaderRepeatCallback implements RepeatCallback { |
33 | |
34 | ItemReader provider; |
35 | |
36 | ItemWriter writer; |
37 | |
38 | public ItemReaderRepeatCallback(ItemReader provider, ItemWriter writer) { |
39 | super(); |
40 | this.provider = provider; |
41 | this.writer = writer; |
42 | } |
43 | |
44 | /** |
45 | * Default writer is null, in which case we do nothing - subclasses can |
46 | * extend this behaviour, but must be careful to actually exhaust the |
47 | * provider by calling next(). |
48 | * @param provider |
49 | */ |
50 | public ItemReaderRepeatCallback(ItemReader provider) { |
51 | this(provider, null); |
52 | } |
53 | |
54 | /** |
55 | * Use the writer to process the next item if there is one. Return the |
56 | * item processed, or null if nothing was available. |
57 | * @see org.springframework.batch.repeat.RepeatCallback#doInIteration(RepeatContext) |
58 | * @param context the current context. |
59 | * @return null if the data provider is exhausted. |
60 | */ |
61 | public ExitStatus doInIteration(RepeatContext context) throws Exception { |
62 | |
63 | ExitStatus result = ExitStatus.FINISHED; |
64 | Object item = provider.read(); |
65 | |
66 | if (writer != null) { |
67 | if (item != null) { |
68 | writer.write(item); |
69 | result = ExitStatus.CONTINUABLE; |
70 | } |
71 | item = null; |
72 | } |
73 | |
74 | return result; |
75 | } |
76 | |
77 | } |