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