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