Coverage Report - org.mule.config.spring.SpringRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringRegistry
0%
0/62
0%
0/14
0
SpringRegistry$SpringContextDisposePhase
0%
0/7
N/A
0
SpringRegistry$SpringContextInitialisePhase
0%
0/5
N/A
0
 
 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  0
     protected AtomicBoolean springContextInitialised = new AtomicBoolean(false);
 58  
 
 59  
     public SpringRegistry(MuleContext muleContext)
 60  
     {
 61  0
         super(REGISTRY_ID, muleContext);
 62  0
     }
 63  
 
 64  
     public SpringRegistry(String id, MuleContext muleContext)
 65  
     {
 66  0
         super(id, muleContext);
 67  0
     }
 68  
 
 69  
     public SpringRegistry(ApplicationContext applicationContext, MuleContext muleContext)
 70  
     {
 71  0
         super(REGISTRY_ID, muleContext);
 72  0
         this.applicationContext = applicationContext;
 73  0
     }
 74  
 
 75  
     public SpringRegistry(String id, ApplicationContext applicationContext, MuleContext muleContext)
 76  
     {
 77  0
         super(id, muleContext);
 78  0
         this.applicationContext = applicationContext;
 79  0
     }
 80  
 
 81  
     public SpringRegistry(ConfigurableApplicationContext applicationContext, ApplicationContext parentContext, MuleContext muleContext)
 82  
     {
 83  0
         super(REGISTRY_ID, muleContext);
 84  0
         applicationContext.setParent(parentContext);
 85  0
         this.applicationContext = applicationContext;
 86  0
     }
 87  
 
 88  
     public SpringRegistry(String id, ConfigurableApplicationContext applicationContext, ApplicationContext parentContext, MuleContext muleContext)
 89  
     {
 90  0
         super(id, muleContext);
 91  0
         applicationContext.setParent(parentContext);
 92  0
         this.applicationContext = applicationContext;
 93  0
     }
 94  
 
 95  
     @Override
 96  
     protected void doInitialise() throws InitialisationException
 97  
     {
 98  0
         if (applicationContext instanceof ConfigurableApplicationContext)
 99  
         {
 100  0
             ((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  0
         springContextInitialised.set(true);
 104  0
     }
 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  0
         if (!this.springContextInitialised.get())
 112  
         {
 113  0
             return;
 114  
         }
 115  
 
 116  0
         if (applicationContext instanceof ConfigurableApplicationContext
 117  
                 && ((ConfigurableApplicationContext) applicationContext).isActive())
 118  
         {
 119  0
             ((ConfigurableApplicationContext) applicationContext).close();
 120  
         }
 121  
 
 122  
         // release the circular implicit ref to MuleContext
 123  0
         applicationContext = null;
 124  
 
 125  0
         this.springContextInitialised.set(false);
 126  0
     }
 127  
 
 128  
     @Override
 129  
     protected RegistryLifecycleManager createLifecycleManager()
 130  
     {
 131  0
         Map<String, LifecyclePhase> phases = new HashMap<String, LifecyclePhase>(3);
 132  0
         phases.put(Initialisable.PHASE_NAME, new SpringContextInitialisePhase());
 133  0
         phases.put(Startable.PHASE_NAME, new MuleContextStartPhase());
 134  0
         phases.put(Stoppable.PHASE_NAME, new MuleContextStopPhase());
 135  0
         phases.put(Disposable.PHASE_NAME, new SpringContextDisposePhase());
 136  0
         return new RegistryLifecycleManager(getRegistryId(), this, phases);
 137  
     }
 138  
 
 139  
     public Object lookupObject(String key)
 140  
     {
 141  0
         if (StringUtils.isBlank(key))
 142  
         {
 143  0
             logger.warn(
 144  
                     MessageFactory.createStaticMessage("Detected a lookup attempt with an empty or null key"),
 145  
                     new Throwable().fillInStackTrace());
 146  0
             return null;
 147  
         }
 148  
 
 149  0
         if (key.equals(SPRING_APPLICATION_CONTEXT) && applicationContext != null)
 150  
         {
 151  0
             return applicationContext;
 152  
         }
 153  
         else
 154  
         {
 155  
             try
 156  
             {
 157  0
                 return applicationContext.getBean(key);
 158  
             }
 159  0
             catch (NoSuchBeanDefinitionException e)
 160  
             {
 161  0
                 logger.debug(e);
 162  0
                 return null;
 163  
             }
 164  
         }
 165  
     }
 166  
 
 167  
     public <T> Collection<T> lookupObjects(Class<T> type)
 168  
     {
 169  0
         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  0
         return internalLookupByType(type, false, false).values();
 179  
     }
 180  
 
 181  
     @SuppressWarnings("unchecked")
 182  
     public <T> Map<String, T> lookupByType(Class<T> type)
 183  
     {
 184  0
         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  0
             return applicationContext.getBeansOfType(type, nonSingletons, eagerInit);
 193  
         }
 194  0
         catch (FatalBeanException fbex)
 195  
         {
 196  
             // FBE is a result of a broken config, propagate it (see MULE-3297 for more details)
 197  0
             String message = String.format("Failed to lookup beans of type %s from the Spring registry", type);
 198  0
             throw new MuleRuntimeException(MessageFactory.createStaticMessage(message), fbex);
 199  
         }
 200  0
         catch (Exception e)
 201  
         {
 202  0
             logger.debug(e);
 203  0
             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  0
         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  0
         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  0
         throw new UnsupportedOperationException("Registry is read-only so objects cannot be registered or unregistered.");
 225  
     }
 226  
 
 227  
     public void unregisterObject(String key)
 228  
     {
 229  0
         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  0
         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  0
         return true;
 244  
     }
 245  
 
 246  
     public boolean isRemote()
 247  
     {
 248  0
         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  0
         {
 262  0
             super(Initialisable.PHASE_NAME, Initialisable.class, Disposable.PHASE_NAME);
 263  0
             registerSupportedPhase(NotInLifecyclePhase.PHASE_NAME);
 264  0
         }
 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  0
         }
 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  0
         {
 288  0
             super(Disposable.PHASE_NAME, Disposable.class, Initialisable.PHASE_NAME);
 289  0
             registerSupportedPhase(NotInLifecyclePhase.PHASE_NAME);
 290  
             //You can dispose from all phases
 291  0
             registerSupportedPhase(LifecyclePhase.ALL_PHASES);
 292  0
         }
 293  
 
 294  
         @Override
 295  
         public void applyLifecycle(Object o) throws LifecycleException
 296  
         {
 297  0
             doDispose();
 298  0
         }
 299  
     }
 300  
 
 301  
 }