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