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