defaultbeanassembler.java

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

JAVA
461
字号
                        pv = new PropertyValue(newName, new ManagedMap());                        targetProperties.addPropertyValue(pv);                    }                    retrieveMap(pv.getValue()).put(                            sourceProperties.getPropertyValue(ChildMapEntryDefinitionParser.KEY).getValue(),                            sourceProperties.getPropertyValue(ChildMapEntryDefinitionParser.VALUE).getValue());                }                else                {                    targetProperties.addPropertyValue(newName, bean.getBeanDefinition());                }            }        }    }    private static List retrieveList(Object value)    {        if (value instanceof List)        {            return (List) value;        }        else if (isDefinitionOf(value, MapCombiner.class))        {            return (List) unpackDefinition(value, MapCombiner.LIST);        }        else        {            throw new ClassCastException("Collection not of expected type: " + value);        }    }    private static Map retrieveMap(Object value)    {        if (value instanceof Map)        {            return (Map) value;        }        else if (isDefinitionOf(value, MapFactoryBean.class))        {            return (Map) unpackDefinition(value, "sourceMap");        }        else        {            throw new ClassCastException("Map not of expected type: " + value);        }    }    private static boolean isDefinitionOf(Object value, Class clazz)    {        return value instanceof BeanDefinition &&                ((BeanDefinition) value).getBeanClassName().equals(clazz.getName());    }    private static Object unpackDefinition(Object definition, String name)    {        return ((BeanDefinition) definition).getPropertyValues().getPropertyValue(name).getValue();    }    /**     * Copy the properties from the bean we have been building into the target (typically     * the parent bean).  In other words, the bean is a facade for the target.     *     * <p>This assumes that the source bean has been constructed correctly (ie the decisions about     * what is ignored, a map, a list, etc) have already been made.   All it does (apart from a     * direct copy) is merge collections with those on the target when necessary.     */    public void copyBeanToTarget()    {        logger.debug("copy " + bean.getBeanDefinition().getBeanClassName() + " -> " + target.getBeanClassName());        assertTargetPresent();        MutablePropertyValues targetProperties = target.getPropertyValues();        MutablePropertyValues beanProperties = bean.getBeanDefinition().getPropertyValues();        for (int i=0;i < beanProperties.size(); i++)        {            PropertyValue propertyValue = beanProperties.getPropertyValues()[i];            addPropertyWithoutReference(targetProperties, new SinglePropertyLiteral(),                    propertyValue.getName(), propertyValue.getValue());        }    }    public void setBeanFlag(String flag)    {        MuleHierarchicalBeanDefinitionParserDelegate.setFlag(bean.getRawBeanDefinition(), flag);    }    protected void assertTargetPresent()    {        if (null == target)        {            throw new IllegalStateException("Bean assembler does not have a target");        }    }    protected void addPropertyWithReference(MutablePropertyValues properties, SingleProperty config,                                            String name, Object value)    {        if (!config.isIgnored())        {            if (config.isReference())            {                if (value instanceof String)                {                    if (((String) value).trim().indexOf(" ") > -1)                    {                        config.setCollection();                    }                    for (StringTokenizer refs = new StringTokenizer((String) value); refs.hasMoreTokens();)                    {                        String ref = refs.nextToken();                        if (logger.isDebugEnabled())                        {                            logger.debug("possible non-dependent reference: " + name + "/" + ref);                        }                        addPropertyWithoutReference(properties, config, name, new RuntimeBeanReference(ref));                    }                }                else                {                    throw new IllegalArgumentException("Bean reference must be a String: " + name + "/" + value);                }            }            else            {                addPropertyWithoutReference(properties, config, name, value);            }        }    }    protected void addPropertyWithoutReference(MutablePropertyValues properties, SingleProperty config,                                               String name, Object value)    {        if (!config.isIgnored())        {            if (logger.isDebugEnabled())            {                logger.debug(name + ": " + value);            }            Object oldValue = null;            if (properties.contains(name))            {                oldValue = properties.getPropertyValue(name).getValue();            }            // merge collections            if (config.isCollection() || oldValue instanceof Collection || value instanceof Collection)            {                Collection values = new ManagedList();                if (null != oldValue)                {                    properties.removePropertyValue(name);                    if (oldValue instanceof Collection)                    {                        values.addAll((Collection) oldValue);                    }                    else                    {                        values.add(oldValue);                    }                }                if (value instanceof Collection)                {                    values.addAll((Collection) value);                }                else                {                    values.add(value);                }                properties.addPropertyValue(name, values);            }            else            {                properties.addPropertyValue(name, value);            }        }    }    protected static String bestGuessName(PropertyConfiguration config, String oldName, String className)    {        String newName = config.translateName(oldName);        if (! methodExists(className, newName))        {            String plural = newName + "s";            if (methodExists(className, plural))            {                // this lets us avoid setting addCollection in the majority of cases                config.addCollection(oldName);                return plural;            }            if (newName.endsWith("y"))            {                String pluraly = newName.substring(0, newName.length()-1) + "ies";                if (methodExists(className, pluraly))                {                    // this lets us avoid setting addCollection in the majority of cases                    config.addCollection(oldName);                    return pluraly;                }            }        }        return newName;    }    protected static boolean methodExists(String className, String newName)    {        try        {            // is there a better way than this?!            // BeanWrapperImpl instantiates an instance, which we don't want.            // if there really is no better way, i guess it should go in            // class or bean utils.            Class clazz = ClassUtils.getClass(className);            Method[] methods = clazz.getMethods();            String setter = "set" + newName;            for (int i = 0; i < methods.length; ++i)            {                if (methods[i].getName().equalsIgnoreCase(setter))                {                    return true;                }            }        }        catch (Exception e)        {            logger.debug("Could not access bean class " + className, e);        }        return false;    }}

⌨️ 快捷键说明

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