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