defaultbeanassembler.java

来自「提供ESB 应用mule源代码 提供ESB 应用mule源代码」· Java 代码 · 共 461 行 · 第 1/2 页

JAVA
461
字号
/* * $Id: DefaultBeanAssembler.java 10494 2008-01-23 21:09:56Z acooke $ * -------------------------------------------------------------------------------------- * 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.parsers.assembly;import org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate;import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;import org.mule.config.spring.parsers.assembly.configuration.SingleProperty;import org.mule.config.spring.parsers.assembly.configuration.SinglePropertyLiteral;import org.mule.config.spring.parsers.assembly.configuration.SinglePropertyWrapper;import org.mule.config.spring.parsers.collection.ChildListEntryDefinitionParser;import org.mule.config.spring.parsers.collection.ChildMapEntryDefinitionParser;import org.mule.config.spring.util.SpringXMLUtils;import org.mule.util.ClassUtils;import org.mule.util.MapCombiner;import java.lang.reflect.Method;import java.util.Collection;import java.util.List;import java.util.Map;import java.util.StringTokenizer;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.MutablePropertyValues;import org.springframework.beans.PropertyValue;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.config.BeanDefinition;import org.springframework.beans.factory.config.MapFactoryBean;import org.springframework.beans.factory.config.RuntimeBeanReference;import org.springframework.beans.factory.support.BeanDefinitionBuilder;import org.springframework.beans.factory.support.ManagedList;import org.springframework.beans.factory.support.ManagedMap;import org.w3c.dom.Attr;public class DefaultBeanAssembler implements BeanAssembler{    private static Log logger = LogFactory.getLog(DefaultBeanAssembler.class);    private PropertyConfiguration beanConfig;    private BeanDefinitionBuilder bean;    private PropertyConfiguration targetConfig;    private BeanDefinition target;    public DefaultBeanAssembler(PropertyConfiguration beanConfig, BeanDefinitionBuilder bean,                                PropertyConfiguration targetConfig, BeanDefinition target)    {        this.beanConfig = beanConfig;        this.bean = bean;        this.targetConfig = targetConfig;        this.target = target;    }    public BeanDefinitionBuilder getBean()    {        return bean;    }    protected void setBean(BeanDefinitionBuilder bean)    {        this.bean = bean;    }    public BeanDefinition getTarget()    {        return target;    }    protected PropertyConfiguration getBeanConfig()    {        return beanConfig;    }    protected PropertyConfiguration getTargetConfig()    {        return targetConfig;    }    /**     * Add a property defined by an attribute to the bean we are constructing.     *     * <p>Since an attribute value is always a string, we don't have to deal with complex types     * here - the only issue is whether or not we have a reference.  References are detected     * by explicit annotation or by the "-ref" at the end of an attribute name.  We do not     * check the Spring repo to see if a name already exists since that could lead to     * unpredictable behaviour.     * (see {@link org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration})     * @param attribute The attribute to add     */    public void extendBean(Attr attribute)    {        String oldName = SpringXMLUtils.attributeName(attribute);        if (!beanConfig.isIgnored(oldName))        {            logger.debug(attribute + " for " + bean.getBeanDefinition().getBeanClassName());            String oldValue = attribute.getNodeValue();            String newName = bestGuessName(beanConfig, oldName, bean.getBeanDefinition().getBeanClassName());            Object newValue = beanConfig.translateValue(oldName, oldValue);            addPropertyWithReference(bean.getBeanDefinition().getPropertyValues(),                    beanConfig.getSingleProperty(oldName), newName, newValue);        }    }    /**     * Allow direct access to bean for major hacks     *     * @param newName The property name to add     * @param newValue The property value to add     * @param isReference If true, a bean reference is added (and newValue must be a String)     */    public void extendBean(String newName, Object newValue, boolean isReference)    {        addPropertyWithReference(bean.getBeanDefinition().getPropertyValues(),                new SinglePropertyLiteral(isReference), newName, newValue);    }    /**     * Add a property defined by an attribute to the parent of the bean we are constructing.     *     * <p>This is unusual.  Normally you want {@link #extendBean(org.w3c.dom.Attr)}.     * @param attribute The attribute to add     */    public void extendTarget(Attr attribute)    {        String oldName = SpringXMLUtils.attributeName(attribute);        String oldValue = attribute.getNodeValue();        String newName = bestGuessName(targetConfig, oldName, bean.getBeanDefinition().getBeanClassName());        Object newValue = targetConfig.translateValue(oldName, oldValue);        addPropertyWithReference(target.getPropertyValues(),                targetConfig.getSingleProperty(oldName), newName, newValue);    }    /**     * Allow direct access to target for major hacks     *     * @param newName The property name to add     * @param newValue The property value to add     * @param isReference If true, a bean reference is added (and newValue must be a String)     */    public void extendTarget(String newName, Object newValue, boolean isReference)    {        assertTargetPresent();        addPropertyWithReference(target.getPropertyValues(),                new SinglePropertyLiteral(isReference), newName, newValue);    }        public void extendTarget(String oldName, String newName, Object newValue)    {        assertTargetPresent();        addPropertyWithReference(target.getPropertyValues(),                new SinglePropertyWrapper(oldName, getTargetConfig()), newName, newValue);    }    /**     * Insert the bean we have built into the target (typically the parent bean).     *     * <p>This is the most complex case because the bean can have an aribtrary type.     *      * @param oldName The identifying the bean (typically element name).     */    public void insertBeanInTarget(String oldName)    {        logger.debug("insert " + bean.getBeanDefinition().getBeanClassName() + " -> " + target.getBeanClassName());        assertTargetPresent();        String beanClass = bean.getBeanDefinition().getBeanClassName();        PropertyValues sourceProperties = bean.getRawBeanDefinition().getPropertyValues();        String newName = bestGuessName(targetConfig, oldName, target.getBeanClassName());        MutablePropertyValues targetProperties = target.getPropertyValues();        PropertyValue pv = targetProperties.getPropertyValue(newName);        Object oldValue = null == pv ? null : pv.getValue();        if (! targetConfig.isIgnored(oldName))        {            if (targetConfig.isCollection(oldName) ||                    beanClass.equals(ChildListEntryDefinitionParser.ListEntry.class.getName()))            {                if (null == oldValue)                {                    if (beanClass.equals(ChildMapEntryDefinitionParser.KeyValuePair.class.getName()) ||                            beanClass.equals(MapEntryCombiner.class.getName()) ||                            beanClass.equals(MapFactoryBean.class.getName()))                    {                        // a collection of maps requires an extra intermediate object that does the                        // lazy combination/caching of maps when first used                        BeanDefinitionBuilder combiner = BeanDefinitionBuilder.rootBeanDefinition(MapCombiner.class);                        targetProperties.addPropertyValue(newName, combiner.getBeanDefinition());                        MutablePropertyValues combinerProperties = combiner.getBeanDefinition().getPropertyValues();                        oldValue = new ManagedList();                        pv = new PropertyValue(MapCombiner.LIST, oldValue);                        combinerProperties.addPropertyValue(pv);                    }                    else                    {                        oldValue = new ManagedList();                        pv = new PropertyValue(newName, oldValue);                        targetProperties.addPropertyValue(pv);                    }                }                List list = retrieveList(oldValue);                if (ChildMapEntryDefinitionParser.KeyValuePair.class.getName().equals(beanClass))                {                    list.add(new ManagedMap());                    retrieveMap(list.get(list.size() - 1)).put(                            sourceProperties.getPropertyValue(ChildMapEntryDefinitionParser.KEY).getValue(),                            sourceProperties.getPropertyValue(ChildMapEntryDefinitionParser.VALUE).getValue());                }                else if (beanClass.equals(ChildListEntryDefinitionParser.ListEntry.class.getName()))                {                    list.add(sourceProperties.getPropertyValue(ChildListEntryDefinitionParser.VALUE).getValue());                }                else                {                    list.add(bean.getBeanDefinition());                }            }            else            {                // not a collection                if (ChildMapEntryDefinitionParser.KeyValuePair.class.getName().equals(beanClass))                {                    if (null == pv || null == oldValue)                    {

⌨️ 快捷键说明

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