View Javadoc

1   /*
2    * $Id: ProcessIfStartedWaitIfPausedMessageProcessor.java 21307 2011-02-17 15:35:44Z 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      public MuleEvent process(MuleEvent event) throws MuleException
31      {
32          if (accept(event))
33          {
34              if (isPaused())
35              {
36                  try
37                  {
38                      if (logger.isDebugEnabled())
39                      {
40                          logger.debug(startable.getClass().getName() + " " + getStartableName(startable)
41                                       + " is paused. Blocking call until resumd");
42                      }
43                      while (isPaused())
44                      {
45                          Thread.sleep(500);
46                      }
47                  }
48                  catch (InterruptedException e)
49                  {
50                      throw new MessagingException(
51                          CoreMessages.interruptedWaitingForPaused(getStartableName(startable)), event, e);
52                  }
53              }
54              return processNext(event);
55          }
56          else
57          {
58              return handleUnaccepted(event);
59          }
60      }
61  
62      @Override
63      protected boolean accept(MuleEvent event)
64      {
65          return lifecycleState.isStarted() || isPaused();
66      }
67  
68      protected boolean isPaused()
69      {
70          return lifecycleState.isPhaseComplete(Pausable.PHASE_NAME);
71      }
72  
73  }