View Javadoc

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