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 super(endpoint);
38 this.connector = (StdioConnector)endpoint.getConnector();
39
40
41 if (connector instanceof PromptStdioConnector)
42 {
43 PromptStdioConnector ssc = (PromptStdioConnector)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(MuleEvent event) throws Exception
54 {
55 OutputStream out = connector.getOutputStream();
56
57 if (out == null)
58 {
59 throw new DispatchException(
60 StdioMessages.couldNotFindStreamWithName(event.getEndpoint().getEndpointURI().getAddress()),
61 event.getMessage(), event.getEndpoint());
62 }
63
64
65 if (connector instanceof PromptStdioConnector)
66 {
67 PromptStdioConnector ssc = (PromptStdioConnector)connector;
68 if (StringUtils.isNotBlank(ssc.getOutputMessage()))
69 {
70 out.write(ssc.getOutputMessage().getBytes());
71 }
72 }
73
74 Object data = event.transformMessage();
75 if (data instanceof byte[])
76 {
77 out.write((byte[])data);
78 }
79 else
80 {
81 out.write(data.toString().getBytes());
82 }
83
84 out.flush();
85 }
86
87
88
89
90
91
92 protected MuleMessage doSend(MuleEvent event) throws Exception
93 {
94 doDispatch(event);
95 return event.getMessage();
96 }
97
98 protected void doDispose()
99 {
100
101 }
102
103 protected void doConnect() throws Exception
104 {
105
106 }
107
108 protected void doDisconnect() throws Exception
109 {
110
111 }
112
113
114
115 }