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.ItemProcessListener;
22  import org.springframework.core.Ordered;
23  
24  /**
25   * @author Dave Syer
26   *
27   */
28  public class CompositeItemProcessListener<T, S> implements ItemProcessListener<T, S> {
29  
30  	private OrderedComposite<ItemProcessListener<? super T, ? super S>> listeners = new OrderedComposite<ItemProcessListener<? super T, ? super S>>();
31  
32  	/**
33  	 * Public setter for the listeners.
34  	 *
35  	 * @param itemReadListeners
36  	 */
37  	public void setListeners(List<? extends ItemProcessListener<? super T, ? super S>> itemReadListeners) {
38  		this.listeners.setItems(itemReadListeners);
39  	}
40  
41  	/**
42  	 * Register additional listener.
43  	 *
44  	 * @param itemReaderListener
45  	 */
46  	public void register(ItemProcessListener<? super T, ? super S> 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.ItemProcessListener#afterProcess(java.lang.Object,
54  	 * java.lang.Object)
55  	 */
56  	@Override
57  	public void afterProcess(T item, S result) {
58  		for (Iterator<ItemProcessListener<? super T, ? super S>> iterator = listeners.reverse(); iterator.hasNext();) {
59  			ItemProcessListener<? super T, ? super S> listener = iterator.next();
60  			listener.afterProcess(item, result);
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.ItemProcessListener#beforeProcess(java.lang.Object)
68  	 */
69  	@Override
70  	public void beforeProcess(T item) {
71  		for (Iterator<ItemProcessListener<? super T, ? super S>> iterator = listeners.iterator(); iterator.hasNext();) {
72  			ItemProcessListener<? super T, ? super S> listener = iterator.next();
73  			listener.beforeProcess(item);
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.ItemProcessListener#onProcessError(java.lang.Object,
81  	 * java.lang.Exception)
82  	 */
83  	@Override
84  	public void onProcessError(T item, Exception e) {
85  		for (Iterator<ItemProcessListener<? super T, ? super S>> iterator = listeners.reverse(); iterator.hasNext();) {
86  			ItemProcessListener<? super T, ? super S> listener = iterator.next();
87  			listener.onProcessError(item, e);
88  		}
89  	}
90  
91  }