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