1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.stdio; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleMessage; |
11 | |
import org.mule.api.endpoint.OutboundEndpoint; |
12 | |
import org.mule.api.transport.DispatchException; |
13 | |
import org.mule.transport.AbstractMessageDispatcher; |
14 | |
import org.mule.transport.stdio.i18n.StdioMessages; |
15 | |
import org.mule.util.StringUtils; |
16 | |
|
17 | |
import java.io.OutputStream; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class StdioMessageDispatcher extends AbstractMessageDispatcher |
28 | |
{ |
29 | |
private final StdioConnector connector; |
30 | |
|
31 | |
public StdioMessageDispatcher(OutboundEndpoint endpoint) |
32 | |
{ |
33 | 0 | super(endpoint); |
34 | 0 | this.connector = (StdioConnector) endpoint.getConnector(); |
35 | |
|
36 | |
|
37 | 0 | if (connector instanceof PromptStdioConnector) |
38 | |
{ |
39 | 0 | PromptStdioConnector ssc = (PromptStdioConnector)connector; |
40 | |
|
41 | 0 | String outputMessage = (String) endpoint.getProperties().get("outputMessage"); |
42 | 0 | if (outputMessage != null) |
43 | |
{ |
44 | 0 | ssc.setOutputMessage(outputMessage); |
45 | |
} |
46 | |
} |
47 | 0 | } |
48 | |
|
49 | |
@Override |
50 | |
protected synchronized void doDispatch(MuleEvent event) throws Exception |
51 | |
{ |
52 | 0 | OutputStream out = connector.getOutputStream(); |
53 | |
|
54 | 0 | if (out == null) |
55 | |
{ |
56 | 0 | throw new DispatchException(StdioMessages.couldNotFindStreamWithName(event.getEndpoint()), |
57 | |
event, (OutboundEndpoint) endpoint); |
58 | |
} |
59 | |
|
60 | 0 | if (connector instanceof PromptStdioConnector) |
61 | |
{ |
62 | 0 | PromptStdioConnector ssc = (PromptStdioConnector)connector; |
63 | 0 | if (StringUtils.isNotBlank(ssc.getOutputMessage())) |
64 | |
{ |
65 | 0 | out.write(ssc.getOutputMessage().getBytes()); |
66 | |
} |
67 | |
} |
68 | |
|
69 | 0 | Object data = event.getMessage().getPayload(); |
70 | 0 | if (data instanceof byte[]) |
71 | |
{ |
72 | 0 | out.write((byte[])data); |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 0 | out.write(data.toString().getBytes()); |
77 | |
} |
78 | |
|
79 | 0 | out.flush(); |
80 | 0 | } |
81 | |
|
82 | |
@Override |
83 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
84 | |
{ |
85 | 0 | doDispatch(event); |
86 | 0 | return event.getMessage(); |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
protected void doDispose() |
91 | |
{ |
92 | |
|
93 | 0 | } |
94 | |
|
95 | |
@Override |
96 | |
protected void doConnect() throws Exception |
97 | |
{ |
98 | |
|
99 | 0 | } |
100 | |
|
101 | |
@Override |
102 | |
protected void doDisconnect() throws Exception |
103 | |
{ |
104 | |
|
105 | 0 | } |
106 | |
} |