1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.cxf; |
18 |
|
|
19 |
|
import java.io.IOException; |
20 |
|
import java.util.concurrent.CountDownLatch; |
21 |
|
|
22 |
|
import org.apache.camel.Exchange; |
23 |
|
import org.apache.camel.RuntimeCamelException; |
24 |
|
import org.apache.camel.impl.DefaultProducer; |
25 |
|
import org.apache.cxf.message.ExchangeImpl; |
26 |
|
import org.apache.cxf.message.Message; |
27 |
|
import org.apache.cxf.message.MessageImpl; |
28 |
|
import org.apache.cxf.service.model.EndpointInfo; |
29 |
|
import org.apache.cxf.transport.Conduit; |
30 |
|
import org.apache.cxf.transport.Destination; |
31 |
|
import org.apache.cxf.transport.MessageObserver; |
32 |
|
import org.apache.cxf.transport.local.LocalConduit; |
33 |
|
import org.apache.cxf.transport.local.LocalTransportFactory; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
public class CxfProducer extends DefaultProducer { |
41 |
|
private CxfEndpoint endpoint; |
42 |
|
private final LocalTransportFactory transportFactory; |
43 |
|
private Destination destination; |
44 |
|
private Conduit conduit; |
45 |
0 |
private ResultFuture future = new ResultFuture(); |
46 |
|
|
47 |
|
public CxfProducer(CxfEndpoint endpoint, LocalTransportFactory transportFactory) { |
48 |
0 |
super(endpoint); |
49 |
0 |
this.endpoint = endpoint; |
50 |
0 |
this.transportFactory = transportFactory; |
51 |
0 |
} |
52 |
|
|
53 |
|
public void process(Exchange exchange) { |
54 |
0 |
CxfExchange cxfExchange = endpoint.toExchangeType(exchange); |
55 |
0 |
process(cxfExchange); |
56 |
0 |
} |
57 |
|
|
58 |
|
public void process(CxfExchange exchange) { |
59 |
|
try { |
60 |
0 |
CxfBinding binding = endpoint.getBinding(); |
61 |
0 |
MessageImpl m = binding.createCxfMessage(exchange); |
62 |
0 |
ExchangeImpl e = new ExchangeImpl(); |
63 |
0 |
e.setInMessage(m); |
64 |
0 |
m.put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE); |
65 |
0 |
m.setDestination(destination); |
66 |
0 |
synchronized (conduit) { |
67 |
0 |
conduit.prepare(m); |
68 |
|
|
69 |
|
|
70 |
0 |
if (endpoint.isInOut()) { |
71 |
0 |
Message response = future.getResponse(); |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
0 |
response = e.getOutMessage(); |
76 |
0 |
binding.storeCxfResponse(exchange, response); |
77 |
|
} |
78 |
0 |
} |
79 |
0 |
} catch (IOException e) { |
80 |
0 |
throw new RuntimeCamelException(e); |
81 |
0 |
} |
82 |
0 |
} |
83 |
|
|
84 |
|
@Override |
85 |
|
protected void doStart() throws Exception { |
86 |
0 |
super.doStart(); |
87 |
0 |
EndpointInfo endpointInfo = endpoint.getEndpointInfo(); |
88 |
0 |
destination = transportFactory.getDestination(endpointInfo); |
89 |
|
|
90 |
|
|
91 |
0 |
conduit = transportFactory.getConduit(endpointInfo); |
92 |
0 |
conduit.setMessageObserver(future); |
93 |
0 |
} |
94 |
|
|
95 |
|
@Override |
96 |
|
protected void doStop() throws Exception { |
97 |
0 |
super.doStop(); |
98 |
|
|
99 |
0 |
if (conduit != null) { |
100 |
0 |
conduit.close(); |
101 |
|
} |
102 |
0 |
} |
103 |
|
|
104 |
0 |
protected class ResultFuture implements MessageObserver { |
105 |
|
Message response; |
106 |
0 |
CountDownLatch latch = new CountDownLatch(1); |
107 |
|
|
108 |
|
public Message getResponse() { |
109 |
0 |
while (response == null) { |
110 |
|
try { |
111 |
0 |
latch.await(); |
112 |
0 |
} catch (InterruptedException e) { |
113 |
|
|
114 |
0 |
} |
115 |
0 |
} |
116 |
0 |
return response; |
117 |
|
} |
118 |
|
|
119 |
|
public synchronized void onMessage(Message message) { |
120 |
|
try { |
121 |
0 |
message.remove(LocalConduit.DIRECT_DISPATCH); |
122 |
0 |
this.response = message; |
123 |
|
} finally { |
124 |
0 |
latch.countDown(); |
125 |
0 |
} |
126 |
0 |
} |
127 |
|
} |
128 |
|
} |