1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.stream; |
12 | |
|
13 | |
import org.mule.providers.AbstractMessageDispatcher; |
14 | |
import org.mule.providers.stream.i18n.StreamMessages; |
15 | |
import org.mule.umo.UMOEvent; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
18 | |
import org.mule.umo.provider.DispatchException; |
19 | |
import org.mule.util.StringUtils; |
20 | |
|
21 | |
import java.io.OutputStream; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class StreamMessageDispatcher extends AbstractMessageDispatcher |
32 | |
{ |
33 | |
private final StreamConnector connector; |
34 | |
|
35 | |
public StreamMessageDispatcher(UMOImmutableEndpoint endpoint) |
36 | |
{ |
37 | 0 | super(endpoint); |
38 | 0 | this.connector = (StreamConnector)endpoint.getConnector(); |
39 | |
|
40 | |
|
41 | 0 | if (connector instanceof SystemStreamConnector) |
42 | |
{ |
43 | 0 | SystemStreamConnector ssc = (SystemStreamConnector)connector; |
44 | |
|
45 | 0 | String outputMessage = (String)endpoint.getProperties().get("outputMessage"); |
46 | 0 | if (outputMessage != null) |
47 | |
{ |
48 | 0 | ssc.setOutputMessage(outputMessage); |
49 | |
} |
50 | |
} |
51 | 0 | } |
52 | |
|
53 | |
protected synchronized void doDispatch(UMOEvent event) throws Exception |
54 | |
{ |
55 | 0 | OutputStream out = connector.getOutputStream(); |
56 | |
|
57 | 0 | if (out == null) |
58 | |
{ |
59 | 0 | throw new DispatchException( |
60 | |
StreamMessages.couldNotFindStreamWithName(event.getEndpoint().getEndpointURI().getAddress()), |
61 | |
event.getMessage(), event.getEndpoint()); |
62 | |
} |
63 | |
|
64 | 0 | if (connector instanceof SystemStreamConnector) |
65 | |
{ |
66 | 0 | SystemStreamConnector ssc = (SystemStreamConnector)connector; |
67 | 0 | if (StringUtils.isNotBlank(ssc.getOutputMessage())) |
68 | |
{ |
69 | 0 | out.write(ssc.getOutputMessage().getBytes()); |
70 | |
} |
71 | |
} |
72 | |
|
73 | 0 | Object data = event.getTransformedMessage(); |
74 | 0 | if (data instanceof byte[]) |
75 | |
{ |
76 | 0 | out.write((byte[])data); |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | 0 | out.write(data.toString().getBytes()); |
81 | |
} |
82 | |
|
83 | 0 | out.flush(); |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
protected UMOMessage doSend(UMOEvent event) throws Exception |
92 | |
{ |
93 | 0 | doDispatch(event); |
94 | 0 | return event.getMessage(); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
protected UMOMessage doReceive(long timeout) throws Exception |
109 | |
{ |
110 | 0 | throw new UnsupportedOperationException("doReceive"); |
111 | |
} |
112 | |
|
113 | |
protected void doDispose() |
114 | |
{ |
115 | |
|
116 | 0 | } |
117 | |
|
118 | |
protected void doConnect() throws Exception |
119 | |
{ |
120 | |
|
121 | 0 | } |
122 | |
|
123 | |
protected void doDisconnect() throws Exception |
124 | |
{ |
125 | |
|
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
} |