/*
* $Id: MuleApplicationContext.java 21385 2011-03-01 18:00:52Z aperepel $
* --------------------------------------------------------------------------------------
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
*
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.config.spring;
import org.mule.api.MuleContext;
import org.mule.api.config.MuleProperties;
import org.mule.config.ConfigResource;
import org.mule.util.IOUtils;
import java.io.IOException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
/**
* <code>MuleApplicationContext</code> is a simple extension application context
* that allows resources to be loaded from the Classpath of file system using the
* MuleBeanDefinitionReader.
*
*/
public class MuleApplicationContext extends AbstractXmlApplicationContext
{
private MuleContext muleContext;
private Resource[] springResources;
/**
* Parses configuration files creating a spring ApplicationContext which is used
* as a parent registry using the SpringRegistry registry implementation to wraps
* the spring ApplicationContext
*
* @param configResources
* @see org.mule.config.spring.SpringRegistry
*/
public MuleApplicationContext(MuleContext muleContext, ConfigResource[] configResources)
throws BeansException
{
this(muleContext, convert(configResources));
}
public MuleApplicationContext(MuleContext muleContext, Resource[] springResources) throws BeansException
{
this.muleContext = muleContext;
this.springResources = springResources;
}
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory)
{
super.prepareBeanFactory(beanFactory);
beanFactory.addBeanPostProcessor(new MuleContextPostProcessor(muleContext));
beanFactory.addBeanPostProcessor(new ExpressionEvaluatorPostProcessor(muleContext));
beanFactory.registerSingleton(MuleProperties.OBJECT_MULE_CONTEXT, muleContext);
}
private static Resource[] convert(ConfigResource[] resources)
{
Resource[] configResources = new Resource[resources.length];
for (int i = 0; i < resources.length; i++)
{
ConfigResource resource = resources[i];
if(resource.getUrl()!=null)
{
configResources[i] = new UrlResource(resource.getUrl());
}
else
{
try
{
configResources[i] = new ByteArrayResource(IOUtils.toByteArray(resource.getInputStream()), resource.getResourceName());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
return configResources;
}
@Override
protected Resource[] getConfigResources()
{
return springResources;
}
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException
{
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
//hook in our custom hierarchical reader
beanDefinitionReader.setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class);
//add error reporting
beanDefinitionReader.setProblemReporter(new MissingParserProblemReporter());
beanDefinitionReader.loadBeanDefinitions(springResources);
}
@Override
protected DefaultListableBeanFactory createBeanFactory()
{
//Copy all postProcessors defined in the defaultMuleConfig so that they get applied to the child container
DefaultListableBeanFactory bf = super.createBeanFactory();
if (getParent() != null)
{
//Copy over all processors
AbstractBeanFactory beanFactory = (AbstractBeanFactory)getParent().getAutowireCapableBeanFactory();
bf.copyConfigurationFrom(beanFactory);
}
return bf;
}
public MuleContext getMuleContext()
{
return muleContext;
}
}