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 java.lang.reflect.InvocationTargetException; |
20 | import java.lang.reflect.Method; |
21 | |
22 | import org.springframework.beans.factory.InitializingBean; |
23 | import org.springframework.util.Assert; |
24 | import org.springframework.util.MethodInvoker; |
25 | |
26 | /** |
27 | * Superclass for delegating classes which dynamically call a |
28 | * custom method of injected object. |
29 | * Provides convenient API for dynamic method invocation shielding |
30 | * subclasses from low-level details and exception handling. |
31 | * |
32 | * @author Robert Kasanicky |
33 | */ |
34 | public class AbstractMethodInvokingDelegator implements InitializingBean { |
35 | |
36 | private Object targetObject; |
37 | |
38 | private String targetMethod; |
39 | |
40 | private Object[] arguments; |
41 | |
42 | /** |
43 | * Invoker the target method with no arguments. |
44 | * @return object returned by invoked method |
45 | * @throws DynamicMethodInvocationException if the {@link MethodInvoker} used throws exception |
46 | */ |
47 | protected Object invokeDelegateMethod() { |
48 | MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); |
49 | invoker.setArguments(arguments); |
50 | return doInvoke(invoker); |
51 | } |
52 | |
53 | /** |
54 | * Invokes the target method with given argument. |
55 | * @param object argument for the target method |
56 | * @return object returned by target method |
57 | * @throws DynamicMethodInvocationException if the {@link MethodInvoker} used throws exception |
58 | */ |
59 | protected Object invokeDelegateMethodWithArgument(Object object) { |
60 | MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); |
61 | invoker.setArguments(new Object[]{object}); |
62 | return doInvoke(invoker); |
63 | } |
64 | |
65 | /** |
66 | * Invokes the target method with given arguments. |
67 | * @param args arguments for the invoked method |
68 | * @return object returned by invoked method |
69 | * @throws DynamicMethodInvocationException if the {@link MethodInvoker} used throws exception |
70 | */ |
71 | protected Object invokeDelegateMethodWithArguments(Object[] args) { |
72 | MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); |
73 | invoker.setArguments(args); |
74 | return doInvoke(invoker); |
75 | } |
76 | |
77 | /** |
78 | * Create a new configured instance of {@link MethodInvoker}. |
79 | */ |
80 | private MethodInvoker createMethodInvoker(Object targetObject, String targetMethod) { |
81 | MethodInvoker invoker = new MethodInvoker(); |
82 | invoker.setTargetObject(targetObject); |
83 | invoker.setTargetMethod(targetMethod); |
84 | invoker.setArguments(arguments); |
85 | return invoker; |
86 | } |
87 | |
88 | /** |
89 | * Prepare and invoke the invoker, rethrow checked exceptions as unchecked. |
90 | * @param invoker configured invoker |
91 | * @return return value of the invoked method |
92 | */ |
93 | private Object doInvoke(MethodInvoker invoker) { |
94 | try { |
95 | invoker.prepare(); |
96 | } |
97 | catch (ClassNotFoundException e) { |
98 | throw new DynamicMethodInvocationException(e); |
99 | } |
100 | catch (NoSuchMethodException e) { |
101 | throw new DynamicMethodInvocationException(e); |
102 | } |
103 | |
104 | try { |
105 | return invoker.invoke(); |
106 | } |
107 | catch (InvocationTargetException e) { |
108 | throw new DynamicMethodInvocationException(e); |
109 | } |
110 | catch (IllegalAccessException e) { |
111 | throw new DynamicMethodInvocationException(e); |
112 | } |
113 | } |
114 | |
115 | public void afterPropertiesSet() throws Exception { |
116 | Assert.notNull(targetObject); |
117 | Assert.hasLength(targetMethod); |
118 | Assert.state(targetClassDeclaresTargetMethod(), |
119 | "target class must declare a method with name matching the target method"); |
120 | } |
121 | |
122 | /** |
123 | * @return true if target class declares a method matching target method name |
124 | * with given number of arguments of appropriate type. |
125 | */ |
126 | private boolean targetClassDeclaresTargetMethod() { |
127 | MethodInvoker invoker = createMethodInvoker(targetObject, targetMethod); |
128 | Method[] methods = invoker.getTargetClass().getDeclaredMethods(); |
129 | String targetMethodName = invoker.getTargetMethod(); |
130 | |
131 | for (int i=0; i < methods.length; i++) { |
132 | if (methods[i].getName().equals(targetMethodName)) { |
133 | Class[] params = methods[i].getParameterTypes(); |
134 | if (arguments == null) { |
135 | return true; |
136 | } else if (arguments.length == params.length) { |
137 | boolean argumentsMatchParameters = true; |
138 | for (int j = 0; j < params.length; j++) { |
139 | if (!(params[j].isAssignableFrom(arguments[j].getClass()))) { |
140 | argumentsMatchParameters = false; |
141 | } |
142 | } |
143 | if (argumentsMatchParameters) return true; |
144 | } |
145 | } |
146 | } |
147 | |
148 | return false; |
149 | } |
150 | |
151 | /** |
152 | * @param targetObject the delegate - bean id can be used to set this value in Spring configuration |
153 | */ |
154 | public void setTargetObject(Object targetObject) { |
155 | this.targetObject = targetObject; |
156 | } |
157 | |
158 | /** |
159 | * @param targetMethod name of the method to be invoked on {@link #setTargetObject(Object)}. |
160 | */ |
161 | public void setTargetMethod(String targetMethod) { |
162 | this.targetMethod = targetMethod; |
163 | } |
164 | |
165 | /** |
166 | * @param arguments arguments values for the {{@link #setTargetMethod(String)}. |
167 | * These are not expected to change during the lifetime of the delegator |
168 | * and will be used only when the subclass tries to invoke the target method |
169 | * without providing explicit argument values. |
170 | */ |
171 | public void setArguments(Object[] arguments) { |
172 | this.arguments = arguments; |
173 | } |
174 | } |