Coverage Report - org.mule.config.spring.MuleApplicationContext
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleApplicationContext
0%
0/33
0%
0/6
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.MuleProperties;
 11  
 import org.mule.config.ConfigResource;
 12  
 import org.mule.util.IOUtils;
 13  
 
 14  
 import java.io.IOException;
 15  
 
 16  
 import org.springframework.beans.BeansException;
 17  
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 18  
 import org.springframework.beans.factory.support.AbstractBeanFactory;
 19  
 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
 20  
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
 21  
 import org.springframework.context.support.AbstractXmlApplicationContext;
 22  
 import org.springframework.core.io.ByteArrayResource;
 23  
 import org.springframework.core.io.Resource;
 24  
 import org.springframework.core.io.UrlResource;
 25  
 
 26  
 /**
 27  
  * <code>MuleApplicationContext</code> is a simple extension application context
 28  
  * that allows resources to be loaded from the Classpath of file system using the
 29  
  * MuleBeanDefinitionReader.
 30  
  *
 31  
  */
 32  
 public class MuleApplicationContext extends AbstractXmlApplicationContext
 33  
 {
 34  
     private MuleContext muleContext;
 35  
     private Resource[] springResources;
 36  
 
 37  
     /**
 38  
      * Parses configuration files creating a spring ApplicationContext which is used
 39  
      * as a parent registry using the SpringRegistry registry implementation to wraps
 40  
      * the spring ApplicationContext
 41  
      * 
 42  
      * @param configResources
 43  
      * @see org.mule.config.spring.SpringRegistry
 44  
      */
 45  
     public MuleApplicationContext(MuleContext muleContext, ConfigResource[] configResources)
 46  
             throws BeansException
 47  
     {
 48  0
         this(muleContext, convert(configResources));
 49  0
     }
 50  
 
 51  
     public MuleApplicationContext(MuleContext muleContext, Resource[] springResources) throws BeansException
 52  0
     {
 53  0
         this.muleContext = muleContext;
 54  0
         this.springResources = springResources;
 55  0
     }
 56  
 
 57  
     protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) 
 58  
     {
 59  0
         super.prepareBeanFactory(beanFactory);
 60  0
         beanFactory.addBeanPostProcessor(new MuleContextPostProcessor(muleContext));
 61  0
         beanFactory.addBeanPostProcessor(new ExpressionEvaluatorPostProcessor(muleContext));
 62  0
         beanFactory.registerSingleton(MuleProperties.OBJECT_MULE_CONTEXT, muleContext);
 63  0
     }
 64  
 
 65  
     private static Resource[] convert(ConfigResource[] resources)
 66  
     {
 67  0
         Resource[] configResources = new Resource[resources.length];
 68  0
         for (int i = 0; i < resources.length; i++)
 69  
         {
 70  0
             ConfigResource resource = resources[i];
 71  0
             if(resource.getUrl()!=null)
 72  
             {
 73  0
                 configResources[i] = new UrlResource(resource.getUrl());
 74  
             }
 75  
             else
 76  
             {
 77  
                 try
 78  
                 {
 79  0
                     configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName());
 80  
                 }
 81  0
                 catch (IOException e)
 82  
                 {
 83  0
                     throw new RuntimeException(e);
 84  0
                 }
 85  
             }
 86  
         }
 87  0
         return configResources;
 88  
     }
 89  
 
 90  
     @Override
 91  
     protected Resource[] getConfigResources()
 92  
     {
 93  0
         return springResources;
 94  
     }
 95  
 
 96  
     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException
 97  
     {
 98  0
         XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
 99  
         //hook in our custom hierarchical reader
 100  0
         beanDefinitionReader.setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class);
 101  
         //add error reporting
 102  0
         beanDefinitionReader.setProblemReporter(new MissingParserProblemReporter());
 103  0
         beanDefinitionReader.loadBeanDefinitions(springResources);
 104  0
     }
 105  
 
 106  
     @Override
 107  
     protected DefaultListableBeanFactory createBeanFactory()
 108  
     {
 109  
         //Copy all postProcessors defined in the defaultMuleConfig so that they get applied to the child container
 110  0
         DefaultListableBeanFactory bf = super.createBeanFactory();
 111  0
         if (getParent() != null)
 112  
         {
 113  
             //Copy over all processors
 114  0
             AbstractBeanFactory beanFactory = (AbstractBeanFactory)getParent().getAutowireCapableBeanFactory();
 115  0
             bf.copyConfigurationFrom(beanFactory);
 116  
         }
 117  0
         return bf;
 118  
     }
 119  
 
 120  
     public MuleContext getMuleContext()
 121  
     {
 122  0
         return muleContext;
 123  
     }
 124  
 }