1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.rmi; |
18 |
|
|
19 |
|
import java.lang.reflect.InvocationHandler; |
20 |
|
import java.lang.reflect.InvocationTargetException; |
21 |
|
import java.lang.reflect.Method; |
22 |
|
import java.lang.reflect.Proxy; |
23 |
|
import java.rmi.Remote; |
24 |
|
import java.rmi.registry.Registry; |
25 |
|
import java.rmi.server.UnicastRemoteObject; |
26 |
|
|
27 |
|
import org.apache.camel.Processor; |
28 |
|
import org.apache.camel.component.bean.BeanExchange; |
29 |
|
import org.apache.camel.component.bean.BeanInvocation; |
30 |
|
import org.apache.camel.impl.DefaultConsumer; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class RmiConsumer extends DefaultConsumer<BeanExchange> implements InvocationHandler { |
39 |
|
|
40 |
|
private final RmiEndpoint endpoint; |
41 |
|
private Remote stub; |
42 |
|
private Remote proxy; |
43 |
|
|
44 |
|
public RmiConsumer(RmiEndpoint endpoint, Processor processor) { |
45 |
1 |
super(endpoint, processor); |
46 |
1 |
this.endpoint = endpoint; |
47 |
|
|
48 |
1 |
} |
49 |
|
|
50 |
|
@Override |
51 |
|
protected void doStart() throws Exception { |
52 |
1 |
Class[] interfaces = new Class[endpoint.getRemoteInterfaces().size()]; |
53 |
1 |
endpoint.getRemoteInterfaces().toArray(interfaces); |
54 |
1 |
proxy = (Remote)Proxy.newProxyInstance(endpoint.getClassLoader(), interfaces, this); |
55 |
1 |
stub = UnicastRemoteObject.exportObject(proxy, endpoint.getPort()); |
56 |
|
|
57 |
|
try { |
58 |
1 |
Registry registry = endpoint.getRegistry(); |
59 |
1 |
String name = endpoint.getName(); |
60 |
1 |
registry.bind(name, stub); |
61 |
|
|
62 |
0 |
} catch (Exception e) { |
63 |
|
try { |
64 |
0 |
UnicastRemoteObject.unexportObject(stub, true); |
65 |
0 |
} catch (Throwable ignore) { |
66 |
0 |
} |
67 |
0 |
stub = null; |
68 |
0 |
throw e; |
69 |
1 |
} |
70 |
1 |
super.doStart(); |
71 |
1 |
} |
72 |
|
|
73 |
|
@Override |
74 |
|
protected void doStop() throws Exception { |
75 |
1 |
super.doStop(); |
76 |
|
try { |
77 |
1 |
Registry registry = endpoint.getRegistry(); |
78 |
1 |
registry.unbind(endpoint.getName()); |
79 |
0 |
} catch (Throwable e) { |
80 |
1 |
} |
81 |
1 |
UnicastRemoteObject.unexportObject(proxy, true); |
82 |
1 |
} |
83 |
|
|
84 |
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
85 |
1 |
if (!isStarted()) { |
86 |
0 |
throw new IllegalStateException("The endpoint is not active: " + getEndpoint().getEndpointUri()); |
87 |
|
} |
88 |
1 |
BeanInvocation invocation = new BeanInvocation(proxy, method, args); |
89 |
1 |
BeanExchange exchange = getEndpoint().createExchange(); |
90 |
1 |
exchange.setInvocation(invocation); |
91 |
1 |
getProcessor().process(exchange); |
92 |
1 |
Throwable fault = exchange.getException(); |
93 |
1 |
if (fault != null) { |
94 |
0 |
throw new InvocationTargetException(fault); |
95 |
|
} |
96 |
1 |
return exchange.getOut().getBody(); |
97 |
|
} |
98 |
|
|
99 |
|
public Remote getProxy() { |
100 |
0 |
return proxy; |
101 |
|
} |
102 |
|
|
103 |
|
public Remote getStub() { |
104 |
0 |
return stub; |
105 |
|
} |
106 |
|
} |