View Javadoc

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