View Javadoc
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          this(muleContext, convert(configResources));
49      }
50  
51      public MuleApplicationContext(MuleContext muleContext, Resource[] springResources) throws BeansException
52      {
53          this.muleContext = muleContext;
54          this.springResources = springResources;
55      }
56  
57      protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) 
58      {
59          super.prepareBeanFactory(beanFactory);
60          beanFactory.addBeanPostProcessor(new MuleContextPostProcessor(muleContext));
61          beanFactory.addBeanPostProcessor(new ExpressionEvaluatorPostProcessor(muleContext));
62          beanFactory.registerSingleton(MuleProperties.OBJECT_MULE_CONTEXT, muleContext);
63      }
64  
65      private static Resource[] convert(ConfigResource[] resources)
66      {
67          Resource[] configResources = new Resource[resources.length];
68          for (int i = 0; i < resources.length; i++)
69          {
70              ConfigResource resource = resources[i];
71              if(resource.getUrl()!=null)
72              {
73                  configResources[i] = new UrlResource(resource.getUrl());
74              }
75              else
76              {
77                  try
78                  {
79                      configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName());
80                  }
81                  catch (IOException e)
82                  {
83                      throw new RuntimeException(e);
84                  }
85              }
86          }
87          return configResources;
88      }
89  
90      @Override
91      protected Resource[] getConfigResources()
92      {
93          return springResources;
94      }
95  
96      protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException
97      {
98          XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
99          //hook in our custom hierarchical reader
100         beanDefinitionReader.setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class);
101         //add error reporting
102         beanDefinitionReader.setProblemReporter(new MissingParserProblemReporter());
103         beanDefinitionReader.loadBeanDefinitions(springResources);
104     }
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         DefaultListableBeanFactory bf = super.createBeanFactory();
111         if (getParent() != null)
112         {
113             //Copy over all processors
114             AbstractBeanFactory beanFactory = (AbstractBeanFactory)getParent().getAutowireCapableBeanFactory();
115             bf.copyConfigurationFrom(beanFactory);
116         }
117         return bf;
118     }
119 
120     public MuleContext getMuleContext()
121     {
122         return muleContext;
123     }
124 }