| 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.StepExecution; |
| 21 | import org.springframework.batch.core.StepExecutionListener; |
| 22 | import org.springframework.batch.repeat.ExitStatus; |
| 23 | import org.springframework.core.Ordered; |
| 24 | |
| 25 | /** |
| 26 | * @author Lucas Ward |
| 27 | * @author Dave Syer |
| 28 | * |
| 29 | */ |
| 30 | public class CompositeStepExecutionListener implements StepExecutionListener { |
| 31 | |
| 32 | private OrderedComposite list = new OrderedComposite(); |
| 33 | |
| 34 | /** |
| 35 | * Public setter for the listeners. |
| 36 | * |
| 37 | * @param listeners |
| 38 | */ |
| 39 | public void setListeners(StepExecutionListener[] listeners) { |
| 40 | list.setItems(listeners); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Register additional listener. |
| 45 | * |
| 46 | * @param stepExecutionListener |
| 47 | */ |
| 48 | public void register(StepExecutionListener stepExecutionListener) { |
| 49 | list.add(stepExecutionListener); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Call the registered listeners in reverse order, respecting and |
| 54 | * prioritising those that implement {@link Ordered}. |
| 55 | * @see org.springframework.batch.core.StepExecutionListener#afterStep(StepExecution) |
| 56 | */ |
| 57 | public ExitStatus afterStep(StepExecution stepExecution) { |
| 58 | ExitStatus status = null; |
| 59 | for (Iterator iterator = list.reverse(); iterator.hasNext();) { |
| 60 | StepExecutionListener listener = (StepExecutionListener) iterator.next(); |
| 61 | ExitStatus close = listener.afterStep(stepExecution); |
| 62 | status = status != null ? status.and(close) : close; |
| 63 | } |
| 64 | return status; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Call the registered listeners in order, respecting and prioritising those |
| 69 | * that implement {@link Ordered}. |
| 70 | * @see org.springframework.batch.core.StepExecutionListener#beforeStep(StepExecution) |
| 71 | */ |
| 72 | public void beforeStep(StepExecution stepExecution) { |
| 73 | for (Iterator iterator = list.iterator(); iterator.hasNext();) { |
| 74 | StepExecutionListener listener = (StepExecutionListener) iterator.next(); |
| 75 | listener.beforeStep(stepExecution); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Call the registered listeners in reverse order, respecting and |
| 81 | * prioritising those that implement {@link Ordered}. |
| 82 | * @see org.springframework.batch.core.StepExecutionListener#onErrorInStep(StepExecution, |
| 83 | * java.lang.Throwable) |
| 84 | */ |
| 85 | public ExitStatus onErrorInStep(StepExecution stepExecution, Throwable e) { |
| 86 | ExitStatus status = null; |
| 87 | for (Iterator iterator = list.reverse(); iterator.hasNext();) { |
| 88 | StepExecutionListener listener = (StepExecutionListener) iterator.next(); |
| 89 | ExitStatus close = listener.onErrorInStep(stepExecution, e); |
| 90 | status = status != null ? status.and(close) : close; |
| 91 | } |
| 92 | return status; |
| 93 | } |
| 94 | } |