1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.tcp; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.providers.AbstractMessageDispatcher; |
15 | |
import org.mule.umo.UMOEvent; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
18 | |
import org.mule.umo.transformer.TransformerException; |
19 | |
|
20 | |
import java.io.BufferedInputStream; |
21 | |
import java.io.BufferedOutputStream; |
22 | |
import java.io.DataInputStream; |
23 | |
import java.io.IOException; |
24 | |
import java.net.Socket; |
25 | |
import java.net.SocketTimeoutException; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class TcpMessageDispatcher extends AbstractMessageDispatcher |
32 | |
{ |
33 | |
private final TcpConnector connector; |
34 | |
|
35 | |
public TcpMessageDispatcher(UMOImmutableEndpoint endpoint) |
36 | |
{ |
37 | 0 | super(endpoint); |
38 | 0 | this.connector = (TcpConnector) endpoint.getConnector(); |
39 | 0 | } |
40 | |
|
41 | |
protected synchronized void doDispatch(UMOEvent event) throws Exception |
42 | |
{ |
43 | 0 | Socket socket = connector.getSocket(event.getEndpoint()); |
44 | |
try |
45 | |
{ |
46 | 0 | dispatchToSocket(socket, event); |
47 | |
} |
48 | |
finally |
49 | |
{ |
50 | 0 | connector.releaseSocket(socket, event.getEndpoint()); |
51 | 0 | } |
52 | 0 | } |
53 | |
|
54 | |
protected synchronized UMOMessage doSend(UMOEvent event) throws Exception |
55 | |
{ |
56 | 0 | Socket socket = connector.getSocket(event.getEndpoint()); |
57 | |
try |
58 | |
{ |
59 | 0 | dispatchToSocket(socket, event); |
60 | |
|
61 | 0 | if (useRemoteSync(event)) |
62 | |
{ |
63 | |
try |
64 | |
{ |
65 | 0 | Object result = receiveFromSocket(socket, event.getTimeout()); |
66 | 0 | if (result == null) |
67 | |
{ |
68 | 0 | return null; |
69 | |
} |
70 | 0 | return new MuleMessage(connector.getMessageAdapter(result)); |
71 | |
} |
72 | 0 | catch (SocketTimeoutException e) |
73 | |
{ |
74 | |
|
75 | 0 | logger.info("Socket timed out normally while doing a synchronous receive on endpointUri: " |
76 | |
+ event.getEndpoint().getEndpointURI()); |
77 | 0 | return null; |
78 | |
} |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 0 | return event.getMessage(); |
83 | |
} |
84 | |
} |
85 | |
finally |
86 | |
{ |
87 | 0 | connector.releaseSocket(socket, event.getEndpoint()); |
88 | |
} |
89 | |
} |
90 | |
|
91 | |
|
92 | |
private void dispatchToSocket(Socket socket, UMOEvent event) throws Exception |
93 | |
{ |
94 | 0 | Object payload = event.getTransformedMessage(); |
95 | 0 | write(socket, payload); |
96 | 0 | } |
97 | |
|
98 | |
private void write(Socket socket, Object data) throws IOException, TransformerException |
99 | |
{ |
100 | 0 | BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); |
101 | 0 | connector.getTcpProtocol().write(bos, data); |
102 | 0 | bos.flush(); |
103 | 0 | } |
104 | |
|
105 | |
private Object receiveFromSocket(Socket socket, int timeout) throws IOException |
106 | |
{ |
107 | 0 | DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream())); |
108 | 0 | if (timeout >= 0) |
109 | |
{ |
110 | 0 | socket.setSoTimeout(timeout); |
111 | |
} |
112 | 0 | return connector.getTcpProtocol().read(dis); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
protected UMOMessage doReceive(long timeout) throws Exception |
127 | |
{ |
128 | 0 | Socket socket = connector.getSocket(endpoint); |
129 | |
try |
130 | |
{ |
131 | 0 | Object result = receiveFromSocket(socket, (int)timeout); |
132 | 0 | if (result == null) |
133 | |
{ |
134 | 0 | return null; |
135 | |
} |
136 | 0 | return new MuleMessage(connector.getMessageAdapter(result)); |
137 | |
} |
138 | 0 | catch (SocketTimeoutException e) |
139 | |
{ |
140 | |
|
141 | 0 | if (logger.isDebugEnabled()) |
142 | |
{ |
143 | 0 | logger.debug("Socket timed out normally while doing a synchronous receive on endpointUri: " |
144 | |
+ endpoint.getEndpointURI()); |
145 | |
} |
146 | 0 | return null; |
147 | |
} |
148 | |
finally |
149 | |
{ |
150 | 0 | connector.releaseSocket(socket, endpoint); |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
protected synchronized void doDispose() |
155 | |
{ |
156 | |
try |
157 | |
{ |
158 | 0 | doDisconnect(); |
159 | |
} |
160 | 0 | catch (Exception e) |
161 | |
{ |
162 | 0 | logger.error("Failed to shutdown the dispatcher.", e); |
163 | 0 | } |
164 | 0 | } |
165 | |
|
166 | |
protected void doConnect() throws Exception |
167 | |
{ |
168 | |
|
169 | 0 | if (connector.isValidateConnections()) |
170 | |
{ |
171 | 0 | Socket socket = connector.getSocket(endpoint); |
172 | 0 | connector.releaseSocket(socket, endpoint); |
173 | |
} |
174 | 0 | } |
175 | |
|
176 | |
protected void doDisconnect() throws Exception |
177 | |
{ |
178 | |
|
179 | 0 | } |
180 | |
|
181 | |
} |