1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.bean; |
18 |
|
|
19 |
|
import java.lang.reflect.Method; |
20 |
|
|
21 |
|
import org.apache.camel.CamelContext; |
22 |
|
import org.apache.camel.Exchange; |
23 |
|
import org.apache.camel.Message; |
24 |
|
import org.apache.camel.Processor; |
25 |
|
import org.apache.camel.spi.Registry; |
26 |
|
import org.apache.camel.util.ObjectHelper; |
27 |
|
import org.apache.commons.logging.Log; |
28 |
|
import org.apache.commons.logging.LogFactory; |
29 |
|
|
30 |
|
import static org.apache.camel.util.ObjectHelper.isNullOrBlank; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class BeanProcessor implements Processor { |
39 |
|
public static final String METHOD_NAME = "org.apache.camel.MethodName"; |
40 |
3 |
private static final Log LOG = LogFactory.getLog(BeanProcessor.class); |
41 |
|
|
42 |
|
private final Object pojo; |
43 |
|
private final BeanInfo beanInfo; |
44 |
|
private Method method; |
45 |
|
private String methodName; |
46 |
|
|
47 |
6 |
public BeanProcessor(Object pojo, BeanInfo beanInfo) { |
48 |
6 |
this.pojo = pojo; |
49 |
6 |
this.beanInfo = beanInfo; |
50 |
6 |
} |
51 |
|
|
52 |
|
public BeanProcessor(Object pojo, ParameterMappingStrategy parameterMappingStrategy) { |
53 |
6 |
this(pojo, new BeanInfo(pojo.getClass(), parameterMappingStrategy)); |
54 |
6 |
} |
55 |
|
|
56 |
|
public BeanProcessor(Object pojo, CamelContext camelContext) { |
57 |
0 |
this(pojo, createParameterMappingStrategy(camelContext)); |
58 |
0 |
} |
59 |
|
|
60 |
|
public static ParameterMappingStrategy createParameterMappingStrategy(CamelContext camelContext) { |
61 |
6 |
Registry registry = camelContext.getRegistry(); |
62 |
6 |
ParameterMappingStrategy answer = registry.lookup(ParameterMappingStrategy.class.getName(), |
63 |
|
ParameterMappingStrategy.class); |
64 |
6 |
if (answer == null) { |
65 |
6 |
answer = new DefaultParameterMappingStrategy(); |
66 |
|
} |
67 |
6 |
return answer; |
68 |
|
} |
69 |
|
@Override |
70 |
|
public String toString() { |
71 |
0 |
String description = method != null ? " " + method : ""; |
72 |
0 |
return "BeanProcessor[" + pojo + description + "]"; |
73 |
|
} |
74 |
|
|
75 |
|
public void process(Exchange exchange) throws Exception { |
76 |
18 |
if (LOG.isDebugEnabled()) { |
77 |
0 |
LOG.debug(">>>> invoking method for: " + exchange); |
78 |
|
} |
79 |
18 |
Message in = exchange.getIn(); |
80 |
18 |
BeanInvocation beanInvoke = in.getBody(BeanInvocation.class); |
81 |
18 |
if (beanInvoke != null) { |
82 |
18 |
beanInvoke.invoke(pojo, exchange); |
83 |
18 |
return; |
84 |
|
} |
85 |
|
|
86 |
|
MethodInvocation invocation; |
87 |
0 |
if (method != null) { |
88 |
0 |
invocation = beanInfo.createInvocation(method, pojo, exchange); |
89 |
0 |
} else { |
90 |
|
|
91 |
0 |
if (ObjectHelper.isNotNullAndNonEmpty(methodName)) { |
92 |
0 |
if (isNullOrBlank(in.getHeader(METHOD_NAME, String.class))) { |
93 |
0 |
in.setHeader(METHOD_NAME, methodName); |
94 |
|
} |
95 |
|
} |
96 |
0 |
invocation = beanInfo.createInvocation(pojo, exchange); |
97 |
|
} |
98 |
0 |
if (invocation == null) { |
99 |
0 |
throw new IllegalStateException("No method invocation could be created"); |
100 |
|
} |
101 |
|
try { |
102 |
0 |
Object value = invocation.proceed(); |
103 |
0 |
if (value != null) { |
104 |
0 |
exchange.getIn().setBody(value); |
105 |
|
} |
106 |
0 |
} catch (Exception e) { |
107 |
0 |
throw e; |
108 |
0 |
} catch (Throwable throwable) { |
109 |
0 |
throw new Exception(throwable); |
110 |
0 |
} |
111 |
0 |
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
public Method getMethod() { |
117 |
0 |
return method; |
118 |
|
} |
119 |
|
|
120 |
|
public void setMethod(Method method) { |
121 |
0 |
this.method = method; |
122 |
0 |
} |
123 |
|
|
124 |
|
public String getMethodName() { |
125 |
0 |
return methodName; |
126 |
|
} |
127 |
|
|
128 |
|
public void setMethodName(String methodName) { |
129 |
0 |
this.methodName = methodName; |
130 |
0 |
} |
131 |
|
} |