muleapplicationcontext.java

来自「提供ESB 应用mule源代码 提供ESB 应用mule源代码」· Java 代码 · 共 122 行

JAVA
122
字号
/* * $Id: MuleApplicationContext.java 11082 2008-02-27 17:48:51Z tcarlson $ * -------------------------------------------------------------------------------------- * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.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 registry     * @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));    }    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;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?