001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.spring;
022
023import java.util.List;
024
025import org.springframework.beans.factory.support.BeanDefinitionBuilder;
026import org.springframework.beans.factory.support.ManagedList;
027import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
028import org.springframework.beans.factory.xml.ParserContext;
029import org.springframework.core.Conventions;
030import org.springframework.util.StringUtils;
031import org.springframework.util.xml.DomUtils;
032import org.w3c.dom.Element;
033
034/**
035 * @author William Drai
036 */
037public class MessagingDestinationBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
038
039    @Override
040    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
041        builder.setLazyInit(false);
042        
043        mapOptionalAttributes(element, parserContext, builder, "id", "no-local", "session-selector");
044        
045        String securizerRef = element.getAttribute("securizer");
046        if (StringUtils.hasText(securizerRef))
047                builder.addPropertyReference("securizer", securizerRef);
048        
049        Object sourceElement = parserContext.extractSource(element);
050        ManagedList<String> roles = new ManagedList<String>();
051        roles.setSource(sourceElement);
052        List<Element> rolesElements = DomUtils.getChildElementsByTagName(element, "roles");
053        for (Element rolesElement : rolesElements) {
054            List<Element> valueElements = DomUtils.getChildElementsByTagName(rolesElement, "role");
055            for (Element valueElement : valueElements)
056                roles.add(valueElement.getTextContent());
057        }
058        if (!roles.isEmpty())
059                builder.addPropertyValue("roles", roles);        
060    }
061
062    @Override
063    protected String getBeanClassName(Element element) {
064        return "org.granite.spring.MessagingDestination";
065    }
066
067    static void mapOptionalAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs) {
068        for (String attr : attrs) {
069            String value = element.getAttribute(attr);
070            if (StringUtils.hasText(value)) {
071                String propertyName = Conventions.attributeNameToPropertyName(attr);
072                if (validateProperty(element, parserContext, propertyName, attr)) {
073                    builder.addPropertyValue(propertyName, value);
074                }
075            }
076        }
077    }
078
079    private static boolean validateProperty(Element element, ParserContext parserContext, String propertyName, String attr) {
080        if (!StringUtils.hasText(propertyName)) {
081            parserContext.getReaderContext().error(
082                "Illegal property name trying to convert from attribute '" + attr + "' : cannot be null or empty.",
083                parserContext.extractSource(element));
084            return false;
085        }
086        return true;
087    }
088}