View Javadoc

1   /*
2    * $Id: ProcessIfStartedWaitIfPausedMessageProcessor.java 20320 2010-11-24 15:03:31Z dfeist $
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.lifecycle.processor;
12  
13  import org.mule.api.MessagingException;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.lifecycle.LifecycleState;
17  import org.mule.api.lifecycle.Startable;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.service.Pausable;
20  
21  public class ProcessIfStartedWaitIfPausedMessageProcessor extends ProcessIfStartedMessageProcessor
22  {
23  
24      public ProcessIfStartedWaitIfPausedMessageProcessor(Startable startable, LifecycleState lifecycleState)
25      {
26          super(startable, lifecycleState);
27      }
28  
29      @Override
30      protected MuleEvent processNext(MuleEvent event) throws MuleException
31      {
32          if (isPaused())
33          {
34              try
35              {
36                  if (logger.isDebugEnabled())
37                  {
38                      logger.debug(startable.getClass().getName() + " " + getStartableName(startable)
39                                   + " is paused. Blocking call until resumd");
40                  }
41                  while (isPaused())
42                  {
43                      Thread.sleep(500);
44                  }
45              }
46              catch (InterruptedException e)
47              {
48                  throw new MessagingException(
49                      CoreMessages.interruptedWaitingForPaused(getStartableName(startable)),
50                      event, e);
51              }
52          }
53          return super.processNext(event);
54      }
55  
56      @Override
57      protected boolean accept(MuleEvent event)
58      {
59          return lifecycleState.isStarted() || isPaused();
60      }
61  
62      protected boolean isPaused()
63      {
64          return lifecycleState.isPhaseComplete(Pausable.PHASE_NAME);
65      }
66  
67  }