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 super(endpoint);
38 this.connector = (StreamConnector)endpoint.getConnector();
39
40
41 if (connector instanceof SystemStreamConnector)
42 {
43 SystemStreamConnector ssc = (SystemStreamConnector)connector;
44
45 String outputMessage = (String)endpoint.getProperties().get("outputMessage");
46 if (outputMessage != null)
47 {
48 ssc.setOutputMessage(outputMessage);
49 }
50 }
51 }
52
53 protected synchronized void doDispatch(UMOEvent event) throws Exception
54 {
55 OutputStream out = connector.getOutputStream();
56
57 if (out == null)
58 {
59 throw new DispatchException(
60 StreamMessages.couldNotFindStreamWithName(event.getEndpoint().getEndpointURI().getAddress()),
61 event.getMessage(), event.getEndpoint());
62 }
63
64 if (connector instanceof SystemStreamConnector)
65 {
66 SystemStreamConnector ssc = (SystemStreamConnector)connector;
67 if (StringUtils.isNotBlank(ssc.getOutputMessage()))
68 {
69 out.write(ssc.getOutputMessage().getBytes());
70 }
71 }
72
73 Object data = event.getTransformedMessage();
74 if (data instanceof byte[])
75 {
76 out.write((byte[])data);
77 }
78 else
79 {
80 out.write(data.toString().getBytes());
81 }
82
83 out.flush();
84 }
85
86
87
88
89
90
91 protected UMOMessage doSend(UMOEvent event) throws Exception
92 {
93 doDispatch(event);
94 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 throw new UnsupportedOperationException("doReceive");
111 }
112
113 protected void doDispose()
114 {
115
116 }
117
118 protected void doConnect() throws Exception
119 {
120
121 }
122
123 protected void doDisconnect() throws Exception
124 {
125
126 }
127
128
129
130 }