All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.contributions.ordered.OrderedContributionBeanDefinitionParser Maven / Gradle / Ivy

Go to download

This project adds a so called contribution mechanism (like known from Tapestry IOC or Eclipse Plugins) for configuration and extension of services to the Spring project.

There is a newer version: 2.0.0
Show newest version
package org.springframework.contributions.ordered;

import static org.springframework.contributions.ContributionsNamespaceHandler.CONTRIBUTION_NAMESPACE;
import static org.springframework.contributions.ContributionsNamespaceHandler.ORDERED_CONTRIBUTION_PREFIX;

import java.util.List;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Parses contribution tags in Spring XML config.
 * @author Christian Köberl
 */
public class OrderedContributionBeanDefinitionParser implements BeanDefinitionParser
{
    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public BeanDefinition parse(final Element element, final ParserContext parserContext)
    {
        String contributionName = element.getAttribute("to");
        String beanName = ORDERED_CONTRIBUTION_PREFIX + contributionName;

        BeanDefinitionRegistry registry = parserContext.getRegistry();
        BeanDefinition beanDefinition;
        List contributionList;
        if (registry.containsBeanDefinition(beanName))
        {
            beanDefinition = registry.getBeanDefinition(beanName);
            contributionList = (List) beanDefinition.getPropertyValues().getPropertyValue("contributionList")
                .getValue();
        }
        else
        {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder
                .rootBeanDefinition(OrderedConfigurationFactoryBean.class);
            contributionList = new ManagedList();
            builder.addPropertyValue("contributionList", contributionList);
            beanDefinition = builder.getBeanDefinition();
            
            // FIX by ORT
            registry.registerBeanDefinition(beanName, beanDefinition);
        }

        NodeList entryList = element.getElementsByTagNameNS(CONTRIBUTION_NAMESPACE, "entry");
        for (int i = 0; i < entryList.getLength(); i++)
        {
            Element entry = (Element) entryList.item(i);
            String name = entry.getAttribute("name");
            String constraints = entry.getAttribute("constraints");
            Object beanValueOrReference = parserContext.getDelegate().parsePropertyValue(entry, null, name);
            contributionList.add(OrderContributionUtils.createContributionBeanDefinition(
                name,
                beanValueOrReference,
                constraints));
        }
        
        return beanDefinition;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy