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.ChunkListener; |
21 | import org.springframework.core.Ordered; |
22 | |
23 | /** |
24 | * @author Lucas Ward |
25 | * |
26 | */ |
27 | public class CompositeChunkListener implements ChunkListener { |
28 | |
29 | private OrderedComposite listeners = new OrderedComposite(); |
30 | |
31 | /** |
32 | * Public setter for the listeners. |
33 | * |
34 | * @param listeners |
35 | */ |
36 | public void setListeners(ChunkListener[] listeners) { |
37 | this.listeners.setItems(listeners); |
38 | } |
39 | |
40 | /** |
41 | * Register additional listener. |
42 | * |
43 | * @param chunkListener |
44 | */ |
45 | public void register(ChunkListener chunkListener) { |
46 | listeners.add(chunkListener); |
47 | } |
48 | |
49 | /** |
50 | * Call the registered listeners in order, respecting and prioritising those |
51 | * that implement {@link Ordered}. |
52 | * |
53 | * @see org.springframework.batch.core.ChunkListener#afterChunk() |
54 | */ |
55 | public void afterChunk() { |
56 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { |
57 | ChunkListener listener = (ChunkListener) iterator.next(); |
58 | listener.afterChunk(); |
59 | } |
60 | } |
61 | |
62 | /** |
63 | * Call the registered listeners in reverse order. |
64 | * |
65 | * @see org.springframework.batch.core.ChunkListener#beforeChunk() |
66 | */ |
67 | public void beforeChunk() { |
68 | for (Iterator iterator = listeners.reverse(); iterator.hasNext();) { |
69 | ChunkListener listener = (ChunkListener) iterator.next(); |
70 | listener.beforeChunk(); |
71 | } |
72 | } |
73 | } |