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.item.support; |
18 | |
19 | import org.springframework.batch.item.ItemReader; |
20 | import org.springframework.beans.factory.InitializingBean; |
21 | import org.springframework.util.Assert; |
22 | |
23 | /** |
24 | * Simple wrapper around {@link ItemReader}. The item reader is expected to |
25 | * take care of open and close operations. If necessary it should be registered |
26 | * as a step scoped bean to ensure that the lifecycle methods are called. |
27 | * |
28 | * The implementation is thread-safe if the delegate is thread-safe. |
29 | * |
30 | * @author Dave Syer |
31 | */ |
32 | public class DelegatingItemReader extends AbstractItemReader implements InitializingBean { |
33 | |
34 | private ItemReader itemReader; |
35 | |
36 | /** |
37 | * Default constructor. |
38 | */ |
39 | public DelegatingItemReader() { |
40 | super(); |
41 | } |
42 | |
43 | /** |
44 | * Convenience constructor for setting mandatory property. |
45 | */ |
46 | public DelegatingItemReader(ItemReader itemReader) { |
47 | this(); |
48 | this.itemReader = itemReader; |
49 | } |
50 | |
51 | public void afterPropertiesSet() throws Exception { |
52 | Assert.notNull(itemReader, "ItemReader must not be null."); |
53 | } |
54 | |
55 | /** |
56 | * Get the next object from the input source. |
57 | * @throws Exception |
58 | * @see org.springframework.batch.item.ItemReader#read() |
59 | */ |
60 | public Object read() throws Exception { |
61 | return itemReader.read(); |
62 | } |
63 | |
64 | /** |
65 | * Setter for input source. |
66 | * @param source |
67 | */ |
68 | public void setItemReader(ItemReader source) { |
69 | this.itemReader = source; |
70 | } |
71 | |
72 | /* |
73 | * (non-Javadoc) |
74 | * @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.ExecutionContext) |
75 | */ |
76 | public void mark() { |
77 | itemReader.mark(); |
78 | } |
79 | |
80 | /* |
81 | * (non-Javadoc) |
82 | * @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.ExecutionContext) |
83 | */ |
84 | public void reset() { |
85 | itemReader.reset(); |
86 | } |
87 | } |