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