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