1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.tcp.protocols; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.RequestContext; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.transformer.wire.WireFormat; |
17 | |
import org.mule.transformer.wire.SerializedMuleMessageWireFormat; |
18 | |
|
19 | |
import java.io.ByteArrayInputStream; |
20 | |
import java.io.ByteArrayOutputStream; |
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
class MuleMessageWorker |
29 | |
{ |
30 | 0 | private static WireFormat wireFormat = new SerializedMuleMessageWireFormat(); |
31 | |
|
32 | |
private MuleMessageWorker() |
33 | 0 | { |
34 | |
|
35 | |
|
36 | 0 | } |
37 | |
|
38 | |
public static byte[] doWrite() throws IOException |
39 | |
{ |
40 | |
|
41 | 0 | DefaultMuleMessage msg = (DefaultMuleMessage) RequestContext.getEvent().getMessage(); |
42 | 0 | wireFormat.setMuleContext(RequestContext.getEvent().getMuleContext()); |
43 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
44 | |
try |
45 | |
{ |
46 | 0 | wireFormat.write(baos, msg, msg.getEncoding()); |
47 | |
} |
48 | 0 | catch (MuleException e) |
49 | |
{ |
50 | 0 | throw new IOException(e.getDetailedMessage()); |
51 | 0 | } |
52 | 0 | return baos.toByteArray(); |
53 | |
} |
54 | |
|
55 | |
public static Object doRead(Object message) throws IOException |
56 | |
{ |
57 | 0 | if(message==null) return null; |
58 | |
|
59 | |
InputStream in; |
60 | 0 | if(message instanceof byte[]) |
61 | |
{ |
62 | 0 | in = new ByteArrayInputStream((byte[])message); |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 0 | in = (InputStream)message; |
67 | |
} |
68 | |
|
69 | |
try |
70 | |
{ |
71 | 0 | return wireFormat.read(in); |
72 | |
} |
73 | 0 | catch (MuleException e) |
74 | |
{ |
75 | 0 | throw new IOException(e.getDetailedMessage()); |
76 | |
} |
77 | |
|
78 | |
} |
79 | |
|
80 | |
} |