View Javadoc

1   /*
2    * $Id: SedaStageLifecycleManager.java 22391 2011-07-12 12:00:48Z dirk.olmes $
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.processor;
11  
12  import org.mule.api.MuleException;
13  import org.mule.api.lifecycle.Disposable;
14  import org.mule.api.lifecycle.Initialisable;
15  import org.mule.api.lifecycle.InitialisationException;
16  import org.mule.api.lifecycle.LifecycleCallback;
17  import org.mule.api.lifecycle.LifecycleException;
18  import org.mule.api.lifecycle.Startable;
19  import org.mule.api.lifecycle.Stoppable;
20  import org.mule.config.i18n.CoreMessages;
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 SedaStageLifecycleManager extends SimpleLifecycleManager<SedaStageInterceptingMessageProcessor>
33  {
34      /**
35       * logger used by this class
36       */
37      protected transient final Log logger = LogFactory.getLog(SedaStageLifecycleManager.class);
38  
39      public SedaStageLifecycleManager(String name, SedaStageInterceptingMessageProcessor sedaStage)
40      {
41          super(name, sedaStage);
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 destinationPhase)
58      {
59          if (destinationPhase.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<SedaStageInterceptingMessageProcessor> callback) throws InitialisationException
70      {
71          checkPhase(Initialisable.PHASE_NAME);
72          if(logger.isInfoEnabled()) logger.info("Initialising service: " + lifecycleManagerId);
73          try
74          {
75              invokePhase(Initialisable.PHASE_NAME, getLifecycleObject(), callback);
76          }
77          catch (InitialisationException e)
78          {
79              throw e;
80          }
81          catch (LifecycleException e)
82          {
83              throw new InitialisationException(e, object);
84          }
85      }
86  
87      @Override
88      public void fireStartPhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws MuleException
89      {
90          checkPhase(Startable.PHASE_NAME);
91          if(logger.isInfoEnabled()) logger.info("Starting service: " + lifecycleManagerId);
92          invokePhase(Startable.PHASE_NAME, getLifecycleObject(), callback);
93      }
94  
95      public void firePausePhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws MuleException
96      {
97          checkPhase(Pausable.PHASE_NAME);
98          if(logger.isInfoEnabled()) logger.info("Pausing service: " + lifecycleManagerId);
99          invokePhase(Pausable.PHASE_NAME, getLifecycleObject(), callback);
100     }
101 
102     public void fireResumePhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws MuleException
103     {
104         checkPhase(Resumable.PHASE_NAME);
105         if(logger.isInfoEnabled()) logger.info("Resuming service: " + lifecycleManagerId);
106         invokePhase(Resumable.PHASE_NAME, getLifecycleObject(), callback);
107     }
108 
109     @Override
110     public void fireStopPhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback) throws MuleException
111     {
112         checkPhase(Stoppable.PHASE_NAME);
113         if(logger.isInfoEnabled()) logger.info("Stopping service: " + lifecycleManagerId);
114         invokePhase(Stoppable.PHASE_NAME, getLifecycleObject(), callback);
115     }
116 
117     @Override
118     public void fireDisposePhase(LifecycleCallback<SedaStageInterceptingMessageProcessor> callback)
119     {
120         checkPhase(Disposable.PHASE_NAME);
121         if(logger.isInfoEnabled()) logger.info("Disposing service: " + lifecycleManagerId);
122         try
123         {
124             invokePhase(Disposable.PHASE_NAME, getLifecycleObject(), callback);
125         }
126         catch (LifecycleException e)
127         {
128             logger.warn(CoreMessages.failedToDispose(lifecycleManagerId), e);
129         }
130     }
131 
132 }