Coverage Report - org.mule.service.ServiceLifecycleManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceLifecycleManager
0%
0/48
0%
0/16
0
 
 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  0
     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  0
         super(service.getName(), service);
 40  0
         this.muleContext = muleContext;
 41  0
     }
 42  
 
 43  
     @Override
 44  
     protected void registerTransitions()
 45  
     {
 46  0
         super.registerTransitions();
 47  
 
 48  
         //pause resume
 49  0
         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  0
         addDirectTransition(Pausable.PHASE_NAME, Resumable.PHASE_NAME);
 52  0
         addDirectTransition(Pausable.PHASE_NAME, Stoppable.PHASE_NAME);
 53  0
     }
 54  
 
 55  
     @Override
 56  
     protected void notifyTransition(String destinationPhase)
 57  
     {
 58  0
         if (destinationPhase.equals(Resumable.PHASE_NAME))
 59  
         {
 60  
             //Revert back to start phase
 61  0
             completedPhases.remove(Resumable.PHASE_NAME);
 62  0
             completedPhases.remove(Pausable.PHASE_NAME);
 63  0
             setCurrentPhase(Startable.PHASE_NAME);
 64  
         }
 65  0
     }
 66  
 
 67  
     @Override
 68  
     public void fireInitialisePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
 69  
     {
 70  0
         checkPhase(Initialisable.PHASE_NAME);
 71  
         //TODO No pre notification
 72  0
         if(logger.isInfoEnabled()) logger.info("Initialising service: " + getLifecycleObject().getName());
 73  0
         invokePhase(Initialisable.PHASE_NAME, getLifecycleObject(), callback);
 74  0
         fireNotification(ServiceNotification.SERVICE_INITIALISED);
 75  0
     }
 76  
 
 77  
     @Override
 78  
     public void fireStartPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
 79  
     {
 80  0
         checkPhase(Startable.PHASE_NAME);
 81  0
         if(logger.isInfoEnabled()) logger.info("Starting service: " + getLifecycleObject().getName());
 82  
         //TODO No pre notification
 83  0
         invokePhase(Startable.PHASE_NAME, getLifecycleObject(), callback);
 84  0
         fireNotification(ServiceNotification.SERVICE_STARTED);
 85  0
     }
 86  
 
 87  
     public void firePausePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
 88  
     {
 89  0
         checkPhase(Pausable.PHASE_NAME);
 90  0
         if(logger.isInfoEnabled()) logger.info("Pausing service: " + getLifecycleObject().getName());
 91  
 
 92  
         //TODO No pre notification
 93  0
         invokePhase(Pausable.PHASE_NAME, getLifecycleObject(), callback);
 94  0
         fireNotification(ServiceNotification.SERVICE_PAUSED);
 95  0
     }
 96  
 
 97  
     public void fireResumePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
 98  
     {
 99  0
         checkPhase(Resumable.PHASE_NAME);
 100  0
         if(logger.isInfoEnabled()) logger.info("Resuming service: " + getLifecycleObject().getName());
 101  
         //TODO No pre notification
 102  0
         invokePhase(Resumable.PHASE_NAME, getLifecycleObject(), callback);
 103  0
         fireNotification(ServiceNotification.SERVICE_RESUMED);
 104  0
     }
 105  
 
 106  
     @Override
 107  
     public void fireStopPhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
 108  
     {
 109  0
         checkPhase(Stoppable.PHASE_NAME);
 110  0
         if(logger.isInfoEnabled()) logger.info("Stopping service: " + getLifecycleObject().getName());
 111  
         //TODO No pre notification
 112  0
         invokePhase(Stoppable.PHASE_NAME, getLifecycleObject(), callback);
 113  0
         fireNotification(ServiceNotification.SERVICE_STOPPED);
 114  0
     }
 115  
 
 116  
     @Override
 117  
     public void fireDisposePhase(LifecycleCallback<FlowConstruct> callback) throws MuleException
 118  
     {
 119  0
         checkPhase(Disposable.PHASE_NAME);
 120  0
         if(logger.isInfoEnabled()) logger.info("Disposing service: " + getLifecycleObject().getName());
 121  
         //TODO No pre notification
 122  0
         invokePhase(Disposable.PHASE_NAME, getLifecycleObject(), callback);
 123  0
         fireNotification(ServiceNotification.SERVICE_DISPOSED);
 124  0
     }
 125  
 
 126  
     protected void fireNotification(int action)
 127  
     {
 128  
         // double broadcast for backwards compatibility
 129  0
         muleContext.fireNotification(new FlowConstructNotification(getLifecycleObject(), action));
 130  0
         if(getLifecycleObject() instanceof Service)
 131  
         {
 132  0
             muleContext.fireNotification(new ServiceNotification((Service)getLifecycleObject(), action));
 133  
         }
 134  0
     }
 135  
 }