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 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 {
56 this.streamOk = streamOk;
57 }
58
59 public void write(OutputStream os, Object data) throws IOException
60 {
61 if (data instanceof InputStream)
62 {
63 if (streamOk)
64 {
65 IOUtils.copyLarge((InputStream) data, os);
66 os.flush();
67 os.close();
68 }
69 else
70 {
71 throw new IOException("TCP protocol " + ClassUtils.getSimpleName(getClass())
72 + " cannot handle streaming");
73 }
74 }
75 else if (data instanceof MessageAdapter)
76 {
77 write(os, ((MessageAdapter) data).getPayload());
78 }
79 else if (data instanceof byte[])
80 {
81 writeByteArray(os, (byte[]) data);
82 }
83 else if (data instanceof String)
84 {
85
86
87 writeByteArray(os, ((String) data).getBytes());
88 }
89 else if (data instanceof Serializable)
90 {
91 writeByteArray(os, SerializationUtils.serialize((Serializable) data));
92 }
93 else
94 {
95 throw new IllegalArgumentException("Cannot serialize data: " + data);
96 }
97 }
98
99 protected void writeByteArray(OutputStream os, byte[] data) throws IOException
100 {
101 os.write(data);
102 }
103
104
105
106
107
108
109
110
111
112 protected int safeRead(InputStream is, byte[] buffer) throws IOException
113 {
114 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 len = is.read(buffer, 0, size);
134 if (0 == len)
135 {
136
137
138 try
139 {
140 Thread.sleep(PAUSE_PERIOD);
141 }
142 catch (InterruptedException e)
143 {
144
145 }
146 }
147 }
148 while (0 == len);
149 return len;
150 }
151 catch (SocketException e)
152 {
153
154 logger.info("Socket exception occured: " + e.getMessage());
155 return EOF;
156 }
157 catch (SocketTimeoutException e)
158 {
159 logger.debug("Socket timeout.");
160 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 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 int len = safeRead(source, buffer, size);
191 if (len > 0)
192 {
193 dest.write(buffer, 0, len);
194 }
195 return len;
196 }
197
198 protected byte[] nullEmptyArray(byte[] data)
199 {
200 if (0 == data.length)
201 {
202 return null;
203 }
204 else
205 {
206 return data;
207 }
208 }
209
210 public ResponseOutputStream createResponse(Socket socket) throws IOException
211 {
212 return new ResponseOutputStream(socket, new ProtocolStream(this, streamOk, socket.getOutputStream()));
213 }
214
215 }