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.construct;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.construct.FlowConstruct;
12  import org.mule.api.lifecycle.Disposable;
13  import org.mule.api.lifecycle.Initialisable;
14  import org.mule.api.lifecycle.LifecycleCallback;
15  import org.mule.api.lifecycle.Startable;
16  import org.mule.api.lifecycle.Stoppable;
17  import org.mule.context.notification.FlowConstructNotification;
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 FlowConstructLifecycleManager extends SimpleLifecycleManager<FlowConstruct>
30  {
31  
32      /**
33       * logger used by this class
34       */
35      protected transient final Log logger = LogFactory.getLog(FlowConstructLifecycleManager.class);
36      protected MuleContext muleContext;
37  
38  
39      public FlowConstructLifecycleManager(FlowConstruct flowConstruct, MuleContext muleContext)
40      {
41          super(flowConstruct.getName(), flowConstruct);
42          this.muleContext = muleContext;
43      }
44  
45      @Override
46      protected void registerTransitions()
47      {
48          super.registerTransitions();
49  
50          //pause resume
51          addDirectTransition(Startable.PHASE_NAME, Pausable.PHASE_NAME);
52          //Note that 'Resume' state gets removed and the current state is set to 'start'. See {@link #notifyTransition}
53          addDirectTransition(Pausable.PHASE_NAME, Resumable.PHASE_NAME);
54          addDirectTransition(Pausable.PHASE_NAME, Stoppable.PHASE_NAME);
55      }
56  
57      @Override
58      protected void notifyTransition(String currentPhase)
59      {
60          if (currentPhase.equals(Resumable.PHASE_NAME))
61          {
62              //Revert back to start phase
63              completedPhases.remove(Resumable.PHASE_NAME);
64              completedPhases.remove(Pausable.PHASE_NAME);
65              setCurrentPhase(Startable.PHASE_NAME);
66          }
67      }
68  
69      @Override
70      public void fireInitialisePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
71      {
72          checkPhase(Initialisable.PHASE_NAME);
73          //TODO No pre notification
74          if (logger.isInfoEnabled())
75          {
76              logger.info("Initialising flow: " + getLifecycleObject().getName());
77          }
78          invokePhase(Initialisable.PHASE_NAME, getLifecycleObject(), callback);
79          fireNotification(FlowConstructNotification.FLOW_CONSTRUCT_INITIALISED);
80      }
81  
82  
83      @Override
84      public void fireStartPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
85      {
86          checkPhase(Startable.PHASE_NAME);
87          if (logger.isInfoEnabled())
88          {
89              logger.info("Starting flow: " + getLifecycleObject().getName());
90          }
91          //TODO No pre notification
92          invokePhase(Startable.PHASE_NAME, getLifecycleObject(), callback);
93          fireNotification(FlowConstructNotification.FLOW_CONSTRUCT_STARTED);
94      }
95  
96  
97      public void firePausePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
98      {
99          checkPhase(Pausable.PHASE_NAME);
100         if (logger.isInfoEnabled())
101         {
102             logger.info("Pausing flow: " + getLifecycleObject().getName());
103         }
104 
105         //TODO No pre notification
106         invokePhase(Pausable.PHASE_NAME, getLifecycleObject(), callback);
107         fireNotification(FlowConstructNotification.FLOW_CONSTRUCT_PAUSED);
108     }
109 
110     public void fireResumePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
111     {
112         checkPhase(Resumable.PHASE_NAME);
113         if (logger.isInfoEnabled())
114         {
115             logger.info("Resuming flow: " + getLifecycleObject().getName());
116         }
117         //TODO No pre notification
118         invokePhase(Resumable.PHASE_NAME, getLifecycleObject(), callback);
119         fireNotification(FlowConstructNotification.FLOW_CONSTRUCT_RESUMED);
120     }
121 
122     @Override
123     public void fireStopPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
124     {
125         checkPhase(Stoppable.PHASE_NAME);
126         if (logger.isInfoEnabled())
127         {
128             logger.info("Stopping flow: " + getLifecycleObject().getName());
129         }
130         //TODO No pre notification
131         invokePhase(Stoppable.PHASE_NAME, getLifecycleObject(), callback);
132         fireNotification(FlowConstructNotification.FLOW_CONSTRUCT_STOPPED);
133     }
134 
135     @Override
136     public void fireDisposePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
137     {
138         checkPhase(Disposable.PHASE_NAME);
139         if (logger.isInfoEnabled())
140         {
141             logger.info("Disposing flow: " + getLifecycleObject().getName());
142         }
143         //TODO No pre notification
144         invokePhase(Disposable.PHASE_NAME, getLifecycleObject(), callback);
145         fireNotification(FlowConstructNotification.FLOW_CONSTRUCT_DISPOSED);
146     }
147 
148     protected void fireNotification(int action)
149     {
150         muleContext.fireNotification(new FlowConstructNotification(getLifecycleObject(), action));
151     }
152 }