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