View Javadoc

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