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 | |
17 | package org.springframework.batch.item.support; |
18 | |
19 | import java.util.List; |
20 | |
21 | import org.springframework.batch.item.ExecutionContext; |
22 | import org.springframework.batch.item.ItemStream; |
23 | import org.springframework.batch.item.ItemStreamException; |
24 | import org.springframework.batch.item.ItemStreamWriter; |
25 | import org.springframework.batch.item.ItemWriter; |
26 | import org.springframework.beans.factory.InitializingBean; |
27 | import org.springframework.util.Assert; |
28 | |
29 | /** |
30 | * Calls a collection of {@link ItemWriter}s in fixed-order sequence.<br/> |
31 | * <br/> |
32 | * |
33 | * The implementation is thread-safe if all delegates are thread-safe. |
34 | * |
35 | * @author Robert Kasanicky |
36 | * @author Dave Syer |
37 | */ |
38 | public class CompositeItemWriter<T> implements ItemStreamWriter<T>, InitializingBean { |
39 | |
40 | private List<ItemWriter<? super T>> delegates; |
41 | |
42 | private boolean ignoreItemStream = false; |
43 | |
44 | public void setIgnoreItemStream(boolean ignoreItemStream) { |
45 | this.ignoreItemStream = ignoreItemStream; |
46 | } |
47 | |
48 | public void write(List<? extends T> item) throws Exception { |
49 | for (ItemWriter<? super T> writer : delegates) { |
50 | writer.write(item); |
51 | } |
52 | } |
53 | |
54 | public void afterPropertiesSet() throws Exception { |
55 | Assert.notNull(delegates, "The 'delgates' may not be null"); |
56 | Assert.notEmpty(delegates, "The 'delgates' may not be empty"); |
57 | } |
58 | |
59 | public void setDelegates(List<ItemWriter<? super T>> delegates) { |
60 | this.delegates = delegates; |
61 | } |
62 | |
63 | public void close() throws ItemStreamException { |
64 | for (ItemWriter<? super T> writer : delegates) { |
65 | if (!ignoreItemStream && (writer instanceof ItemStream)) { |
66 | ((ItemStream) writer).close(); |
67 | } |
68 | } |
69 | } |
70 | |
71 | public void open(ExecutionContext executionContext) throws ItemStreamException { |
72 | for (ItemWriter<? super T> writer : delegates) { |
73 | if (!ignoreItemStream && (writer instanceof ItemStream)) { |
74 | ((ItemStream) writer).open(executionContext); |
75 | } |
76 | } |
77 | } |
78 | |
79 | public void update(ExecutionContext executionContext) throws ItemStreamException { |
80 | for (ItemWriter<? super T> writer : delegates) { |
81 | if (!ignoreItemStream && (writer instanceof ItemStream)) { |
82 | ((ItemStream) writer).update(executionContext); |
83 | } |
84 | } |
85 | } |
86 | |
87 | } |