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