1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.pojo; |
18 |
|
|
19 |
|
import org.apache.camel.Consumer; |
20 |
|
import org.apache.camel.Exchange; |
21 |
|
import org.apache.camel.NoSuchEndpointException; |
22 |
|
import org.apache.camel.Processor; |
23 |
|
import org.apache.camel.Producer; |
24 |
|
import org.apache.camel.Component; |
25 |
|
import org.apache.camel.impl.DefaultEndpoint; |
26 |
|
import org.apache.camel.impl.DefaultProducer; |
27 |
|
import org.apache.camel.spi.Provider; |
28 |
|
|
29 |
|
import java.lang.reflect.InvocationTargetException; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
0 |
public class PojoEndpoint extends DefaultEndpoint<PojoExchange> { |
38 |
|
private Object pojo; |
39 |
|
|
40 |
|
public PojoEndpoint(String uri, Component component, Object pojo) { |
41 |
2 |
super(uri, component); |
42 |
2 |
this.pojo = pojo; |
43 |
2 |
} |
44 |
|
|
45 |
|
public Producer<PojoExchange> createProducer() throws Exception { |
46 |
2 |
final Object pojo = getPojo(); |
47 |
2 |
if (pojo == null) { |
48 |
0 |
throw new NoPojoAvailableException(this); |
49 |
|
} |
50 |
|
|
51 |
2 |
return new DefaultProducer(this) { |
52 |
2 |
public void process(Exchange exchange) { |
53 |
5 |
PojoExchange pojoExchange = toExchangeType(exchange); |
54 |
5 |
invoke(pojo, pojoExchange); |
55 |
5 |
exchange.copyFrom(pojoExchange); |
56 |
5 |
} |
57 |
|
}; |
58 |
|
} |
59 |
|
|
60 |
|
public Consumer<PojoExchange> createConsumer(Processor processor) throws Exception { |
61 |
0 |
throw new Exception("You cannot consume from pojo endpoints."); |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
public static void invoke(Object pojo, PojoExchange exchange) { |
70 |
5 |
PojoInvocation invocation = exchange.getInvocation(); |
71 |
|
try { |
72 |
5 |
Object response = invocation.getMethod().invoke(pojo, invocation.getArgs()); |
73 |
5 |
exchange.getOut().setBody(response); |
74 |
|
} |
75 |
0 |
catch (InvocationTargetException e) { |
76 |
0 |
exchange.setException(e.getCause()); |
77 |
|
} |
78 |
0 |
catch (RuntimeException e) { |
79 |
0 |
throw e; |
80 |
|
} |
81 |
0 |
catch (Throwable e) { |
82 |
0 |
throw new RuntimeException(e); |
83 |
5 |
} |
84 |
5 |
} |
85 |
|
|
86 |
|
public PojoExchange createExchange() { |
87 |
0 |
return new PojoExchange(getContext()); |
88 |
|
} |
89 |
|
|
90 |
|
public boolean isSingleton() { |
91 |
2 |
return true; |
92 |
|
} |
93 |
|
|
94 |
|
public Object getPojo() { |
95 |
2 |
return pojo; |
96 |
|
} |
97 |
|
|
98 |
|
public void setPojo(Object pojo) { |
99 |
0 |
this.pojo = pojo; |
100 |
0 |
} |
101 |
|
} |