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.lifecycle;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.context.MuleContextAware;
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.LifecycleException;
16  import org.mule.api.lifecycle.Startable;
17  import org.mule.api.lifecycle.Stoppable;
18  import org.mule.lifecycle.phases.NotInLifecyclePhase;
19  
20  /**
21   * This is a specialized class that extends {@link RegistryLifecycleManager} and will
22   * invoke lifecycle on the registry instance for the MuleContext.  This class must only be used by the MuleContext.
23   */
24  public class MuleContextLifecycleManager extends AbstractLifecycleManager<MuleContext> implements MuleContextAware
25  {
26  
27      private MuleContext muleContext;
28  
29      private MuleContextLifecycleCallback callback = new MuleContextLifecycleCallback();
30  
31      public MuleContextLifecycleManager()
32      {
33          //We cannot pass in a MuleContext on creation since the context is not actually created when this object is needed
34          super("MuleContext", null);
35      }
36  
37      @Override
38      protected void registerTransitions()
39      {
40          //init dispose
41          addDirectTransition(NotInLifecyclePhase.PHASE_NAME, Initialisable.PHASE_NAME);
42          addDirectTransition(NotInLifecyclePhase.PHASE_NAME, Disposable.PHASE_NAME);
43          addDirectTransition(Initialisable.PHASE_NAME, Startable.PHASE_NAME);
44          addDirectTransition(Initialisable.PHASE_NAME, Disposable.PHASE_NAME);
45  
46          //start stop
47          addDirectTransition(Startable.PHASE_NAME, Stoppable.PHASE_NAME);
48          addDirectTransition(Stoppable.PHASE_NAME, Startable.PHASE_NAME);
49          addDirectTransition(Stoppable.PHASE_NAME, Disposable.PHASE_NAME);
50      }
51  
52      public void setMuleContext(MuleContext context)
53      {
54          this.muleContext = context;
55          this.object = muleContext;
56      }
57  
58      public void fireLifecycle(String destinationPhase) throws LifecycleException
59      {
60          checkPhase(destinationPhase);
61          invokePhase(destinationPhase, object, callback);
62      }
63  
64       protected void invokePhase(String phase, Object object, LifecycleCallback callback) throws LifecycleException
65      {
66          try
67          {
68              setExecutingPhase(phase);
69              callback.onTransition(phase, object);
70              setCurrentPhase(phase);
71          }
72          catch (LifecycleException e)
73          {
74              throw e;
75          }
76          catch (MuleException e)
77          {
78              throw new LifecycleException(e, this);
79          }
80          finally
81          {
82              setExecutingPhase(null);
83          }
84  
85      }
86  
87      class MuleContextLifecycleCallback implements LifecycleCallback<MuleContext>
88      {
89          public void onTransition(String phaseName, MuleContext muleContext) throws MuleException
90          {
91              muleContext.getRegistry().fireLifecycle(phaseName);
92          }
93      }
94  }