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