Example usage for org.springframework.beans.factory.support ManagedList setMergeEnabled

List of usage examples for org.springframework.beans.factory.support ManagedList setMergeEnabled

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support ManagedList setMergeEnabled.

Prototype

public void setMergeEnabled(boolean mergeEnabled) 

Source Link

Document

Set whether merging should be enabled for this collection, in case of a 'parent' collection value being present.

Usage

From source file:com.bstek.dorado.spring.ClassTypeListShortCutDecorator.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
    AbstractBeanDefinition beanDef = (AbstractBeanDefinition) definition.getBeanDefinition();
    MutablePropertyValues propertyValues = (beanDef.getPropertyValues() == null) ? new MutablePropertyValues()
            : beanDef.getPropertyValues();

    ManagedList list = null;
    boolean firstPropertyValue = propertyValues.getPropertyValue(IMPL_TYPES) == null;

    if (!firstPropertyValue) {
        list = (ManagedList) (propertyValues.getPropertyValue(IMPL_TYPES).getValue());
    } else {/*  ww w . ja v a 2s .  c o  m*/
        list = new ManagedList();
        list.setSource(node);
        list.setMergeEnabled(true);
        propertyValues.addPropertyValue(IMPL_TYPES, list);
        beanDef.setPropertyValues(propertyValues);
    }

    Element el = (Element) node;
    String className = el.getAttribute("name");
    try {
        list.add(Class.forName(className));
    } catch (ClassNotFoundException e) {
        logger.warn(e, e);
    }
    return definition;
}

From source file:de.acosix.alfresco.utility.common.spring.PropertyAlteringBeanFactoryPostProcessor.java

protected Object handleListValues(final PropertyValue configuredValue) {
    final Object value;
    LOGGER.debug(// w ww.  ja v a  2s  . c  o  m
            "[{}] List of values / bean reference names has been configured - treating property {} of {} as <list>",
            this.beanName, this.propertyName, this.targetBeanName);

    final ManagedList<Object> list = new ManagedList<>();

    if (this.merge && configuredValue != null) {
        final Object configuredValueDefinition = configuredValue.getValue();
        if (configuredValueDefinition instanceof ManagedList<?>) {
            final ManagedList<?> oldList = (ManagedList<?>) configuredValueDefinition;
            list.setElementTypeName(oldList.getElementTypeName());
            list.setMergeEnabled(oldList.isMergeEnabled());
            list.setSource(oldList.getSource());

            list.addAll(oldList);

            LOGGER.debug("[{}] Merged existing value list values: {}", this.beanName, oldList);
        }
    }

    List<Object> valuesToAdd;
    if (this.valueList != null) {
        LOGGER.debug("[{}] List of configured values for {} of {}: ", this.beanName, this.propertyName,
                this.targetBeanName, this.valueList);
        valuesToAdd = this.valueList;
    } else {
        LOGGER.debug("[{}] List of configured bean reference names for {} of {}: ", this.beanName,
                this.propertyName, this.targetBeanName, this.beanReferenceNameList);
        valuesToAdd = new ArrayList<>();
        for (final String beanReferenceName : this.beanReferenceNameList) {
            valuesToAdd.add(new RuntimeBeanReference(beanReferenceName));
        }
    }

    if (this.addAsFirst) {
        LOGGER.debug("[{}] Adding new entries at start of list for {} of {}", this.beanName, this.propertyName,
                this.targetBeanName);
        list.addAll(0, valuesToAdd);
    } else if (this.addAtIndex >= 0 && this.addAtIndex < list.size()) {
        LOGGER.debug("[{}] Adding new entries at position {} of list for {} of {}", this.beanName,
                String.valueOf(this.addAtIndex), this.propertyName, this.targetBeanName);
        list.addAll(this.addAtIndex, valuesToAdd);
    } else {
        LOGGER.debug("[{}] Adding new entries at end of list for {} of {}", this.beanName, this.propertyName,
                this.targetBeanName);
        list.addAll(valuesToAdd);
    }

    if (!list.isMergeEnabled() && this.mergeParent) {
        LOGGER.debug("[{}] Enabling \"merge\" for <list> on {} of {}", this.beanName, this.propertyName,
                this.targetBeanName);
    }
    list.setMergeEnabled(list.isMergeEnabled() || this.mergeParent);
    value = list;
    return value;
}

From source file:org.eclipse.gemini.blueprint.blueprint.config.internal.BlueprintParser.java

/**
 * Parse a list element./*  w ww .  java  2 s. co  m*/
 */
public List<?> parseListElement(Element collectionEle, BeanDefinition bd) {
    String defaultElementType = collectionEle.getAttribute(BeanDefinitionParserDelegate.VALUE_TYPE_ATTRIBUTE);
    NodeList nl = collectionEle.getChildNodes();
    ManagedList<Object> target = new ManagedList<Object>(nl.getLength());
    target.setSource(extractSource(collectionEle));
    target.setElementTypeName(defaultElementType);
    target.setMergeEnabled(parseMergeAttribute(collectionEle));
    parseCollectionElements(nl, target, bd, defaultElementType);
    return target;
}

From source file:org.kuali.rice.krad.datadictionary.DictionaryBeanFactoryPostProcessor.java

/**
 * Iterates through the list values and calls helpers to process the value
 *
 * @param listVal the list to process/* w ww  .  j av a2s .c  o  m*/
 * @param propertyName name of the property which has the list value
 * @param nestedBeanStack stack of bean containers which contains the list property
 */
protected void visitList(List<?> listVal, String propertyName, Stack<BeanDefinitionHolder> nestedBeanStack) {
    boolean isMergeEnabled = false;
    if (listVal instanceof ManagedList) {
        isMergeEnabled = ((ManagedList) listVal).isMergeEnabled();
    }

    ManagedList newList = new ManagedList();
    newList.setMergeEnabled(isMergeEnabled);

    for (int i = 0; i < listVal.size(); i++) {
        Object elem = listVal.get(i);

        if (isStringValue(elem)) {
            elem = processListStringPropertyValue(propertyName, listVal, getString(elem), i, nestedBeanStack,
                    beanProcessors);
        } else {
            elem = visitPropertyValue(propertyName, elem, nestedBeanStack);
        }

        newList.add(i, elem);
    }

    listVal.clear();
    listVal.addAll(newList);
}

From source file:org.kuali.rice.krad.datadictionary.parse.CustomSchemaParser.java

/**
 * Parses a list of elements into a list of beans/standard content.
 *
 * @param grandChildren - The list of beans/content in a bean property
 * @param child - The property tag for the parent.
 * @param parent - The parent bean that the tag is nested in.
 * @param parserContext - Provided information and functionality regarding current bean set.
 * @return A managedList of the nested content.
 *///from  w ww.  ja va 2  s. c  om
protected ManagedList parseList(ArrayList<Element> grandChildren, Element child, BeanDefinitionBuilder parent,
        ParserContext parserContext) {
    ArrayList<Object> listItems = new ArrayList<Object>();

    for (int i = 0; i < grandChildren.size(); i++) {
        Element grandChild = grandChildren.get(i);

        if (grandChild.getTagName().compareTo("value") == 0) {
            listItems.add(grandChild.getTextContent());
        } else {
            listItems.add(parseBean(grandChild, parent, parserContext));
        }
    }

    String merge = child.getAttribute("merge");

    ManagedList beans = new ManagedList(listItems.size());
    beans.addAll(listItems);

    if (merge != null) {
        beans.setMergeEnabled(Boolean.valueOf(merge));
    }

    return beans;
}

From source file:org.kuali.rice.krad.datadictionary.uif.UifBeanFactoryPostProcessor.java

@SuppressWarnings("unchecked")
protected void visitList(String nestedPropertyName, String propertyName, BeanDefinition beanDefinition,
        Map<String, String> parentExpressionGraph, Map<String, String> expressionGraph, List listVal,
        ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) {
    boolean isMergeEnabled = false;
    if (listVal instanceof ManagedList) {
        isMergeEnabled = ((ManagedList) listVal).isMergeEnabled();
    }//  ww  w  .ja v a2s.c o  m

    ManagedList newList = new ManagedList();
    newList.setMergeEnabled(isMergeEnabled);

    // if merging, need to find size of parent list so we can know which element to set
    // when evaluating expressions
    int parentListSize = 0;
    if (isMergeEnabled && StringUtils.isNotBlank(beanDefinition.getParentName())) {
        BeanDefinition parentBeanDefinition = beanFactory
                .getMergedBeanDefinition(beanDefinition.getParentName());
        PropertyValue parentListPropertyValue = parentBeanDefinition.getPropertyValues()
                .getPropertyValue(propertyName);
        if (parentListPropertyValue != null) {
            List parentList = (List) parentListPropertyValue.getValue();
            parentListSize = parentList.size();
        }
    }

    for (int i = 0; i < listVal.size(); i++) {
        Object elem = listVal.get(i);

        int elementPosition = i + parentListSize;
        String elemPropertyName = nestedPropertyName + "[" + elementPosition + "]";

        if (hasExpression(elem)) {
            String strValue = getStringValue(elem);

            expressionGraph.put(elemPropertyName, strValue);
            newList.add(i, null);
        } else {
            // process list value bean definition as a top level bean
            if ((elem instanceof BeanDefinition) || (elem instanceof BeanDefinitionHolder)) {
                String beanName = null;
                BeanDefinition beanDefinitionValue;
                if (elem instanceof BeanDefinition) {
                    beanDefinitionValue = (BeanDefinition) elem;
                } else {
                    beanDefinitionValue = ((BeanDefinitionHolder) elem).getBeanDefinition();
                    beanName = ((BeanDefinitionHolder) elem).getBeanName();
                }

                processBeanDefinition(beanName, beanDefinitionValue, beanFactory, processedBeanNames);
            }

            newList.add(i, elem);
        }
    }

    // determine if we need to clear any parent expressions for this list
    if (!isMergeEnabled) {
        // clear any expressions that match the property name minus index
        Map<String, String> adjustedParentExpressionGraph = new HashMap<String, String>();
        for (Map.Entry<String, String> parentExpression : parentExpressionGraph.entrySet()) {
            if (!parentExpression.getKey().startsWith(nestedPropertyName + "[")) {
                adjustedParentExpressionGraph.put(parentExpression.getKey(), parentExpression.getValue());
            }
        }

        parentExpressionGraph.clear();
        parentExpressionGraph.putAll(adjustedParentExpressionGraph);
    }

    listVal.clear();
    listVal.addAll(newList);
}

From source file:org.metaeffekt.dcc.commons.spring.xml.DCCConfigurationBeanDefinitionParser.java

private void parseAndRegisterUnits(Element profileElement, File origin, BeanDefinitionRegistry registry,
        ParserContext parserContext) {//from  ww w .j ava2 s . com

    List<Element> unitElements = DomUtils.getChildElementsByTagName(profileElement, ELEMENT_UNIT);
    for (Element unitElement : unitElements) {
        // first retrieve all XML attributes of the Unit element
        String unitId = extractMandatoryIdAttributeFromElement(unitElement, "Unit", registry);
        // TODO: currently not used as there is nowhere to set this on a ConfigurationUnit
        @SuppressWarnings("unused")
        String unitType = unitElement.getAttribute(ATTRIBUTE_UNIT_TYPE);
        boolean unitIsAbstract = Boolean.valueOf(unitElement.getAttribute(ATTRIBUTE_UNIT_ABSTRACT));
        String unitExtends = parseString(unitElement, ATTRIBUTE_UNIT_EXTENDS, null, registry);

        ManagedList<AbstractBeanDefinition> requiredCapabilityReferences = parseRequiredCapabilities(
                unitElement, unitId, registry);
        ManagedList<AbstractBeanDefinition> providedCapabilityReferences = parseProvidedCapabilities(
                unitElement, unitId, registry);
        ManagedList<Attribute> attributes = parseAttributesElement(unitElement, parserContext);
        ManagedList<AbstractBeanDefinition> mappings = parseMappingsElement(unitElement, unitId, registry);
        ManagedList<AbstractBeanDefinition> commands = parseCommands(unitElement, unitId, registry);
        ManagedList<AbstractBeanDefinition> asserts = parseAsserts(unitElement, registry);

        BeanDefinitionBuilder unitBeanDefBuilder = null;
        if (StringUtils.isNotBlank(unitExtends)) {
            unitBeanDefBuilder = BeanDefinitionBuilder.childBeanDefinition(unitExtends);
            unitBeanDefBuilder.getRawBeanDefinition().setBeanClass(ConfigurationUnit.class);
            requiredCapabilityReferences.setMergeEnabled(true);
            providedCapabilityReferences.setMergeEnabled(true);
            attributes.setMergeEnabled(true);
            mappings.setMergeEnabled(true);
            commands.setMergeEnabled(true);
            asserts.setMergeEnabled(true);

            unitBeanDefBuilder.addPropertyValue(PROPERTY_UNIT_PARENT_ID, unitExtends);
        } else {
            unitBeanDefBuilder = BeanDefinitionBuilder.rootBeanDefinition(ConfigurationUnit.class);
        }

        String description = extractDescription(unitElement, registry);

        unitBeanDefBuilder.setAbstract(false);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_ORIGIN, origin);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_DESCRIPTION, description);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_UNIT_ABSTRACT, unitIsAbstract);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_UNIT_PROVIDED_CAPABILITIES, providedCapabilityReferences);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_UNIT_REQUIRED_CAPABILITIES, requiredCapabilityReferences);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_UNIT_ATTRIBUTES, attributes);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_UNIT_MAPPINGS, mappings);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_UNIT_COMMANDS, commands);
        unitBeanDefBuilder.addPropertyValue(PROPERTY_UNIT_ASSERTS, asserts);

        unitBeanDefBuilder.addConstructorArgValue(unitId);
        RuntimeBeanReference unitBeanRef = new RuntimeBeanReference(unitId);
        unitBeanRef.setSource(parserContext.getReaderContext().extractSource(unitElement));

        registry.registerBeanDefinition(unitId, unitBeanDefBuilder.getBeanDefinition());
    }

}

From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java

/**
 * Parse a list element.//from   w  ww .j a  v  a2  s.c  o m
 */
public List<Object> parseListElement(Element collectionEle, @Nullable BeanDefinition bd) {
    String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
    NodeList nl = collectionEle.getChildNodes();
    ManagedList<Object> target = new ManagedList<>(nl.getLength());
    target.setSource(extractSource(collectionEle));
    target.setElementTypeName(defaultElementType);
    target.setMergeEnabled(parseMergeAttribute(collectionEle));
    parseCollectionElements(nl, target, bd, defaultElementType);
    return target;
}