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.JobExecution; |
24 | import org.springframework.batch.core.JobExecutionListener; |
25 | |
26 | /** |
27 | * @author Dave Syer |
28 | * |
29 | */ |
30 | public class CompositeExecutionJobListener implements JobExecutionListener { |
31 | |
32 | private List listeners = new ArrayList(); |
33 | |
34 | /** |
35 | * Public setter for the listeners. |
36 | * |
37 | * @param listeners |
38 | */ |
39 | public void setListeners(JobExecutionListener[] listeners) { |
40 | this.listeners = Arrays.asList(listeners); |
41 | } |
42 | |
43 | /** |
44 | * Register additional listener. |
45 | * |
46 | * @param jobExecutionListener |
47 | */ |
48 | public void register(JobExecutionListener jobExecutionListener) { |
49 | if (!listeners.contains(jobExecutionListener)) { |
50 | listeners.add(jobExecutionListener); |
51 | } |
52 | } |
53 | |
54 | public void afterJob(JobExecution jobExecution) { |
55 | for (Iterator iterator = listeners.listIterator(); iterator.hasNext();) { |
56 | JobExecutionListener listener = (JobExecutionListener) iterator.next(); |
57 | listener.afterJob(jobExecution); |
58 | } |
59 | } |
60 | |
61 | public void beforeJob(JobExecution jobExecution) { |
62 | for (Iterator iterator = listeners.listIterator(); iterator.hasNext();) { |
63 | JobExecutionListener listener = (JobExecutionListener) iterator.next(); |
64 | listener.beforeJob(jobExecution); |
65 | } |
66 | } |
67 | |
68 | public void onError(JobExecution jobExecution, Throwable e) { |
69 | for (Iterator iterator = listeners.listIterator(); iterator.hasNext();) { |
70 | JobExecutionListener listener = (JobExecutionListener) iterator.next(); |
71 | listener.onError(jobExecution, e); |
72 | } |
73 | |
74 | } |
75 | |
76 | public void onInterrupt(JobExecution jobExecution) { |
77 | for (Iterator iterator = listeners.listIterator(); iterator.hasNext();) { |
78 | JobExecutionListener listener = (JobExecutionListener) iterator.next(); |
79 | listener.onInterrupt(jobExecution); |
80 | } |
81 | |
82 | } |
83 | } |