1
2
3
4
5
6
7
8
9
10
11 package org.mule.tck.functional;
12
13 import org.mule.MuleServer;
14 import org.mule.RequestContext;
15 import org.mule.api.MuleContext;
16 import org.mule.api.MuleEventContext;
17 import org.mule.api.MuleException;
18 import org.mule.api.MuleMessage;
19 import org.mule.api.lifecycle.Callable;
20 import org.mule.api.lifecycle.Disposable;
21 import org.mule.api.lifecycle.Initialisable;
22 import org.mule.tck.exceptions.FunctionalTestException;
23 import org.mule.util.NumberUtils;
24 import org.mule.util.StringMessageUtils;
25 import org.mule.util.expression.ExpressionEvaluatorManager;
26
27 import java.util.List;
28
29 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class FunctionalTestComponent implements Callable, Initialisable, Disposable
49 {
50 protected transient Log logger = LogFactory.getLog(getClass());
51
52 public static final int STREAM_SAMPLE_SIZE = 4;
53 public static final int STREAM_BUFFER_SIZE = 4096;
54 private EventCallback eventCallback;
55 private Object returnData = null;
56 private boolean throwException = false;
57 private boolean enableMessageHistory = true;
58 private boolean enableNotifications = true;
59 private boolean doInboundTransform = true;
60 private String appendString;
61 private Class exceptionToThrow;
62 private long waitTime = 0;
63
64
65
66
67
68
69 private List messageHistory;
70
71 public void initialise()
72 {
73 if (enableMessageHistory)
74 {
75 messageHistory = new CopyOnWriteArrayList();
76 }
77 }
78
79 public void dispose()
80 {
81
82 }
83
84
85
86
87 public Object onCall(MuleEventContext context) throws Exception
88 {
89 if (isThrowException())
90 {
91 throwException();
92 }
93 return process(getMessageFromContext(context), context);
94 }
95
96 private Object getMessageFromContext(MuleEventContext context) throws MuleException
97 {
98 if(isDoInboundTransform())
99 {
100 Object o = context.transformMessage();
101 if(getAppendString()!=null && !(o instanceof String))
102 {
103 o = context.transformMessageToString();
104 }
105 return o;
106 }
107 else if(getAppendString()!=null)
108 {
109 return context.getMessageAsString();
110 }
111 else
112 {
113 return context.getMessage().getPayload();
114 }
115 }
116
117
118
119
120
121
122
123
124
125 public Object onReceive(Object data) throws Exception
126 {
127 MuleEventContext context = RequestContext.getEventContext();
128
129 if (isThrowException())
130 {
131 throwException();
132 }
133 return process(data, context);
134 }
135
136
137
138
139
140
141
142
143 protected void throwException() throws Exception
144 {
145 if (getExceptionToThrow() != null)
146 {
147 throw (Exception)getExceptionToThrow().newInstance();
148 }
149 else
150 {
151 throw new FunctionalTestException();
152 }
153 }
154
155
156
157
158
159
160
161
162
163
164 protected String append(String contents, MuleMessage message)
165 {
166 return contents + ExpressionEvaluatorManager.parse(appendString, message);
167 }
168
169
170
171
172
173
174
175
176
177
178 protected Object process(Object data, MuleEventContext context) throws Exception
179 {
180 if (enableMessageHistory)
181 {
182 messageHistory.add(data);
183 }
184
185 String msg = StringMessageUtils.getBoilerPlate("Message Received in service: "
186 + context.getService().getName() + ". Content is: "
187 + StringMessageUtils.truncate(data.toString(), 100, true), '*', 80);
188
189 logger.info(msg);
190
191 if (eventCallback != null)
192 {
193 eventCallback.eventReceived(context, this);
194 }
195
196 Object replyMessage;
197 if (returnData != null)
198 {
199 if (returnData instanceof String && ExpressionEvaluatorManager.isValidExpression(returnData.toString()))
200 {
201 replyMessage = ExpressionEvaluatorManager.parse(returnData.toString(), context.getMessage());
202 }
203 else
204 {
205 replyMessage = returnData;
206 }
207 }
208 else
209 {
210 if (appendString != null)
211 {
212 replyMessage = append(data.toString(), context.getMessage());
213 }
214 else
215 {
216 replyMessage = data;
217 }
218 }
219
220 if (isEnableNotifications())
221 {
222 MuleContext muleContext = context.getMuleContext();
223 if (muleContext == null)
224 {
225 logger.warn("No MuleContext available from MuleEventContext");
226 muleContext = MuleServer.getMuleContext();
227 }
228 muleContext.fireNotification(
229 new FunctionalTestNotification(context, replyMessage, FunctionalTestNotification.EVENT_RECEIVED));
230 }
231
232
233 if(waitTime > 0)
234 {
235 try
236 {
237 Thread.sleep(waitTime);
238 }
239 catch (InterruptedException e)
240 {
241 logger.info("FunctionalTestComponent waitTime was interrupted");
242 }
243 }
244 return replyMessage;
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 public EventCallback getEventCallback()
263 {
264 return eventCallback;
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 public void setEventCallback(EventCallback eventCallback)
283 {
284 this.eventCallback = eventCallback;
285 }
286
287
288
289
290
291
292
293
294 public Object getReturnData()
295 {
296 return returnData;
297 }
298
299
300
301
302
303
304
305
306 public void setReturnData(Object returnData)
307 {
308 this.returnData = returnData;
309 }
310
311
312
313
314
315
316
317
318
319 public boolean isThrowException()
320 {
321 return throwException;
322 }
323
324
325
326
327
328
329
330
331
332 public void setThrowException(boolean throwException)
333 {
334 this.throwException = throwException;
335 }
336
337 public boolean isEnableMessageHistory()
338 {
339 return enableMessageHistory;
340 }
341
342 public void setEnableMessageHistory(boolean enableMessageHistory)
343 {
344 this.enableMessageHistory = enableMessageHistory;
345 }
346
347
348
349
350 public int getReceivedMessages()
351 {
352 if (messageHistory != null)
353 {
354 return messageHistory.size();
355 }
356 else
357 {
358 return NumberUtils.INTEGER_MINUS_ONE.intValue();
359 }
360 }
361
362
363
364
365
366
367 public Object getReceivedMessage(int number)
368 {
369 Object message = null;
370 if (messageHistory != null)
371 {
372 if (number <= messageHistory.size())
373 {
374 message = messageHistory.get(number - 1);
375 }
376 }
377 return message;
378 }
379
380
381
382
383 public Object getLastReceivedMessage()
384 {
385 if (messageHistory != null)
386 {
387 return messageHistory.get(messageHistory.size() - 1);
388 }
389 else
390 {
391 return null;
392 }
393 }
394
395 public String getAppendString()
396 {
397 return appendString;
398 }
399
400 public void setAppendString(String appendString)
401 {
402 this.appendString = appendString;
403 }
404
405 public boolean isEnableNotifications()
406 {
407 return enableNotifications;
408 }
409
410 public void setEnableNotifications(boolean enableNotifications)
411 {
412 this.enableNotifications = enableNotifications;
413 }
414
415 public Class getExceptionToThrow()
416 {
417 return exceptionToThrow;
418 }
419
420 public void setExceptionToThrow(Class exceptionToThrow)
421 {
422 this.exceptionToThrow = exceptionToThrow;
423 }
424
425 public long getWaitTime()
426 {
427 return waitTime;
428 }
429
430 public void setWaitTime(long waitTime)
431 {
432 this.waitTime = waitTime;
433 }
434
435 public boolean isDoInboundTransform()
436 {
437 return doInboundTransform;
438 }
439
440 public void setDoInboundTransform(boolean doInboundTransform)
441 {
442 this.doInboundTransform = doInboundTransform;
443 }
444 }