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