| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.spring.xml; |
| 19 |
|
|
| 20 |
|
import org.springframework.beans.SimpleTypeConverter; |
| 21 |
|
import org.springframework.beans.factory.BeanFactory; |
| 22 |
|
import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 23 |
|
|
| 24 |
|
import java.lang.reflect.InvocationTargetException; |
| 25 |
|
import java.util.HashMap; |
| 26 |
|
import java.util.List; |
| 27 |
|
import java.util.Map; |
| 28 |
|
|
| 29 |
|
public class BuilderAction { |
| 30 |
|
private final MethodInfo methodInfo; |
| 31 |
|
private final HashMap<String, Object> parameterValues; |
| 32 |
|
|
| 33 |
80 |
public BuilderAction(MethodInfo methodInfo, HashMap<String, Object> parameterValues) { |
| 34 |
80 |
this.methodInfo = methodInfo; |
| 35 |
80 |
this.parameterValues = parameterValues; |
| 36 |
80 |
} |
| 37 |
|
|
| 38 |
|
public Object invoke(BeanFactory beanFactory, Object rootBuilder, Object contextBuilder) { |
| 39 |
80 |
SimpleTypeConverter converter = new SimpleTypeConverter(); |
| 40 |
80 |
Object args[] = new Object[methodInfo.parameters.size()]; |
| 41 |
80 |
int pos = 0; |
| 42 |
80 |
for (Map.Entry<String, Class> entry : methodInfo.parameters.entrySet()) { |
| 43 |
70 |
String paramName = entry.getKey(); |
| 44 |
70 |
Class paramClass = entry.getValue(); |
| 45 |
70 |
Object value = parameterValues.get(paramName); |
| 46 |
70 |
if (value != null) { |
| 47 |
70 |
value = replaceBeanReferences(beanFactory, rootBuilder, value); |
| 48 |
70 |
args[pos] = converter.convertIfNecessary(value, paramClass); |
| 49 |
|
} |
| 50 |
70 |
} |
| 51 |
|
|
| 52 |
|
try { |
| 53 |
80 |
return methodInfo.method.invoke(contextBuilder, args); |
| 54 |
|
} |
| 55 |
0 |
catch (InvocationTargetException e) { |
| 56 |
0 |
throw new IllegalArgumentException(e.getCause()); |
| 57 |
|
} |
| 58 |
0 |
catch (RuntimeException e) { |
| 59 |
0 |
throw e; |
| 60 |
|
} |
| 61 |
0 |
catch (Throwable e) { |
| 62 |
0 |
throw new IllegalArgumentException(e); |
| 63 |
|
} |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
protected Object replaceBeanReferences(BeanFactory beanFactory, Object rootBuilder, Object value) { |
| 67 |
|
|
| 68 |
70 |
if (value.getClass() == RuntimeBeanReference.class) { |
| 69 |
17 |
String beanName = ((RuntimeBeanReference) value).getBeanName(); |
| 70 |
17 |
value = beanFactory.getBean(beanName); |
| 71 |
|
} |
| 72 |
70 |
if (value.getClass() == BuilderStatement.class) { |
| 73 |
8 |
BuilderStatement bs = (BuilderStatement) value; |
| 74 |
8 |
value = bs.create(beanFactory, rootBuilder); |
| 75 |
|
} |
| 76 |
70 |
if (value instanceof List) { |
| 77 |
0 |
List list = (List) value; |
| 78 |
0 |
for (int i = 0, size = list.size(); i < size; i++) { |
| 79 |
0 |
list.set(i, replaceBeanReferences(beanFactory, rootBuilder, list.get(i))); |
| 80 |
|
} |
| 81 |
|
} |
| 82 |
70 |
return value; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
public String getName() { |
| 86 |
0 |
return methodInfo.getName(); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
public MethodInfo getMethodInfo() { |
| 90 |
77 |
return methodInfo; |
| 91 |
|
} |
| 92 |
|
} |