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 RemoteDestinationBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { 038 039 @Override 040 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 041 builder.setLazyInit(false); 042 043 String source = element.getAttribute("source"); 044 045 // Set the default ID if necessary 046 if (!StringUtils.hasText(element.getAttribute(ID_ATTRIBUTE)) && source != null) 047 element.setAttribute(ID_ATTRIBUTE, source + "_remoteDestination"); 048 049 mapOptionalAttributes(element, parserContext, builder, "id", "source"); 050 051 String securizerRef = element.getAttribute("securizer"); 052 if (StringUtils.hasText(securizerRef)) 053 builder.addPropertyReference("securizer", securizerRef); 054 055 Object sourceElement = parserContext.extractSource(element); 056 ManagedList<String> roles = new ManagedList<String>(); 057 roles.setSource(sourceElement); 058 List<Element> rolesElements = DomUtils.getChildElementsByTagName(element, "roles"); 059 for (Element rolesElement : rolesElements) { 060 List<Element> valueElements = DomUtils.getChildElementsByTagName(rolesElement, "role"); 061 for (Element valueElement : valueElements) 062 roles.add(valueElement.getTextContent()); 063 } 064 if (!roles.isEmpty()) 065 builder.addPropertyValue("roles", roles); 066 } 067 068 @Override 069 protected String getBeanClassName(Element element) { 070 return "org.granite.spring.RemoteDestination"; 071 } 072 073 static void mapOptionalAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs) { 074 for (String attr : attrs) { 075 String value = element.getAttribute(attr); 076 if (StringUtils.hasText(value)) { 077 String propertyName = Conventions.attributeNameToPropertyName(attr); 078 if (validateProperty(element, parserContext, propertyName, attr)) { 079 builder.addPropertyValue(propertyName, value); 080 } 081 } 082 } 083 } 084 085 private static boolean validateProperty(Element element, ParserContext parserContext, String propertyName, String attr) { 086 if (!StringUtils.hasText(propertyName)) { 087 parserContext.getReaderContext().error( 088 "Illegal property name trying to convert from attribute '" + attr + "' : cannot be null or empty.", 089 parserContext.extractSource(element)); 090 return false; 091 } 092 return true; 093 } 094}