1 | /* |
2 | * Copyright 2006-2010 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 | package org.springframework.batch.item.support; |
17 | |
18 | import java.util.Map.Entry; |
19 | |
20 | import org.springframework.batch.item.ExecutionContext; |
21 | import org.springframework.batch.item.ItemReader; |
22 | import org.springframework.batch.item.ItemStream; |
23 | import org.springframework.batch.item.ItemStreamException; |
24 | import org.springframework.batch.item.ItemStreamReader; |
25 | import org.springframework.batch.item.ParseException; |
26 | import org.springframework.batch.item.PeekableItemReader; |
27 | import org.springframework.batch.item.UnexpectedInputException; |
28 | |
29 | /** |
30 | * <p> |
31 | * A {@link PeekableItemReader} that allows the user to peek one item ahead. |
32 | * Repeated calls to {@link #peek()} will return the same item, and this will be |
33 | * the next item returned from {@link #read()}. |
34 | * </p> |
35 | * |
36 | * <p> |
37 | * Intentionally not thread safe: it wouldn't be possible to honour the peek in |
38 | * multiple threads because only one of the threads that peeked would get that |
39 | * item in the next call to read. |
40 | * </p> |
41 | * |
42 | * @author Dave Syer |
43 | * |
44 | */ |
45 | public class SingleItemPeekableItemReader<T> implements ItemStreamReader<T>, PeekableItemReader<T> { |
46 | |
47 | private ItemReader<T> delegate; |
48 | |
49 | private T next; |
50 | |
51 | private ExecutionContext executionContext = new ExecutionContext(); |
52 | |
53 | /** |
54 | * The item reader to use as a delegate. Items are read from the delegate |
55 | * and passed to the caller in {@link #read()}. |
56 | * |
57 | * @param delegate the delegate to set |
58 | */ |
59 | public void setDelegate(ItemReader<T> delegate) { |
60 | this.delegate = delegate; |
61 | } |
62 | |
63 | /** |
64 | * Get the next item from the delegate (whether or not it has already been |
65 | * peeked at). |
66 | * |
67 | * @see ItemReader#read() |
68 | */ |
69 | public T read() throws Exception, UnexpectedInputException, ParseException { |
70 | if (next != null) { |
71 | T item = next; |
72 | next = null; |
73 | // executionContext = new ExecutionContext(); |
74 | return item; |
75 | } |
76 | return delegate.read(); |
77 | } |
78 | |
79 | /** |
80 | * Peek at the next item, ensuring that if the delegate is an |
81 | * {@link ItemStream} the state is stored for the next call to |
82 | * {@link #update(ExecutionContext)}. |
83 | * |
84 | * @return the next item (or null if there is none). |
85 | * |
86 | * @see PeekableItemReader#peek() |
87 | */ |
88 | public T peek() throws Exception, UnexpectedInputException, ParseException { |
89 | if (next == null) { |
90 | updateDelegate(executionContext); |
91 | next = delegate.read(); |
92 | } |
93 | return next; |
94 | } |
95 | |
96 | /** |
97 | * If the delegate is an {@link ItemStream}, just pass the call on, |
98 | * otherwise reset the peek cache. |
99 | * |
100 | * @throws ItemStreamException if there is a problem |
101 | * @see ItemStream#close() |
102 | */ |
103 | public void close() throws ItemStreamException { |
104 | next = null; |
105 | if (delegate instanceof ItemStream) { |
106 | ((ItemStream) delegate).close(); |
107 | } |
108 | executionContext = new ExecutionContext(); |
109 | } |
110 | |
111 | /** |
112 | * If the delegate is an {@link ItemStream}, just pass the call on, |
113 | * otherwise reset the peek cache. |
114 | * |
115 | * @param executionContext the current context |
116 | * @throws ItemStreamException if there is a problem |
117 | * @see ItemStream#open(ExecutionContext) |
118 | */ |
119 | public void open(ExecutionContext executionContext) throws ItemStreamException { |
120 | next = null; |
121 | if (delegate instanceof ItemStream) { |
122 | ((ItemStream) delegate).open(executionContext); |
123 | } |
124 | executionContext = new ExecutionContext(); |
125 | } |
126 | |
127 | /** |
128 | * If there is a cached peek, then retrieve the execution context state from |
129 | * that point. If there is no peek cached, then call directly to the |
130 | * delegate. |
131 | * |
132 | * @param executionContext the current context |
133 | * @throws ItemStreamException if there is a problem |
134 | * @see ItemStream#update(ExecutionContext) |
135 | */ |
136 | public void update(ExecutionContext executionContext) throws ItemStreamException { |
137 | if (next != null) { |
138 | // Get the last state from the delegate instead of using |
139 | // current value. |
140 | for (Entry<String, Object> entry : this.executionContext.entrySet()) { |
141 | executionContext.put(entry.getKey(), entry.getValue()); |
142 | } |
143 | return; |
144 | } |
145 | updateDelegate(executionContext); |
146 | } |
147 | |
148 | private void updateDelegate(ExecutionContext executionContext) { |
149 | if (delegate instanceof ItemStream) { |
150 | ((ItemStream) delegate).update(executionContext); |
151 | } |
152 | } |
153 | |
154 | } |