View Javadoc

1   /*
2    * $Id: MuleApplicationContext.java 22816 2011-09-01 22:02:55Z mike.schilling $
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      private static final ThreadLocal<MuleContext> currentMuleContext = new ThreadLocal<MuleContext>();
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          this(muleContext, convert(configResources));
53      }
54  
55      public MuleApplicationContext(MuleContext muleContext, Resource[] springResources) throws BeansException
56      {
57          this.muleContext = muleContext;
58          this.springResources = springResources;
59      }
60  
61      protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) 
62      {
63          super.prepareBeanFactory(beanFactory);
64          beanFactory.addBeanPostProcessor(new MuleContextPostProcessor(muleContext));
65          beanFactory.addBeanPostProcessor(new ExpressionEvaluatorPostProcessor(muleContext));
66          beanFactory.registerSingleton(MuleProperties.OBJECT_MULE_CONTEXT, muleContext);
67      }
68  
69      private static Resource[] convert(ConfigResource[] resources)
70      {
71          Resource[] configResources = new Resource[resources.length];
72          for (int i = 0; i < resources.length; i++)
73          {
74              ConfigResource resource = resources[i];
75              if(resource.getUrl()!=null)
76              {
77                  configResources[i] = new UrlResource(resource.getUrl());
78              }
79              else
80              {
81                  try
82                  {
83                      configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName());
84                  }
85                  catch (IOException e)
86                  {
87                      throw new RuntimeException(e);
88                  }
89              }
90          }
91          return configResources;
92      }
93  
94      @Override
95      protected Resource[] getConfigResources()
96      {
97          return springResources;
98      }
99  
100     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException
101     {
102         XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
103         //hook in our custom hierarchical reader
104         beanDefinitionReader.setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class);
105         //add error reporting
106         beanDefinitionReader.setProblemReporter(new MissingParserProblemReporter());
107 
108         // Communicate mule context to parsers
109         try
110         {
111             currentMuleContext.set(muleContext);
112             beanDefinitionReader.loadBeanDefinitions(springResources);
113         }
114         finally
115         {
116             currentMuleContext.remove();
117         }
118     }
119 
120     @Override
121     protected DefaultListableBeanFactory createBeanFactory()
122     {
123         //Copy all postProcessors defined in the defaultMuleConfig so that they get applied to the child container
124         DefaultListableBeanFactory bf = super.createBeanFactory();
125         if (getParent() != null)
126         {
127             //Copy over all processors
128             AbstractBeanFactory beanFactory = (AbstractBeanFactory)getParent().getAutowireCapableBeanFactory();
129             bf.copyConfigurationFrom(beanFactory);
130         }
131         return bf;
132     }
133 
134     public MuleContext getMuleContext()
135     {
136         return muleContext;
137     }
138 
139     public static ThreadLocal<MuleContext> getCurrentMuleContext()
140     {
141         return currentMuleContext;
142     }
143 }