View Javadoc

1   /*
2    * $Id$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tck;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.api.source.MessageSource;
17  import org.mule.util.ObjectUtils;
18  
19  public class SensingNullMessageProcessor implements MessageProcessor
20  {
21      public MuleEvent event;
22      protected InternalMessageSource source = new InternalMessageSource();
23      private long waitTime = 0;
24  
25      public MuleEvent process(MuleEvent event) throws MuleException
26      {
27          if (waitTime > 0)
28          {
29              try
30              {
31                  Thread.sleep(waitTime);
32              }
33              catch (InterruptedException e)
34              {
35                  throw new RuntimeException(e);
36              }
37          }
38          this.event = event;
39          if (source.listener != null)
40          {
41              return source.listener.process(event);
42          }
43          else
44          {
45              if (event.getEndpoint().getExchangePattern().hasResponse())
46              {
47                  return event;
48              }
49              else
50              {
51                  return null;
52              }
53          }
54      }
55  
56      public void clear()
57      {
58          event = null;
59      }
60  
61      public MessageSource getMessageSource()
62      {
63          return source;
64      }
65  
66      public void setWaitTime(long waitTime)
67      {
68          this.waitTime = waitTime;
69      }
70  
71      class InternalMessageSource implements MessageSource
72      {
73          MessageProcessor listener;
74  
75          public void setListener(MessageProcessor listener)
76          {
77              this.listener = listener;
78  
79          }
80  
81          @Override
82          public String toString()
83          {
84              return ObjectUtils.toString(this);
85          }
86      }
87  }