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.processor;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.lifecycle.Disposable;
11  import org.mule.api.lifecycle.Initialisable;
12  import org.mule.api.lifecycle.InitialisationException;
13  import org.mule.api.lifecycle.LifecycleCallback;
14  import org.mule.api.lifecycle.LifecycleException;
15  import org.mule.api.lifecycle.Startable;
16  import org.mule.api.lifecycle.Stoppable;
17  import org.mule.config.i18n.CoreMessages;
18  import org.mule.lifecycle.SimpleLifecycleManager;
19  import org.mule.service.Pausable;
20  import org.mule.service.Resumable;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * The lifecycle manager responsible for managing lifecycle transitions for a Mule service.  The Mule service adds some additional
27   * states, namely pause and resume.  The lifecycle manager manages lifecycle notifications and logging as well.
28   */
29  public class SedaStageLifecycleManager extends SimpleLifecycleManager<SedaStageInterceptingMessageProcessor>
30  {
31      /**
32       * logger used by this class
33       */
34      protected transient final Log logger = LogFactory.getLog(SedaStageLifecycleManager.class);
35  
36      public SedaStageLifecycleManager(String name, SedaStageInterceptingMessageProcessor sedaStage)
37      {
38          super(name, sedaStage);
39      }
40  
41      @Override
42      protected void registerTransitions()
43      {
44          super.registerTransitions();
45  
46          //pause resume
47          addDirectTransition(Startable.PHASE_NAME, Pausable.PHASE_NAME);
48          //Note that 'Resume' state gets removed and the current state is set to 'start'. See {@link #notifyTransition}
49          addDirectTransition(Pausable.PHASE_NAME, Resumable.PHASE_NAME);
50          addDirectTransition(Pausable.PHASE_NAME, Stoppable.PHASE_NAME);
51      }
52  
53      @Override
54      protected void notifyTransition(String destinationPhase)
55      {
56          if (destinationPhase.equals(Resumable.PHASE_NAME))
57          {
58              //Revert back to start phase
59              completedPhases.remove(Resumable.PHASE_NAME);
60              completedPhases.remove(Pausable.PHASE_NAME);
61              setCurrentPhase(Startable.PHASE_NAME);
62          }
63      }
64  
65      @Override
66      public void fireInitialisePhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws InitialisationException
67      {
68          checkPhase(Initialisable.PHASE_NAME);
69          if(logger.isInfoEnabled()) logger.info("Initialising service: " + lifecycleManagerId);
70          try
71          {
72              invokePhase(Initialisable.PHASE_NAME, getLifecycleObject(), callback);
73          }
74          catch (InitialisationException e)
75          {
76              throw e;
77          }
78          catch (LifecycleException e)
79          {
80              throw new InitialisationException(e, (Initialisable) object);
81          }
82      }
83  
84      @Override
85      public void fireStartPhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws MuleException
86      {
87          checkPhase(Startable.PHASE_NAME);
88          if(logger.isInfoEnabled()) logger.info("Starting service: " + lifecycleManagerId);
89          invokePhase(Startable.PHASE_NAME, getLifecycleObject(), callback);
90      }
91  
92      public void firePausePhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws MuleException
93      {
94          checkPhase(Pausable.PHASE_NAME);
95          if(logger.isInfoEnabled()) logger.info("Pausing service: " + lifecycleManagerId);
96          invokePhase(Pausable.PHASE_NAME, getLifecycleObject(), callback);
97      }
98  
99      public void fireResumePhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws MuleException
100     {
101         checkPhase(Resumable.PHASE_NAME);
102         if(logger.isInfoEnabled()) logger.info("Resuming service: " + lifecycleManagerId);
103         invokePhase(Resumable.PHASE_NAME, getLifecycleObject(), callback);
104     }
105 
106     @Override
107     public void fireStopPhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws MuleException
108     {
109         checkPhase(Stoppable.PHASE_NAME);
110         if(logger.isInfoEnabled()) logger.info("Stopping service: " + lifecycleManagerId);
111         invokePhase(Stoppable.PHASE_NAME, getLifecycleObject(), callback);
112     }
113 
114     @Override
115     public void fireDisposePhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback)
116     {
117         checkPhase(Disposable.PHASE_NAME);
118         if(logger.isInfoEnabled()) logger.info("Disposing service: " + lifecycleManagerId);
119         try
120         {
121             invokePhase(Disposable.PHASE_NAME, getLifecycleObject(), callback);
122         }
123         catch (LifecycleException e)
124         {
125             logger.warn(CoreMessages.failedToDispose(lifecycleManagerId), e);
126         }
127     }
128 
129 }