View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.lifecycle.processor;
8   
9   import org.mule.api.MessagingException;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.lifecycle.LifecycleState;
13  import org.mule.api.lifecycle.Startable;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.service.Pausable;
16  
17  public class ProcessIfStartedWaitIfPausedMessageProcessor extends ProcessIfStartedMessageProcessor
18  {
19  
20      public ProcessIfStartedWaitIfPausedMessageProcessor(Startable startable, LifecycleState lifecycleState)
21      {
22          super(startable, lifecycleState);
23      }
24  
25      @Override
26      public MuleEvent process(MuleEvent event) throws MuleException
27      {
28          if (accept(event))
29          {
30              if (isPaused())
31              {
32                  try
33                  {
34                      if (logger.isDebugEnabled())
35                      {
36                          logger.debug(startable.getClass().getName() + " " + getStartableName(startable)
37                                       + " is paused. Blocking call until resumd");
38                      }
39                      while (isPaused())
40                      {
41                          Thread.sleep(500);
42                      }
43                  }
44                  catch (InterruptedException e)
45                  {
46                      throw new MessagingException(
47                          CoreMessages.interruptedWaitingForPaused(getStartableName(startable)), event, e);
48                  }
49              }
50              return processNext(event);
51          }
52          else
53          {
54              return handleUnaccepted(event);
55          }
56      }
57  
58      @Override
59      protected boolean accept(MuleEvent event)
60      {
61          return lifecycleState.isStarted() || isPaused();
62      }
63  
64      protected boolean isPaused()
65      {
66          return lifecycleState.isPhaseComplete(Pausable.PHASE_NAME);
67      }
68  
69  }