View Javadoc

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