Coverage Report - org.mule.config.spring.SpringXmlConfigurationBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringXmlConfigurationBuilder
0%
0/40
0%
0/8
0
 
 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.config.spring;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.config.ConfigurationException;
 11  
 import org.mule.api.lifecycle.LifecycleManager;
 12  
 import org.mule.api.lifecycle.Startable;
 13  
 import org.mule.api.registry.Registry;
 14  
 import org.mule.config.ConfigResource;
 15  
 import org.mule.config.builders.AbstractResourceConfigurationBuilder;
 16  
 import org.mule.config.i18n.MessageFactory;
 17  
 import org.springframework.context.ApplicationContext;
 18  
 import org.springframework.context.ConfigurableApplicationContext;
 19  
 
 20  
 /**
 21  
  * <code>SpringXmlConfigurationBuilder</code> enables Mule to be configured from a
 22  
  * Spring XML Configuration file used with Mule name-spaces. Multiple configuration
 23  
  * files can be loaded from this builder (specified as a comma-separated list).
 24  
  */
 25  
 public class SpringXmlConfigurationBuilder extends AbstractResourceConfigurationBuilder
 26  
 {
 27  
     public static final String MULE_DEFAULTS_CONFIG = "default-mule-config.xml";
 28  
     public static final String MULE_SPRING_CONFIG = "mule-spring-config.xml";
 29  
 
 30  
     /** Prepend "default-mule-config.xml" to the list of config resources. */
 31  0
     protected boolean useDefaultConfigResource = true;
 32  
 
 33  
     protected Registry registry;
 34  
 
 35  
     protected ApplicationContext parentContext;
 36  
 
 37  
     public SpringXmlConfigurationBuilder(String[] configResources) throws ConfigurationException
 38  
     {
 39  0
         super(configResources);
 40  0
     }
 41  
 
 42  
     public SpringXmlConfigurationBuilder(String configResources) throws ConfigurationException
 43  
     {
 44  0
         super(configResources);
 45  0
     }
 46  
 
 47  
     public SpringXmlConfigurationBuilder(ConfigResource[] configResources)
 48  
     {
 49  0
         super(configResources);
 50  0
     }
 51  
 
 52  
     @Override
 53  
     protected void doConfigure(MuleContext muleContext) throws Exception
 54  
     {
 55  
         ConfigResource[] allResources;
 56  0
         if (useDefaultConfigResource)
 57  
         {
 58  0
             allResources = new ConfigResource[configResources.length + 2];
 59  0
             allResources[0] = new ConfigResource(MULE_SPRING_CONFIG);
 60  0
             allResources[1] = new ConfigResource(MULE_DEFAULTS_CONFIG);
 61  0
             System.arraycopy(configResources, 0, allResources, 2, configResources.length);
 62  
         }
 63  
         else
 64  
         {
 65  0
             allResources = new ConfigResource[configResources.length + 1];
 66  0
             allResources[0] = new ConfigResource(MULE_SPRING_CONFIG);
 67  0
             System.arraycopy(configResources, 0, allResources, 1, configResources.length);
 68  
         }
 69  0
         createSpringRegistry(muleContext, createApplicationContext(muleContext, allResources));
 70  0
     }
 71  
 
 72  
     public void unconfigure(MuleContext muleContext)
 73  
     {
 74  0
         registry.dispose();
 75  0
         muleContext.removeRegistry(registry);
 76  0
         registry = null;
 77  0
         configured = false;
 78  0
     }
 79  
 
 80  
     protected ApplicationContext createApplicationContext(MuleContext muleContext,
 81  
                                                           ConfigResource[] configResources) throws Exception
 82  
     {
 83  0
         return new MuleApplicationContext(muleContext, configResources);
 84  
     }
 85  
 
 86  
     protected void createSpringRegistry(MuleContext muleContext, ApplicationContext applicationContext)
 87  
         throws Exception
 88  
     {
 89  0
         if (parentContext != null)
 90  
         {
 91  0
             if (applicationContext instanceof ConfigurableApplicationContext)
 92  
             {
 93  0
                 registry = new SpringRegistry((ConfigurableApplicationContext) applicationContext,
 94  
                     parentContext, muleContext);
 95  
             }
 96  
             else
 97  
             {
 98  0
                 throw new ConfigurationException(
 99  
                     MessageFactory.createStaticMessage("Cannot set a parent context if the ApplicationContext does not implement ConfigurableApplicationContext"));
 100  
             }
 101  
         }
 102  
         else
 103  
         {
 104  0
             registry = new SpringRegistry(applicationContext, muleContext);
 105  
         }
 106  
 
 107  
         // Note: The SpringRegistry must be created before
 108  
         // applicationContext.refresh() gets called because
 109  
         // some beans may try to look up other beans via the Registry during
 110  
         // preInstantiateSingletons().
 111  0
         muleContext.addRegistry(registry);
 112  0
         registry.initialise();
 113  0
     }
 114  
 
 115  
     @Override
 116  
     protected void applyLifecycle(LifecycleManager lifecycleManager) throws Exception
 117  
     {
 118  
         // If the MuleContext is started, start all objects in the new Registry.
 119  0
         if (lifecycleManager.isPhaseComplete(Startable.PHASE_NAME))
 120  
         {
 121  0
             lifecycleManager.fireLifecycle(Startable.PHASE_NAME);
 122  
         }
 123  0
     }
 124  
 
 125  
     public boolean isUseDefaultConfigResource()
 126  
     {
 127  0
         return useDefaultConfigResource;
 128  
     }
 129  
 
 130  
     public void setUseDefaultConfigResource(boolean useDefaultConfigResource)
 131  
     {
 132  0
         this.useDefaultConfigResource = useDefaultConfigResource;
 133  0
     }
 134  
 
 135  
     protected ApplicationContext getParentContext()
 136  
     {
 137  0
         return parentContext;
 138  
     }
 139  
 
 140  
     public void setParentContext(ApplicationContext parentContext)
 141  
     {
 142  0
         this.parentContext = parentContext;
 143  0
     }
 144  
 
 145  
 }