| 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.ArrayList; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.springframework.batch.core.ItemReadListener; |
| 24 | |
| 25 | /** |
| 26 | * @author Lucas Ward |
| 27 | * |
| 28 | */ |
| 29 | public class CompositeItemReadListener implements ItemReadListener { |
| 30 | |
| 31 | private List listeners = new ArrayList(); |
| 32 | |
| 33 | /** |
| 34 | * Public setter for the listeners. |
| 35 | * |
| 36 | * @param itemReadListeners |
| 37 | */ |
| 38 | public void setListeners(ItemReadListener[] itemReadListeners) { |
| 39 | this.listeners = Arrays.asList(itemReadListeners); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Register additional listener. |
| 44 | * |
| 45 | * @param itemReaderListener |
| 46 | */ |
| 47 | public void register(ItemReadListener itemReaderListener) { |
| 48 | if (!listeners.contains(itemReaderListener)) { |
| 49 | listeners.add(itemReaderListener); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public void afterRead(Object item) { |
| 54 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { |
| 55 | ItemReadListener listener = (ItemReadListener) iterator.next(); |
| 56 | listener.afterRead(item); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public void beforeRead() { |
| 61 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { |
| 62 | ItemReadListener listener = (ItemReadListener) iterator.next(); |
| 63 | listener.beforeRead(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public void onReadError(Exception ex) { |
| 68 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { |
| 69 | ItemReadListener listener = (ItemReadListener) iterator.next(); |
| 70 | listener.onReadError(ex); |
| 71 | } |
| 72 | } |
| 73 | } |