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 | package org.springframework.batch.core.listener; |
17 | |
18 | import java.util.Iterator; |
19 | |
20 | import org.springframework.batch.core.ItemReadListener; |
21 | import org.springframework.core.Ordered; |
22 | |
23 | /** |
24 | * @author Lucas Ward |
25 | * @author Dave Syer |
26 | * |
27 | */ |
28 | public class CompositeItemReadListener implements ItemReadListener { |
29 | |
30 | private OrderedComposite listeners = new OrderedComposite(); |
31 | |
32 | /** |
33 | * Public setter for the listeners. |
34 | * |
35 | * @param itemReadListeners |
36 | */ |
37 | public void setListeners(ItemReadListener[] itemReadListeners) { |
38 | this.listeners.setItems(itemReadListeners); |
39 | } |
40 | |
41 | /** |
42 | * Register additional listener. |
43 | * |
44 | * @param itemReaderListener |
45 | */ |
46 | public void register(ItemReadListener itemReaderListener) { |
47 | listeners.add(itemReaderListener); |
48 | } |
49 | |
50 | /** |
51 | * Call the registered listeners in reverse order, respecting and |
52 | * prioritising those that implement {@link Ordered}. |
53 | * @see org.springframework.batch.core.ItemReadListener#afterRead(java.lang.Object) |
54 | */ |
55 | public void afterRead(Object item) { |
56 | for (Iterator iterator = listeners.reverse(); iterator.hasNext();) { |
57 | ItemReadListener listener = (ItemReadListener) iterator.next(); |
58 | listener.afterRead(item); |
59 | } |
60 | } |
61 | |
62 | /** |
63 | * Call the registered listeners in order, respecting and prioritising those |
64 | * that implement {@link Ordered}. |
65 | * @see org.springframework.batch.core.ItemReadListener#beforeRead() |
66 | */ |
67 | public void beforeRead() { |
68 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { |
69 | ItemReadListener listener = (ItemReadListener) iterator.next(); |
70 | listener.beforeRead(); |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Call the registered listeners in reverse order, respecting and |
76 | * prioritising those that implement {@link Ordered}. |
77 | * @see org.springframework.batch.core.ItemReadListener#onReadError(java.lang.Exception) |
78 | */ |
79 | public void onReadError(Exception ex) { |
80 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { |
81 | ItemReadListener listener = (ItemReadListener) iterator.next(); |
82 | listener.onReadError(ex); |
83 | } |
84 | } |
85 | } |