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.adapter; |
18 | |
19 | import org.springframework.batch.item.ClearFailedException; |
20 | import org.springframework.batch.item.FlushFailedException; |
21 | import org.springframework.batch.item.ItemWriter; |
22 | import org.springframework.beans.BeanWrapper; |
23 | import org.springframework.beans.BeanWrapperImpl; |
24 | import org.springframework.util.Assert; |
25 | |
26 | /** |
27 | * Delegates processing to a custom method - extracts property values |
28 | * from item object and uses them as arguments for the delegate method. |
29 | * |
30 | * @see ItemWriterAdapter |
31 | * |
32 | * @author Robert Kasanicky |
33 | */ |
34 | public class PropertyExtractingDelegatingItemWriter extends AbstractMethodInvokingDelegator implements ItemWriter { |
35 | |
36 | private String[] fieldsUsedAsTargetMethodArguments; |
37 | |
38 | /** |
39 | * Extracts values from item's fields named in fieldsUsedAsTargetMethodArguments |
40 | * and passes them as arguments to the delegate method. |
41 | */ |
42 | public void write(Object item) throws Exception { |
43 | // helper for extracting property values from a bean |
44 | BeanWrapper beanWrapper = new BeanWrapperImpl(item); |
45 | |
46 | Object[] methodArguments = new Object[fieldsUsedAsTargetMethodArguments.length]; |
47 | for (int i = 0; i < fieldsUsedAsTargetMethodArguments.length; i++) { |
48 | methodArguments[i] = beanWrapper.getPropertyValue(fieldsUsedAsTargetMethodArguments[i]); |
49 | } |
50 | |
51 | invokeDelegateMethodWithArguments(methodArguments); |
52 | } |
53 | |
54 | |
55 | public void afterPropertiesSet() throws Exception { |
56 | super.afterPropertiesSet(); |
57 | Assert.notEmpty(fieldsUsedAsTargetMethodArguments); |
58 | } |
59 | |
60 | /** |
61 | * @param fieldsUsedAsMethodArguments the values of the these item's fields |
62 | * will be used as arguments for the delegate method. Nested property values are |
63 | * supported, e.g. <code>address.city</code> |
64 | */ |
65 | public void setFieldsUsedAsTargetMethodArguments(String[] fieldsUsedAsMethodArguments) { |
66 | this.fieldsUsedAsTargetMethodArguments = fieldsUsedAsMethodArguments; |
67 | } |
68 | |
69 | |
70 | public void clear() throws ClearFailedException { |
71 | } |
72 | |
73 | |
74 | public void flush() throws FlushFailedException { |
75 | } |
76 | } |