1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.stdio; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.endpoint.OutboundEndpoint; |
16 | |
import org.mule.api.transport.DispatchException; |
17 | |
import org.mule.transport.AbstractMessageDispatcher; |
18 | |
import org.mule.transport.stdio.i18n.StdioMessages; |
19 | |
import org.mule.util.StringUtils; |
20 | |
|
21 | |
import java.io.OutputStream; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class StdioMessageDispatcher extends AbstractMessageDispatcher |
32 | |
{ |
33 | |
private final StdioConnector connector; |
34 | |
|
35 | |
public StdioMessageDispatcher(OutboundEndpoint endpoint) |
36 | |
{ |
37 | 0 | super(endpoint); |
38 | 0 | this.connector = (StdioConnector) endpoint.getConnector(); |
39 | |
|
40 | |
|
41 | 0 | if (connector instanceof PromptStdioConnector) |
42 | |
{ |
43 | 0 | PromptStdioConnector ssc = (PromptStdioConnector)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 | |
@Override |
54 | |
protected synchronized void doDispatch(MuleEvent event) throws Exception |
55 | |
{ |
56 | 0 | OutputStream out = connector.getOutputStream(); |
57 | |
|
58 | 0 | if (out == null) |
59 | |
{ |
60 | 0 | throw new DispatchException(StdioMessages.couldNotFindStreamWithName(event.getEndpoint()), |
61 | |
event, (OutboundEndpoint) endpoint); |
62 | |
} |
63 | |
|
64 | 0 | if (connector instanceof PromptStdioConnector) |
65 | |
{ |
66 | 0 | PromptStdioConnector ssc = (PromptStdioConnector)connector; |
67 | 0 | if (StringUtils.isNotBlank(ssc.getOutputMessage())) |
68 | |
{ |
69 | 0 | out.write(ssc.getOutputMessage().getBytes()); |
70 | |
} |
71 | |
} |
72 | |
|
73 | 0 | Object data = event.getMessage().getPayload(); |
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 | |
@Override |
87 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
88 | |
{ |
89 | 0 | doDispatch(event); |
90 | 0 | return event.getMessage(); |
91 | |
} |
92 | |
|
93 | |
@Override |
94 | |
protected void doDispose() |
95 | |
{ |
96 | |
|
97 | 0 | } |
98 | |
|
99 | |
@Override |
100 | |
protected void doConnect() throws Exception |
101 | |
{ |
102 | |
|
103 | 0 | } |
104 | |
|
105 | |
@Override |
106 | |
protected void doDisconnect() throws Exception |
107 | |
{ |
108 | |
|
109 | 0 | } |
110 | |
} |