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.service;
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.api.service.Service;
18  import org.mule.context.notification.FlowConstructNotification;
19  import org.mule.context.notification.ServiceNotification;
20  import org.mule.lifecycle.SimpleLifecycleManager;
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 ServiceLifecycleManager extends SimpleLifecycleManager<FlowConstruct>
30  {
31      /**
32       * logger used by this class
33       */
34      protected transient final Log logger = LogFactory.getLog(ServiceLifecycleManager.class);
35      protected MuleContext muleContext;
36  
37      public ServiceLifecycleManager(FlowConstruct service, MuleContext muleContext) throws MuleException
38      {
39          super(service.getName(), service);
40          this.muleContext = muleContext;
41      }
42  
43      @Override
44      protected void registerTransitions()
45      {
46          super.registerTransitions();
47  
48          //pause resume
49          addDirectTransition(Startable.PHASE_NAME, Pausable.PHASE_NAME);
50          //Note that 'Resume' state gets removed and the current state is set to 'start'. See {@link #notifyTransition}
51          addDirectTransition(Pausable.PHASE_NAME, Resumable.PHASE_NAME);
52          addDirectTransition(Pausable.PHASE_NAME, Stoppable.PHASE_NAME);
53      }
54  
55      @Override
56      protected void notifyTransition(String destinationPhase)
57      {
58          if (destinationPhase.equals(Resumable.PHASE_NAME))
59          {
60              //Revert back to start phase
61              completedPhases.remove(Resumable.PHASE_NAME);
62              completedPhases.remove(Pausable.PHASE_NAME);
63              setCurrentPhase(Startable.PHASE_NAME);
64          }
65      }
66  
67      @Override
68      public void fireInitialisePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
69      {
70          checkPhase(Initialisable.PHASE_NAME);
71          //TODO No pre notification
72          if(logger.isInfoEnabled()) logger.info("Initialising service: " + getLifecycleObject().getName());
73          invokePhase(Initialisable.PHASE_NAME, getLifecycleObject(), callback);
74          fireNotification(ServiceNotification.SERVICE_INITIALISED);
75      }
76  
77      @Override
78      public void fireStartPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
79      {
80          checkPhase(Startable.PHASE_NAME);
81          if(logger.isInfoEnabled()) logger.info("Starting service: " + getLifecycleObject().getName());
82          //TODO No pre notification
83          invokePhase(Startable.PHASE_NAME, getLifecycleObject(), callback);
84          fireNotification(ServiceNotification.SERVICE_STARTED);
85      }
86  
87      public void firePausePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
88      {
89          checkPhase(Pausable.PHASE_NAME);
90          if(logger.isInfoEnabled()) logger.info("Pausing service: " + getLifecycleObject().getName());
91  
92          //TODO No pre notification
93          invokePhase(Pausable.PHASE_NAME, getLifecycleObject(), callback);
94          fireNotification(ServiceNotification.SERVICE_PAUSED);
95      }
96  
97      public void fireResumePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
98      {
99          checkPhase(Resumable.PHASE_NAME);
100         if(logger.isInfoEnabled()) logger.info("Resuming service: " + getLifecycleObject().getName());
101         //TODO No pre notification
102         invokePhase(Resumable.PHASE_NAME, getLifecycleObject(), callback);
103         fireNotification(ServiceNotification.SERVICE_RESUMED);
104     }
105 
106     @Override
107     public void fireStopPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
108     {
109         checkPhase(Stoppable.PHASE_NAME);
110         if(logger.isInfoEnabled()) logger.info("Stopping service: " + getLifecycleObject().getName());
111         //TODO No pre notification
112         invokePhase(Stoppable.PHASE_NAME, getLifecycleObject(), callback);
113         fireNotification(ServiceNotification.SERVICE_STOPPED);
114     }
115 
116     @Override
117     public void fireDisposePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
118     {
119         checkPhase(Disposable.PHASE_NAME);
120         if(logger.isInfoEnabled()) logger.info("Disposing service: " + getLifecycleObject().getName());
121         //TODO No pre notification
122         invokePhase(Disposable.PHASE_NAME, getLifecycleObject(), callback);
123         fireNotification(ServiceNotification.SERVICE_DISPOSED);
124     }
125 
126     protected void fireNotification(int action)
127     {
128         // double broadcast for backwards compatibility
129         muleContext.fireNotification(new FlowConstructNotification(getLifecycleObject(), action));
130         if(getLifecycleObject() instanceof Service)
131         {
132             muleContext.fireNotification(new ServiceNotification((Service)getLifecycleObject(), action));
133         }
134     }
135 }