Example usage for org.springframework.beans.factory.config RuntimeBeanReference setSource

List of usage examples for org.springframework.beans.factory.config RuntimeBeanReference setSource

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config RuntimeBeanReference setSource.

Prototype

public void setSource(@Nullable Object source) 

Source Link

Document

Set the configuration source Object for this metadata element.

Usage

From source file:edu.internet2.middleware.shibboleth.common.config.SpringConfigurationUtils.java

/**
 * Parses a bean definition using an xsi:type aware version of
 * BeanDefinitionParserDelegate.parseCustomElement(Element).
 * //from w  w  w  .  j  a  v  a2  s . com
 * @param element element to parse
 * @param idAttribute attribute that carries the unique ID for the bean
 * @param parserContext current parser context
 * 
 * @return bean definition reference
 */
public static RuntimeBeanReference parseCustomElement(Element element, String idAttribute,
        ParserContext parserContext) {
    createBeanDefinition(element, parserContext);
    RuntimeBeanReference beanRef = new RuntimeBeanReference(element.getAttributeNS(null, idAttribute));
    beanRef.setSource(element);
    return beanRef;
}

From source file:com.bstek.dorado.spring.MapEntryShortCutDecorator.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();

    ManagedMap map = null;//from w  w  w .j  ava 2 s.  c om
    boolean firstPropertyValue = propertyValues.getPropertyValue(property) == null;

    if (!firstPropertyValue) {
        map = (ManagedMap) (propertyValues.getPropertyValue(property).getValue());
    } else {
        map = new ManagedMap();
        map.setSource(node);
        map.setMergeEnabled(true);
        propertyValues.addPropertyValue(property, map);
        beanDef.setPropertyValues(propertyValues);
    }

    Element el = (Element) node;
    String key = el.getAttribute("key");
    String value = el.getAttribute("value");
    String valueRef = el.getAttribute("value-ref");

    Object entryValue = null;
    if (StringUtils.isNotEmpty(value)) {
        entryValue = value;
    } else if (StringUtils.isNotEmpty(valueRef)) {
        RuntimeBeanReference ref = new RuntimeBeanReference(valueRef);
        ref.setSource(parserContext.getReaderContext().extractSource(el));
        entryValue = ref;
    } else {
        Element beanEl = DomUtils.getChildElementByTagName(el, "bean");
        if (beanEl != null) {
            entryValue = parserContext.getDelegate().parseBeanDefinitionElement(beanEl);
        }
    }

    if (supportsMultiKey && StringUtils.isNotEmpty(key)) {
        for (String k : StringUtils.split(key, KEY_DELIM)) {
            map.put(k, entryValue);
        }
    } else {
        map.put(key, entryValue);
    }
    return definition;
}

From source file:de.itsvs.cwtrpc.controller.config.RemoteServiceControllerConfigBeanDefinitionParser.java

protected ManagedList<BeanMetadataElement> parseModules(Element element, ParserContext parserContext) {
    final ManagedList<BeanMetadataElement> modules;

    modules = new ManagedList<BeanMetadataElement>();
    for (Element child : DomUtils.getChildElementsByTagName(element, XmlNames.MODULE_ELEMENT)) {
        modules.add(getServiceModuleConfigBeanDefinitionParser().parseNested(child, parserContext));
    }/*from ww  w .j  a  va 2s.co m*/
    for (Element child : DomUtils.getChildElementsByTagName(element, XmlNames.MODULE_REF_ELEMENT)) {
        final String name;
        final RuntimeBeanReference beanReference;

        name = child.getAttribute(XmlNames.BEAN_REFERENCE_NAME_ATTR);
        if (!StringUtils.hasText(name)) {
            parserContext.getReaderContext().error("Module reference must not be empty",
                    parserContext.extractSource(element));
        }
        beanReference = new RuntimeBeanReference(name);
        beanReference.setSource(parserContext.extractSource(element));
        modules.add(beanReference);
    }

    return modules;
}

From source file:de.itsvs.cwtrpc.controller.config.RemoteServiceGroupConfigBeanDefinitionParser.java

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    final ManagedList<BeanMetadataElement> serviceConfigs;
    final ManagedList<BeanMetadataElement> childGroupConfigs;

    getBaseServiceConfigParser().update(element, parserContext, builder);

    serviceConfigs = new ManagedList<BeanMetadataElement>();
    for (Element child : DomUtils.getChildElementsByTagName(element, XmlNames.SERVICE_ELEMENT)) {
        serviceConfigs.add(createServiceConfigBeanDefinition(child, parserContext));
    }/*from w  w  w  .  ja  va 2 s.co  m*/
    childGroupConfigs = new ManagedList<BeanMetadataElement>();
    for (Element child : DomUtils.getChildElementsByTagName(element, XmlNames.SERVICE_GROUP_ELEMENT)) {
        childGroupConfigs.add(parseNested(child, parserContext));
    }
    for (Element child : DomUtils.getChildElementsByTagName(element, XmlNames.SERVICE_GROUP_REF_ELEMENT)) {
        final String name;
        final RuntimeBeanReference beanReference;

        name = child.getAttribute(XmlNames.BEAN_REFERENCE_NAME_ATTR);
        if (!StringUtils.hasText(name)) {
            parserContext.getReaderContext().error("Module reference must not be empty",
                    parserContext.extractSource(element));
        }
        beanReference = new RuntimeBeanReference(name);
        beanReference.setSource(parserContext.extractSource(element));
        childGroupConfigs.add(beanReference);
    }
    for (Element child : DomUtils.getChildElementsByTagName(element,
            XmlNames.AUTOWIRED_SERVICE_GROUP_ELEMENT)) {
        childGroupConfigs
                .add(getAutowiredServiceGroupConfigBeanDefinitionParser().parseNested(child, parserContext));
    }

    builder.addConstructorArgValue(serviceConfigs);
    builder.addConstructorArgValue(childGroupConfigs);
}

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

private Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {
    String elementName = (propertyName != null) ? "<property> element for property '" + propertyName + "'"
            : "<constructor-arg> element";

    // Should only have one child element: ref, value, list, etc.
    NodeList nl = ele.getChildNodes();
    Element subElement = null;/*from   w  ww.  j  a v  a 2 s. c om*/
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element
                && !DomUtils.nodeNameEquals(node, BeanDefinitionParserDelegate.DESCRIPTION_ELEMENT)) {
            // Child element is what we're looking for.
            if (subElement != null) {
                error(elementName + " must not contain more than one sub-element", ele);
            } else {
                subElement = (Element) node;
            }
        }
    }

    boolean hasRefAttribute = ele.hasAttribute(BeanDefinitionParserDelegate.REF_ATTRIBUTE);
    boolean hasValueAttribute = ele.hasAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE);
    if ((hasRefAttribute && hasValueAttribute)
            || ((hasRefAttribute || hasValueAttribute) && subElement != null)) {
        error(elementName
                + " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element",
                ele);
    }

    if (hasRefAttribute) {
        String refName = ele.getAttribute(BeanDefinitionParserDelegate.REF_ATTRIBUTE);
        if (!StringUtils.hasText(refName)) {
            error(elementName + " contains empty 'ref' attribute", ele);
        }
        RuntimeBeanReference ref = new RuntimeBeanReference(refName);
        ref.setSource(parserContext.extractSource(ele));
        return ref;
    } else if (hasValueAttribute) {
        TypedStringValue valueHolder = new TypedStringValue(
                ele.getAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE));
        valueHolder.setSource(parserContext.extractSource(ele));
        return valueHolder;
    } else if (subElement != null) {
        return parsePropertySubElement(subElement, bd, null);
    } else {
        // Neither child element nor "ref" or "value" attribute found.
        error(elementName + " must specify a ref or value", ele);
        return null;
    }
}

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

private Object parseRefElement(Element ele) {
    // A generic reference to any name of any component.
    String refName = ele.getAttribute(COMPONENT_ID_ATTR);
    if (!StringUtils.hasLength(refName)) {
        error("'" + COMPONENT_ID_ATTR + "' is required for <ref> element", ele);
        return null;
    }/*w  w  w.j a v  a2 s  . c  o m*/

    if (!StringUtils.hasText(refName)) {
        error("<ref> element contains empty target attribute", ele);
        return null;
    }
    RuntimeBeanReference ref = new InstanceEqualityRuntimeBeanReference(refName);
    ref.setSource(parserContext.extractSource(ele));
    return ref;
}

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

/**
 * Parse a map element.//from   w  w w  .  j  a  v  a  2s  .c o  m
 */
public Map<?, ?> parseMapElement(Element mapEle, BeanDefinition bd) {
    String defaultKeyType = mapEle.getAttribute(BeanDefinitionParserDelegate.KEY_TYPE_ATTRIBUTE);
    String defaultValueType = mapEle.getAttribute(BeanDefinitionParserDelegate.VALUE_TYPE_ATTRIBUTE);

    List<Element> entryEles = DomUtils.getChildElementsByTagName(mapEle,
            BeanDefinitionParserDelegate.ENTRY_ELEMENT);
    ManagedMap<Object, Object> map = new ManagedMap<Object, Object>(entryEles.size());
    map.setSource(extractSource(mapEle));
    map.setKeyTypeName(defaultKeyType);
    map.setValueTypeName(defaultValueType);
    map.setMergeEnabled(parseMergeAttribute(mapEle));

    for (Element entryEle : entryEles) {
        // Should only have one value child element: ref, value, list, etc.
        // Optionally, there might be a key child element.
        NodeList entrySubNodes = entryEle.getChildNodes();
        Element keyEle = null;
        Element valueEle = null;
        for (int j = 0; j < entrySubNodes.getLength(); j++) {
            Node node = entrySubNodes.item(j);
            if (node instanceof Element) {
                Element candidateEle = (Element) node;
                if (DomUtils.nodeNameEquals(candidateEle, BeanDefinitionParserDelegate.KEY_ELEMENT)) {
                    if (keyEle != null) {
                        error("<entry> element is only allowed to contain one <key> sub-element", entryEle);
                    } else {
                        keyEle = candidateEle;
                    }
                } else {
                    // Child element is what we're looking for.
                    if (valueEle != null) {
                        error("<entry> element must not contain more than one value sub-element", entryEle);
                    } else {
                        valueEle = candidateEle;
                    }
                }
            }
        }

        // Extract key from attribute or sub-element.
        Object key = null;
        boolean hasKeyAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE);
        boolean hasKeyRefAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.KEY_REF_ATTRIBUTE);
        if ((hasKeyAttribute && hasKeyRefAttribute)
                || ((hasKeyAttribute || hasKeyRefAttribute)) && keyEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element", entryEle);
        }
        if (hasKeyAttribute) {
            key = buildTypedStringValueForMap(entryEle.getAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE),
                    defaultKeyType, entryEle);
        } else if (hasKeyRefAttribute) {
            String refName = entryEle.getAttribute(BeanDefinitionParserDelegate.KEY_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'key-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            key = ref;
        } else if (keyEle != null) {
            key = parseKeyElement(keyEle, bd, defaultKeyType);
        } else {
            error("<entry> element must specify a key", entryEle);
        }

        // Extract value from attribute or sub-element.
        Object value = null;
        boolean hasValueAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE);
        boolean hasValueRefAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.VALUE_REF_ATTRIBUTE);
        if ((hasValueAttribute && hasValueRefAttribute)
                || ((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
        }
        if (hasValueAttribute) {
            value = buildTypedStringValueForMap(
                    entryEle.getAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE), defaultValueType,
                    entryEle);
        } else if (hasValueRefAttribute) {
            String refName = entryEle.getAttribute(BeanDefinitionParserDelegate.VALUE_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'value-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            value = ref;
        } else if (valueEle != null) {
            value = parsePropertySubElement(valueEle, bd, defaultValueType);
        } else {
            error("<entry> element must specify a value", entryEle);
        }

        // Add final key and value to the Map.
        map.put(key, value);
    }

    return map;
}

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

private void parseAndRegisterUnits(Element profileElement, File origin, BeanDefinitionRegistry registry,
        ParserContext parserContext) {//from w ww . j av a2  s  .  co m

    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.metaeffekt.dcc.commons.spring.xml.DCCConfigurationBeanDefinitionParser.java

private void parseAndRegisterBindings(Element profileElement, File origin, BeanDefinitionRegistry registry,
        ParserContext parserContext) {//from   w w  w  .  j  av a2 s.c o m

    List<Element> bindingElements = DomUtils.getChildElementsByTagName(profileElement, ELEMENT_BINDING);
    for (Element bindingElement : bindingElements) {
        Element sourceElement = DomUtils.getChildElementByTagName(bindingElement, ELEMENT_BINDING_SOURCE);
        Element targetElement = DomUtils.getChildElementByTagName(bindingElement, ELEMENT_BINDING_TARGET);

        String sourceUnitRef = parseString(sourceElement, "unitRef", null, registry);
        String sourceCapabilityId = parseString(sourceElement, "capabilityId", null, registry);
        String targetUnitRef = parseString(targetElement, "unitRef", null, registry);
        String targetCapabilityId = parseString(targetElement, "capabilityId", null, registry);

        // convention: if target capabilityId is omitted then use the source capability id
        if (StringUtils.isEmpty(targetCapabilityId)) {
            targetCapabilityId = sourceCapabilityId;
        }

        String description = extractDescription(bindingElement, registry);

        BeanDefinitionBuilder bindingBeanBuilder = BeanDefinitionBuilder
                .genericBeanDefinition(BindingFactoryBean.class);
        bindingBeanBuilder.addPropertyValue(PROPERTY_ORIGIN, origin);
        bindingBeanBuilder.addPropertyValue(PROPERTY_DESCRIPTION, description);
        bindingBeanBuilder.addPropertyValue("sourceCapabilityId", Id.createCapabilityId(sourceCapabilityId));
        bindingBeanBuilder.addPropertyReference("sourceUnit", sourceUnitRef);
        bindingBeanBuilder.addPropertyValue("targetCapabilityId", Id.createCapabilityId(targetCapabilityId));
        bindingBeanBuilder.addPropertyReference("targetUnit", targetUnitRef);

        String bindingBeanName = parserContext.getReaderContext()
                .generateBeanName(bindingBeanBuilder.getBeanDefinition());
        registry.registerBeanDefinition(bindingBeanName, bindingBeanBuilder.getBeanDefinition());

        RuntimeBeanReference beanRef = new RuntimeBeanReference(bindingBeanName);
        beanRef.setSource(parserContext.extractSource(bindingElement));

    }
}

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

/**
 * Get the value of a property element. May be a list etc.
 * Also used for constructor arguments, "propertyName" being null in this case.
 *///  w  ww.  j a  v a2s .c  om
@Nullable
public Object parsePropertyValue(Element ele, BeanDefinition bd, @Nullable String propertyName) {
    String elementName = (propertyName != null) ? "<property> element for property '" + propertyName + "'"
            : "<constructor-arg> element";

    // Should only have one child element: ref, value, list, etc.
    NodeList nl = ele.getChildNodes();
    Element subElement = null;
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT)
                && !nodeNameEquals(node, META_ELEMENT)) {
            // Child element is what we're looking for.
            if (subElement != null) {
                error(elementName + " must not contain more than one sub-element", ele);
            } else {
                subElement = (Element) node;
            }
        }
    }

    boolean hasRefAttribute = ele.hasAttribute(REF_ATTRIBUTE);
    boolean hasValueAttribute = ele.hasAttribute(VALUE_ATTRIBUTE);
    if ((hasRefAttribute && hasValueAttribute)
            || ((hasRefAttribute || hasValueAttribute) && subElement != null)) {
        error(elementName
                + " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element",
                ele);
    }

    if (hasRefAttribute) {
        String refName = ele.getAttribute(REF_ATTRIBUTE);
        if (!StringUtils.hasText(refName)) {
            error(elementName + " contains empty 'ref' attribute", ele);
        }
        RuntimeBeanReference ref = new RuntimeBeanReference(refName);
        ref.setSource(extractSource(ele));
        return ref;
    } else if (hasValueAttribute) {
        TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE));
        valueHolder.setSource(extractSource(ele));
        return valueHolder;
    } else if (subElement != null) {
        return parsePropertySubElement(subElement, bd);
    } else {
        // Neither child element nor "ref" or "value" attribute found.
        error(elementName + " must specify a ref or value", ele);
        return null;
    }
}