1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.tcp.protocols; |
12 | |
|
13 | |
import org.mule.ResponseOutputStream; |
14 | |
import org.mule.api.transport.MessageAdapter; |
15 | |
import org.mule.transport.tcp.TcpProtocol; |
16 | |
import org.mule.util.ClassUtils; |
17 | |
import org.mule.util.IOUtils; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.io.InputStream; |
21 | |
import java.io.OutputStream; |
22 | |
import java.io.Serializable; |
23 | |
import java.net.Socket; |
24 | |
import java.net.SocketException; |
25 | |
import java.net.SocketTimeoutException; |
26 | |
|
27 | |
import org.apache.commons.lang.SerializationUtils; |
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public abstract class AbstractByteProtocol implements TcpProtocol |
44 | |
{ |
45 | 2 | private static final Log logger = LogFactory.getLog(DirectProtocol.class); |
46 | |
private static final long PAUSE_PERIOD = 100; |
47 | |
public static final int EOF = -1; |
48 | |
|
49 | |
|
50 | |
public static final boolean STREAM_OK = true; |
51 | |
public static final boolean NO_STREAM = false; |
52 | |
private boolean streamOk; |
53 | |
|
54 | |
public AbstractByteProtocol(boolean streamOk) |
55 | 404 | { |
56 | 404 | this.streamOk = streamOk; |
57 | 404 | } |
58 | |
|
59 | |
public void write(OutputStream os, Object data) throws IOException |
60 | |
{ |
61 | 1904 | if (data instanceof InputStream) |
62 | |
{ |
63 | 14 | if (streamOk) |
64 | |
{ |
65 | 14 | IOUtils.copyLarge((InputStream) data, os); |
66 | 14 | os.flush(); |
67 | 14 | os.close(); |
68 | |
} |
69 | |
else |
70 | |
{ |
71 | 0 | throw new IOException("TCP protocol " + ClassUtils.getSimpleName(getClass()) |
72 | |
+ " cannot handle streaming"); |
73 | |
} |
74 | |
} |
75 | 1890 | else if (data instanceof MessageAdapter) |
76 | |
{ |
77 | 48 | write(os, ((MessageAdapter) data).getPayload()); |
78 | |
} |
79 | 1842 | else if (data instanceof byte[]) |
80 | |
{ |
81 | 1426 | writeByteArray(os, (byte[]) data); |
82 | |
} |
83 | 416 | else if (data instanceof String) |
84 | |
{ |
85 | |
|
86 | |
|
87 | 416 | writeByteArray(os, ((String) data).getBytes()); |
88 | |
} |
89 | 0 | else if (data instanceof Serializable) |
90 | |
{ |
91 | 0 | writeByteArray(os, SerializationUtils.serialize((Serializable) data)); |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | throw new IllegalArgumentException("Cannot serialize data: " + data); |
96 | |
} |
97 | 1862 | } |
98 | |
|
99 | |
protected void writeByteArray(OutputStream os, byte[] data) throws IOException |
100 | |
{ |
101 | 1367 | os.write(data); |
102 | 1367 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
protected int safeRead(InputStream is, byte[] buffer) throws IOException |
113 | |
{ |
114 | 172 | return safeRead(is, buffer, buffer.length); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
protected int safeRead(InputStream is, byte[] buffer, int size) throws IOException |
127 | |
{ |
128 | |
int len; |
129 | |
try |
130 | |
{ |
131 | |
do |
132 | |
{ |
133 | 4761 | len = is.read(buffer, 0, size); |
134 | 4751 | if (0 == len) |
135 | |
{ |
136 | |
|
137 | |
|
138 | |
try |
139 | |
{ |
140 | 0 | Thread.sleep(PAUSE_PERIOD); |
141 | |
} |
142 | 0 | catch (InterruptedException e) |
143 | |
{ |
144 | |
|
145 | 0 | } |
146 | |
} |
147 | |
} |
148 | 4751 | while (0 == len); |
149 | 4751 | return len; |
150 | |
} |
151 | 6 | catch (SocketException e) |
152 | |
{ |
153 | |
|
154 | 6 | logger.info("Socket exception occured: " + e.getMessage()); |
155 | 6 | return EOF; |
156 | |
} |
157 | 4 | catch (SocketTimeoutException e) |
158 | |
{ |
159 | 4 | logger.debug("Socket timeout."); |
160 | 4 | return EOF; |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
protected int copy(InputStream source, byte[] buffer, OutputStream dest) throws IOException |
174 | |
{ |
175 | 0 | return copy(source, buffer, dest, buffer.length); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
protected int copy(InputStream source, byte[] buffer, OutputStream dest, int size) throws IOException |
189 | |
{ |
190 | 4589 | int len = safeRead(source, buffer, size); |
191 | 4589 | if (len > 0) |
192 | |
{ |
193 | 1833 | dest.write(buffer, 0, len); |
194 | |
} |
195 | 4589 | return len; |
196 | |
} |
197 | |
|
198 | |
protected byte[] nullEmptyArray(byte[] data) |
199 | |
{ |
200 | 3311 | if (0 == data.length) |
201 | |
{ |
202 | 1437 | return null; |
203 | |
} |
204 | |
else |
205 | |
{ |
206 | 1874 | return data; |
207 | |
} |
208 | |
} |
209 | |
|
210 | |
public ResponseOutputStream createResponse(Socket socket) throws IOException |
211 | |
{ |
212 | 1426 | return new ResponseOutputStream(socket, new ProtocolStream(this, streamOk, socket.getOutputStream())); |
213 | |
} |
214 | |
|
215 | |
} |