1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.stdio;
12
13 import org.mule.api.MuleException;
14 import org.mule.api.MuleMessage;
15 import org.mule.api.endpoint.ImmutableEndpoint;
16 import org.mule.api.endpoint.InboundEndpoint;
17 import org.mule.api.lifecycle.InitialisationException;
18 import org.mule.api.service.Service;
19 import org.mule.api.transport.Connector;
20 import org.mule.api.transport.MessageReceiver;
21 import org.mule.config.i18n.MessageFactory;
22 import org.mule.util.StringUtils;
23
24 import java.io.InputStream;
25 import java.io.OutputStream;
26
27
28
29
30
31 public class PromptStdioConnector extends StdioConnector
32 {
33 private String promptMessage;
34 private String promptMessageCode = null;
35 private String resourceBundle = null;
36 private String outputMessage;
37 private String outputMessageCode = null;
38 private long messageDelayTime = 3000;
39 private boolean firstTime = true;
40
41 public PromptStdioConnector()
42 {
43 super();
44
45 inputStream = System.in;
46 outputStream = System.out;
47 }
48
49
50 protected void doInitialise() throws InitialisationException
51 {
52
53 }
54
55 protected void doDispose()
56 {
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 }
74
75 protected void doConnect() throws Exception
76 {
77
78 }
79
80 protected void doDisconnect() throws Exception
81 {
82
83 }
84
85 public InputStream getInputStream()
86 {
87 return inputStream;
88 }
89
90 public void doStart()
91 {
92 firstTime = false;
93 }
94
95 public OutputStream getOutputStream()
96 {
97 return outputStream;
98 }
99
100
101
102
103 public String getPromptMessage()
104 {
105 if (StringUtils.isNotBlank(resourceBundle) && StringUtils.isNotBlank(promptMessageCode))
106 {
107 return StdioMessageFactory.getString(resourceBundle, promptMessageCode);
108 }
109
110 return promptMessage;
111 }
112
113
114
115
116 public void setPromptMessage(String promptMessage)
117 {
118 this.promptMessage = promptMessage;
119 }
120
121
122
123
124 public String getPromptMessageCode()
125 {
126 return promptMessageCode;
127 }
128
129
130
131
132 public void setPromptMessageCode(String promptMessageCode)
133 {
134 this.promptMessageCode = promptMessageCode;
135 }
136
137
138
139
140 public String getResourceBundle()
141 {
142 return resourceBundle;
143 }
144
145
146
147
148
149 public void setResourceBundle(String resourceBundle)
150 {
151 this.resourceBundle = resourceBundle;
152 }
153
154
155
156
157 public String getOutputMessage()
158 {
159 if (StringUtils.isNotBlank(resourceBundle) && StringUtils.isNotBlank(outputMessageCode))
160 {
161 return StdioMessageFactory.getString(resourceBundle, outputMessageCode);
162 }
163
164 return outputMessage;
165 }
166
167
168
169
170 public void setOutputMessage(String outputMessage)
171 {
172 this.outputMessage = outputMessage;
173 }
174
175
176
177
178 public String getOutputMessageCode()
179 {
180 return outputMessageCode;
181 }
182
183
184
185
186 public void setOutputMessageCode(String outputMessageCode)
187 {
188 this.outputMessageCode = outputMessageCode;
189 }
190
191 public Connector getConnector()
192 {
193 return this;
194 }
195
196 public MessageReceiver registerListener(Service service, InboundEndpoint endpoint) throws Exception
197 {
198 if (receivers.size() > 0)
199 {
200 throw new UnsupportedOperationException(
201 "You can only register one listener per system stream connector");
202 }
203 MessageReceiver receiver = super.registerListener(service, endpoint);
204 return receiver;
205 }
206
207 public long getMessageDelayTime()
208 {
209 if (firstTime)
210 {
211 return messageDelayTime + 4000;
212 }
213 else
214 {
215 return messageDelayTime;
216 }
217 }
218
219 public void setMessageDelayTime(long messageDelayTime)
220 {
221 this.messageDelayTime = messageDelayTime;
222 }
223
224
225 public OutputStream getOutputStream(ImmutableEndpoint endpoint, MuleMessage message) throws MuleException
226 {
227 OutputStream out;
228 String streamName = endpoint.getEndpointURI().getAddress();
229
230 if (STREAM_SYSTEM_OUT.equalsIgnoreCase(streamName))
231 {
232 out = System.out;
233 }
234 else if (STREAM_SYSTEM_ERR.equalsIgnoreCase(streamName))
235 {
236 out = System.err;
237 }
238 else
239 {
240 out = getOutputStream();
241 }
242 return out;
243 }
244
245
246
247
248
249
250
251 private static class StdioMessageFactory extends MessageFactory
252 {
253 protected static String getString(String bundlePath, String code)
254 {
255 return MessageFactory.getString(bundlePath, Integer.parseInt(code));
256 }
257 }
258 }