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

org.springframework.contributions.mapped.MappedContributionBeanDefinitionParser 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.mapped;

import static org.springframework.contributions.ContributionsNamespaceHandler.MAPPED_CONTRIBUTION_PREFIX;

import java.util.Map;

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

/**
 * Parser for mapped contribution bean definitions.
 *
 * @author Christian Köberl
 */
public class MappedContributionBeanDefinitionParser implements BeanDefinitionParser
{
    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("rawtypes")
    public BeanDefinition parse(Element element, ParserContext parserContext)
    {
        String contributionName = element.getAttribute("to");
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(MapFactoryBean.class);

        element.setAttribute("merge", "true");
        Map parsedMap = parserContext.getDelegate().parseMapElement(element, builder.getRawBeanDefinition());

        final String beanName = MAPPED_CONTRIBUTION_PREFIX + contributionName;
        BeanDefinition beanDefinition;
        BeanDefinitionRegistry registry = parserContext.getRegistry();
        if (registry.containsBeanDefinition(beanName))
        {
            beanDefinition = registry.getBeanDefinition(beanName);
            beanDefinition.getPropertyValues().addPropertyValue("sourceMap", parsedMap);

        }
        else
        {
            builder.addPropertyValue("sourceMap", parsedMap);
            beanDefinition = builder.getBeanDefinition();
            registry.registerBeanDefinition(beanName, beanDefinition);
        }

        return beanDefinition;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy