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
021 package org.granite.spring;
022
023 import java.util.List;
024
025 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
026 import org.springframework.beans.factory.support.ManagedList;
027 import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
028 import org.springframework.beans.factory.xml.ParserContext;
029 import org.springframework.core.Conventions;
030 import org.springframework.util.StringUtils;
031 import org.springframework.util.xml.DomUtils;
032 import org.w3c.dom.Element;
033
034 /**
035 * @author William Drai
036 */
037 public class RemoteDestinationBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
038
039 @Override
040 @SuppressWarnings("unchecked")
041 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
042 builder.setLazyInit(false);
043
044 String source = element.getAttribute("source");
045
046 // Set the default ID if necessary
047 if (!StringUtils.hasText(element.getAttribute(ID_ATTRIBUTE)) && source != null)
048 element.setAttribute(ID_ATTRIBUTE, source + "_remoteDestination");
049
050 mapOptionalAttributes(element, parserContext, builder, "id", "source");
051
052 Object sourceElement = parserContext.extractSource(element);
053 ManagedList roles = new ManagedList();
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.RemoteDestination";
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 }