1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.stdio; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.construct.FlowConstruct; |
15 | |
import org.mule.api.endpoint.InboundEndpoint; |
16 | |
import org.mule.api.lifecycle.CreateException; |
17 | |
import org.mule.api.transport.Connector; |
18 | |
import org.mule.transport.AbstractPollingMessageReceiver; |
19 | |
|
20 | |
import java.io.InputStream; |
21 | |
import java.io.PrintStream; |
22 | |
import java.io.PushbackInputStream; |
23 | |
|
24 | |
import org.apache.commons.lang.BooleanUtils; |
25 | |
import org.apache.commons.lang.SystemUtils; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class StdioMessageReceiver extends AbstractPollingMessageReceiver |
32 | |
{ |
33 | |
public static final int DEFAULT_BUFFER_SIZE = 4096; |
34 | |
|
35 | 0 | private int bufferSize = DEFAULT_BUFFER_SIZE; |
36 | |
private InputStream inputStream; |
37 | |
private StdioConnector connector; |
38 | |
|
39 | |
private boolean sendStream; |
40 | |
|
41 | |
public StdioMessageReceiver(Connector connector, |
42 | |
FlowConstruct flowConstruct, |
43 | |
InboundEndpoint endpoint, |
44 | |
long checkFrequency) throws CreateException |
45 | |
{ |
46 | 0 | super(connector, flowConstruct, endpoint); |
47 | 0 | this.setFrequency(checkFrequency); |
48 | |
|
49 | 0 | this.connector = (StdioConnector) connector; |
50 | 0 | String streamName = endpoint.getEndpointURI().getAddress(); |
51 | 0 | if (StdioConnector.STREAM_SYSTEM_IN.equalsIgnoreCase(streamName)) |
52 | |
{ |
53 | 0 | inputStream = System.in; |
54 | |
} |
55 | |
else |
56 | |
{ |
57 | 0 | inputStream = this.connector.getInputStream(); |
58 | |
} |
59 | |
|
60 | |
|
61 | 0 | if (connector instanceof PromptStdioConnector) |
62 | |
{ |
63 | 0 | PromptStdioConnector ssc = (PromptStdioConnector) connector; |
64 | |
|
65 | 0 | String promptMessage = (String) endpoint.getProperties().get("promptMessage"); |
66 | 0 | if (promptMessage != null) |
67 | |
{ |
68 | 0 | ssc.setPromptMessage(promptMessage); |
69 | |
} |
70 | |
} |
71 | |
|
72 | 0 | this.sendStream = BooleanUtils.toBoolean((String) endpoint.getProperties().get("sendStream")); |
73 | 0 | } |
74 | |
|
75 | |
@Override |
76 | |
protected void doDispose() |
77 | |
{ |
78 | |
|
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public void doConnect() throws Exception |
83 | |
{ |
84 | 0 | if (connector instanceof PromptStdioConnector) |
85 | |
{ |
86 | 0 | PromptStdioConnector ssc = (PromptStdioConnector) connector; |
87 | 0 | DelayedMessageWriter writer = new DelayedMessageWriter(ssc); |
88 | 0 | writer.start(); |
89 | |
} |
90 | 0 | } |
91 | |
|
92 | |
@Override |
93 | |
public void doDisconnect() throws Exception |
94 | |
{ |
95 | |
|
96 | 0 | } |
97 | |
|
98 | |
@Override |
99 | |
public void poll() |
100 | |
{ |
101 | 0 | String encoding = endpoint.getEncoding(); |
102 | |
try |
103 | |
{ |
104 | 0 | if (sendStream) |
105 | |
{ |
106 | 0 | PushbackInputStream in = new PushbackInputStream(inputStream); |
107 | |
|
108 | |
|
109 | 0 | int i = in.read(); |
110 | |
|
111 | 0 | in.unread(i); |
112 | 0 | MuleMessage message = createMuleMessage(in, encoding); |
113 | 0 | routeMessage(message); |
114 | 0 | } |
115 | |
else |
116 | |
{ |
117 | 0 | byte[] inputBuffer = new byte[bufferSize]; |
118 | 0 | int len = inputStream.read(inputBuffer); |
119 | |
|
120 | 0 | if (len == -1) |
121 | |
{ |
122 | 0 | return; |
123 | |
} |
124 | |
|
125 | 0 | StringBuffer fullBuffer = new StringBuffer(bufferSize); |
126 | 0 | while (len > 0) |
127 | |
{ |
128 | 0 | fullBuffer.append(new String(inputBuffer, 0, len)); |
129 | 0 | len = 0; |
130 | 0 | if (inputStream.available() > 0) |
131 | |
{ |
132 | 0 | len = inputStream.read(inputBuffer); |
133 | |
} |
134 | |
} |
135 | |
|
136 | |
|
137 | 0 | String[] lines = fullBuffer.toString().split(SystemUtils.LINE_SEPARATOR); |
138 | 0 | for (int i = 0; i < lines.length; ++i) |
139 | |
{ |
140 | 0 | MuleMessage message = createMuleMessage(lines[i], encoding); |
141 | 0 | routeMessage(message); |
142 | |
} |
143 | |
} |
144 | |
|
145 | 0 | doConnect(); |
146 | |
} |
147 | 0 | catch (Exception e) |
148 | |
{ |
149 | 0 | getConnector().getMuleContext().getExceptionListener().handleException(e); |
150 | 0 | } |
151 | 0 | } |
152 | |
|
153 | |
public InputStream getInputStream() |
154 | |
{ |
155 | 0 | return inputStream; |
156 | |
} |
157 | |
|
158 | |
public void setInputStream(InputStream inputStream) |
159 | |
{ |
160 | 0 | this.inputStream = inputStream; |
161 | 0 | } |
162 | |
|
163 | |
public int getBufferSize() |
164 | |
{ |
165 | 0 | return bufferSize; |
166 | |
} |
167 | |
|
168 | |
public void setBufferSize(int bufferSize) |
169 | |
{ |
170 | 0 | this.bufferSize = bufferSize; |
171 | 0 | } |
172 | |
|
173 | |
private class DelayedMessageWriter extends Thread |
174 | |
{ |
175 | 0 | private long delay = 0; |
176 | |
private PromptStdioConnector ssc; |
177 | |
|
178 | |
public DelayedMessageWriter(PromptStdioConnector ssc) |
179 | 0 | { |
180 | 0 | this.delay = ssc.getMessageDelayTime(); |
181 | 0 | this.ssc = ssc; |
182 | 0 | } |
183 | |
|
184 | |
@Override |
185 | |
public void run() |
186 | |
{ |
187 | 0 | if (delay > 0) |
188 | |
{ |
189 | |
try |
190 | |
{ |
191 | |
|
192 | 0 | sleep(delay); |
193 | |
} |
194 | 0 | catch (InterruptedException e1) |
195 | |
{ |
196 | |
|
197 | 0 | } |
198 | |
} |
199 | 0 | ((PrintStream) ssc.getOutputStream()).println(); |
200 | 0 | ((PrintStream) ssc.getOutputStream()).print(ssc.getPromptMessage()); |
201 | 0 | } |
202 | |
} |
203 | |
} |