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