View Javadoc

1   /*
2    * $Id: SpringRegistry.java 20277 2010-11-19 18:52:43Z 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.MuleRuntimeException;
15  import org.mule.api.lifecycle.Disposable;
16  import org.mule.api.lifecycle.Initialisable;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.lifecycle.LifecycleException;
19  import org.mule.api.lifecycle.LifecyclePhase;
20  import org.mule.api.lifecycle.Startable;
21  import org.mule.api.lifecycle.Stoppable;
22  import org.mule.api.registry.RegistrationException;
23  import org.mule.config.i18n.MessageFactory;
24  import org.mule.lifecycle.RegistryLifecycleManager;
25  import org.mule.lifecycle.phases.ContainerManagedLifecyclePhase;
26  import org.mule.lifecycle.phases.MuleContextStartPhase;
27  import org.mule.lifecycle.phases.MuleContextStopPhase;
28  import org.mule.lifecycle.phases.NotInLifecyclePhase;
29  import org.mule.registry.AbstractRegistry;
30  import org.mule.util.StringUtils;
31  
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.HashMap;
35  import java.util.Map;
36  import java.util.concurrent.atomic.AtomicBoolean;
37  
38  import org.springframework.beans.FatalBeanException;
39  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
40  import org.springframework.context.ApplicationContext;
41  import org.springframework.context.ConfigurableApplicationContext;
42  
43  public class SpringRegistry extends AbstractRegistry
44  {
45      public static final String REGISTRY_ID = "org.mule.Registry.Spring";
46  
47      /**
48       * Key used to lookup Spring Application Context from SpringRegistry via Mule's
49       * Registry interface.
50       */
51      public static final String SPRING_APPLICATION_CONTEXT = "springApplicationContext";
52  
53      protected ApplicationContext applicationContext;
54  
55      //This is used to track the Spring context lifecycle since there is no way to confirm the
56      //lifecycle phase from the application context
57      protected AtomicBoolean springContextInitialised = new AtomicBoolean(false);
58  
59      public SpringRegistry(MuleContext muleContext)
60      {
61          super(REGISTRY_ID, muleContext);
62      }
63  
64      public SpringRegistry(String id, MuleContext muleContext)
65      {
66          super(id, muleContext);
67      }
68  
69      public SpringRegistry(ApplicationContext applicationContext, MuleContext muleContext)
70      {
71          super(REGISTRY_ID, muleContext);
72          this.applicationContext = applicationContext;
73      }
74  
75      public SpringRegistry(String id, ApplicationContext applicationContext, MuleContext muleContext)
76      {
77          super(id, muleContext);
78          this.applicationContext = applicationContext;
79      }
80  
81      public SpringRegistry(ConfigurableApplicationContext applicationContext, ApplicationContext parentContext, MuleContext muleContext)
82      {
83          super(REGISTRY_ID, muleContext);
84          applicationContext.setParent(parentContext);
85          this.applicationContext = applicationContext;
86      }
87  
88      public SpringRegistry(String id, ConfigurableApplicationContext applicationContext, ApplicationContext parentContext, MuleContext muleContext)
89      {
90          super(id, muleContext);
91          applicationContext.setParent(parentContext);
92          this.applicationContext = applicationContext;
93      }
94  
95      @Override
96      protected void doInitialise() throws InitialisationException
97      {
98          if (applicationContext instanceof ConfigurableApplicationContext)
99          {
100             ((ConfigurableApplicationContext) applicationContext).refresh();
101         }
102         //This is used to track the Spring context lifecycle since there is no way to confirm the lifecycle phase from the application context
103         springContextInitialised.set(true);
104     }
105 
106     @Override
107     public void doDispose()
108     {
109         // check we aren't trying to close a context which has never been started,
110         // spring's appContext.isActive() isn't working for this case
111         if (!this.springContextInitialised.get())
112         {
113             return;
114         }
115 
116         if (applicationContext instanceof ConfigurableApplicationContext
117                 && ((ConfigurableApplicationContext) applicationContext).isActive())
118         {
119             ((ConfigurableApplicationContext) applicationContext).close();
120         }
121 
122         // release the circular implicit ref to MuleContext
123         applicationContext = null;
124 
125         this.springContextInitialised.set(false);
126     }
127 
128     @Override
129     protected RegistryLifecycleManager createLifecycleManager()
130     {
131         Map<String, LifecyclePhase> phases = new HashMap<String, LifecyclePhase>(3);
132         phases.put(Initialisable.PHASE_NAME, new SpringContextInitialisePhase());
133         phases.put(Startable.PHASE_NAME, new MuleContextStartPhase());
134         phases.put(Stoppable.PHASE_NAME, new MuleContextStopPhase());
135         phases.put(Disposable.PHASE_NAME, new SpringContextDisposePhase());
136         return new RegistryLifecycleManager(getRegistryId(), this, phases);
137     }
138 
139     public Object lookupObject(String key)
140     {
141         if (StringUtils.isBlank(key))
142         {
143             logger.warn(
144                     MessageFactory.createStaticMessage("Detected a lookup attempt with an empty or null key"),
145                     new Throwable().fillInStackTrace());
146             return null;
147         }
148 
149         if (key.equals(SPRING_APPLICATION_CONTEXT) && applicationContext != null)
150         {
151             return applicationContext;
152         }
153         else
154         {
155             try
156             {
157                 return applicationContext.getBean(key);
158             }
159             catch (NoSuchBeanDefinitionException e)
160             {
161                 logger.debug(e);
162                 return null;
163             }
164         }
165     }
166 
167     public <T> Collection<T> lookupObjects(Class<T> type)
168     {
169         return lookupByType(type).values();
170     }
171     
172     /**
173      * For lifecycle we only want spring to return singleton objects from it's application context
174      */
175     @Override
176     public <T> Collection<T> lookupObjectsForLifecycle(Class<T> type)
177     {
178         return internalLookupByType(type, false, false).values();
179     }
180 
181     @SuppressWarnings("unchecked")
182     public <T> Map<String, T> lookupByType(Class<T> type)
183     {
184         return internalLookupByType(type, true, true);
185     }
186 
187     @SuppressWarnings("unchecked")
188     protected <T> Map<String, T> internalLookupByType(Class<T> type, boolean nonSingletons, boolean eagerInit)
189     {
190         try
191         {
192             return applicationContext.getBeansOfType(type, nonSingletons, eagerInit);
193         }
194         catch (FatalBeanException fbex)
195         {
196             // FBE is a result of a broken config, propagate it (see MULE-3297 for more details)
197             String message = String.format("Failed to lookup beans of type %s from the Spring registry", type);
198             throw new MuleRuntimeException(MessageFactory.createStaticMessage(message), fbex);
199         }
200         catch (Exception e)
201         {
202             logger.debug(e);
203             return Collections.emptyMap();
204         }
205     }
206     
207     
208     ////////////////////////////////////////////////////////////////////////////////////
209     // Registry is read-only
210     ////////////////////////////////////////////////////////////////////////////////////
211 
212     public void registerObject(String key, Object value) throws RegistrationException
213     {
214         throw new UnsupportedOperationException("Registry is read-only so objects cannot be registered or unregistered.");
215     }
216 
217     public void registerObject(String key, Object value, Object metadata) throws RegistrationException
218     {
219         throw new UnsupportedOperationException("Registry is read-only so objects cannot be registered or unregistered.");
220     }
221 
222     public void registerObjects(Map objects) throws RegistrationException
223     {
224         throw new UnsupportedOperationException("Registry is read-only so objects cannot be registered or unregistered.");
225     }
226 
227     public void unregisterObject(String key)
228     {
229         throw new UnsupportedOperationException("Registry is read-only so objects cannot be registered or unregistered.");
230     }
231 
232     public void unregisterObject(String key, Object metadata) throws RegistrationException
233     {
234         throw new UnsupportedOperationException("Registry is read-only so objects cannot be registered or unregistered.");
235     }
236 
237     ////////////////////////////////////////////////////////////////////////////////////
238     // Registry meta-data
239     ////////////////////////////////////////////////////////////////////////////////////
240 
241     public boolean isReadOnly()
242     {
243         return true;
244     }
245 
246     public boolean isRemote()
247     {
248         return false;
249     }
250 
251     /////////////////////////////////////////////////////////////////////////////////////
252     // Spring custom lifecycle phases
253     /////////////////////////////////////////////////////////////////////////////////////
254 
255     /**
256      * A lifecycle phase that will delegate any lifecycle invocations to a container such as Spring or Guice
257      */
258     class SpringContextInitialisePhase extends ContainerManagedLifecyclePhase
259     {
260         public SpringContextInitialisePhase()
261         {
262             super(Initialisable.PHASE_NAME, Initialisable.class, Disposable.PHASE_NAME);
263             registerSupportedPhase(NotInLifecyclePhase.PHASE_NAME);
264         }
265 
266         /**
267          * We don't need to apply any lifecycle here since Spring manages that for us
268          *
269          * @param o the object apply lifecycle to.  This parameter will be ignorred
270          * @throws LifecycleException never thrown
271          */
272         @Override
273         public void applyLifecycle(Object o) throws LifecycleException
274         {
275             //Spring starts initialised, do nothing here
276         }
277     }
278 
279 
280     /**
281      * A lifecycle phase that will delegate to the {@link org.mule.config.spring.SpringRegistry#doDispose()} method which in
282      * turn will destroy the application context managed by this registry
283      */
284     class SpringContextDisposePhase extends ContainerManagedLifecyclePhase
285     {
286         public SpringContextDisposePhase()
287         {
288             super(Disposable.PHASE_NAME, Disposable.class, Initialisable.PHASE_NAME);
289             registerSupportedPhase(NotInLifecyclePhase.PHASE_NAME);
290             //You can dispose from all phases
291             registerSupportedPhase(LifecyclePhase.ALL_PHASES);
292         }
293 
294         @Override
295         public void applyLifecycle(Object o) throws LifecycleException
296         {
297             doDispose();
298         }
299     }
300 
301 }