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.repeat.listener; |
17 | |
18 | import java.util.ArrayList; |
19 | import java.util.Arrays; |
20 | import java.util.List; |
21 | |
22 | import org.springframework.batch.repeat.RepeatStatus; |
23 | import org.springframework.batch.repeat.RepeatContext; |
24 | import org.springframework.batch.repeat.RepeatListener; |
25 | |
26 | /** |
27 | * @author Dave Syer |
28 | * |
29 | */ |
30 | public class CompositeRepeatListener implements RepeatListener { |
31 | |
32 | private List<RepeatListener> listeners = new ArrayList<RepeatListener>(); |
33 | |
34 | /** |
35 | * Public setter for the listeners. |
36 | * |
37 | * @param listeners |
38 | */ |
39 | public void setListeners(RepeatListener[] listeners) { |
40 | this.listeners = Arrays.asList(listeners); |
41 | } |
42 | |
43 | /** |
44 | * Register additional listener. |
45 | * |
46 | * @param listener |
47 | */ |
48 | public void register(RepeatListener listener) { |
49 | if (!listeners.contains(listener)) { |
50 | listeners.add(listener); |
51 | } |
52 | } |
53 | |
54 | /* (non-Javadoc) |
55 | * @see org.springframework.batch.repeat.RepeatListener#after(org.springframework.batch.repeat.RepeatContext, org.springframework.batch.repeat.ExitStatus) |
56 | */ |
57 | @Override |
58 | public void after(RepeatContext context, RepeatStatus result) { |
59 | for (RepeatListener listener : listeners) { |
60 | listener.after(context, result); |
61 | } |
62 | } |
63 | |
64 | /* (non-Javadoc) |
65 | * @see org.springframework.batch.repeat.RepeatListener#before(org.springframework.batch.repeat.RepeatContext) |
66 | */ |
67 | @Override |
68 | public void before(RepeatContext context) { |
69 | for (RepeatListener listener : listeners) { |
70 | listener.before(context); |
71 | } |
72 | } |
73 | |
74 | /* (non-Javadoc) |
75 | * @see org.springframework.batch.repeat.RepeatListener#close(org.springframework.batch.repeat.RepeatContext) |
76 | */ |
77 | @Override |
78 | public void close(RepeatContext context) { |
79 | for (RepeatListener listener : listeners) { |
80 | listener.close(context); |
81 | } |
82 | } |
83 | |
84 | /* (non-Javadoc) |
85 | * @see org.springframework.batch.repeat.RepeatListener#onError(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable) |
86 | */ |
87 | @Override |
88 | public void onError(RepeatContext context, Throwable e) { |
89 | for (RepeatListener listener : listeners) { |
90 | listener.onError(context, e); |
91 | } |
92 | } |
93 | |
94 | /* (non-Javadoc) |
95 | * @see org.springframework.batch.repeat.RepeatListener#open(org.springframework.batch.repeat.RepeatContext) |
96 | */ |
97 | @Override |
98 | public void open(RepeatContext context) { |
99 | for (RepeatListener listener : listeners) { |
100 | listener.open(context); |
101 | } |
102 | } |
103 | |
104 | } |