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