View Javadoc

1   /*
2    * $Id: SpringRegistryLifecycleManager.java 21528 2011-03-10 16:00:09Z 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  
11  package org.mule.config.spring;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.lifecycle.Disposable;
15  import org.mule.api.lifecycle.Initialisable;
16  import org.mule.api.lifecycle.LifecycleException;
17  import org.mule.api.lifecycle.LifecyclePhase;
18  import org.mule.api.lifecycle.Startable;
19  import org.mule.api.lifecycle.Stoppable;
20  import org.mule.api.registry.Registry;
21  import org.mule.lifecycle.EmptyLifecycleCallback;
22  import org.mule.lifecycle.RegistryLifecycleManager;
23  import org.mule.lifecycle.phases.ContainerManagedLifecyclePhase;
24  import org.mule.lifecycle.phases.MuleContextStartPhase;
25  import org.mule.lifecycle.phases.MuleContextStopPhase;
26  import org.mule.lifecycle.phases.NotInLifecyclePhase;
27  import org.mule.registry.AbstractRegistryBroker;
28  
29  public class SpringRegistryLifecycleManager extends RegistryLifecycleManager
30  {
31      public SpringRegistryLifecycleManager(String id, Registry object, MuleContext muleContext)
32      {
33          super(id, object, muleContext);
34      }
35  
36      protected void registerPhases()
37      {
38          registerPhase(NotInLifecyclePhase.PHASE_NAME, NOT_IN_LIFECYCLE_PHASE,
39              new EmptyLifecycleCallback<AbstractRegistryBroker>());
40          registerPhase(Initialisable.PHASE_NAME, new SpringContextInitialisePhase());
41          registerPhase(Startable.PHASE_NAME, new MuleContextStartPhase(),
42              new EmptyLifecycleCallback<AbstractRegistryBroker>());
43          registerPhase(Stoppable.PHASE_NAME, new MuleContextStopPhase(),
44              new EmptyLifecycleCallback<AbstractRegistryBroker>());
45          registerPhase(Disposable.PHASE_NAME, new SpringContextDisposePhase());
46      }
47  
48      // ///////////////////////////////////////////////////////////////////////////////////
49      // Spring custom lifecycle phases
50      // ///////////////////////////////////////////////////////////////////////////////////
51  
52      /**
53       * A lifecycle phase that will delegate any lifecycle invocations to a container
54       * such as Spring or Guice
55       */
56      class SpringContextInitialisePhase extends ContainerManagedLifecyclePhase
57      {
58          public SpringContextInitialisePhase()
59          {
60              super(Initialisable.PHASE_NAME, Initialisable.class, Disposable.PHASE_NAME);
61              registerSupportedPhase(NotInLifecyclePhase.PHASE_NAME);
62          }
63  
64          /**
65           * We don't need to apply any lifecycle here since Spring manages that for us
66           * 
67           * @param o the object apply lifecycle to. This parameter will be ignorred
68           * @throws LifecycleException never thrown
69           */
70          @Override
71          public void applyLifecycle(Object o) throws LifecycleException
72          {
73              // Spring starts initialised, do nothing here
74          }
75      }
76  
77      /**
78       * A lifecycle phase that will delegate to the
79       * {@link org.mule.config.spring.SpringRegistry#doDispose()} method which in turn
80       * will destroy the application context managed by this registry
81       */
82      class SpringContextDisposePhase extends ContainerManagedLifecyclePhase
83      {
84          public SpringContextDisposePhase()
85          {
86              super(Disposable.PHASE_NAME, Disposable.class, Initialisable.PHASE_NAME);
87              registerSupportedPhase(NotInLifecyclePhase.PHASE_NAME);
88              // You can dispose from all phases
89              registerSupportedPhase(LifecyclePhase.ALL_PHASES);
90          }
91  
92          @Override
93          public void applyLifecycle(Object o) throws LifecycleException
94          {
95              ((SpringRegistry) o).doDispose();
96          }
97      }
98  
99  }