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 */ 022package org.granite.spring; 023 024import java.util.List; 025 026import org.springframework.beans.factory.support.BeanDefinitionBuilder; 027import org.springframework.beans.factory.support.ManagedList; 028import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; 029import org.springframework.beans.factory.xml.ParserContext; 030import org.springframework.core.Conventions; 031import org.springframework.util.StringUtils; 032import org.springframework.util.xml.DomUtils; 033import org.w3c.dom.Element; 034 035/** 036 * @author William Drai 037 */ 038public class RemoteDestinationBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { 039 040 @Override 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 String securizerRef = element.getAttribute("securizer"); 053 if (StringUtils.hasText(securizerRef)) 054 builder.addPropertyReference("securizer", securizerRef); 055 056 Object sourceElement = parserContext.extractSource(element); 057 ManagedList<String> roles = new ManagedList<String>(); 058 roles.setSource(sourceElement); 059 List<Element> rolesElements = DomUtils.getChildElementsByTagName(element, "roles"); 060 for (Element rolesElement : rolesElements) { 061 List<Element> valueElements = DomUtils.getChildElementsByTagName(rolesElement, "role"); 062 for (Element valueElement : valueElements) 063 roles.add(valueElement.getTextContent()); 064 } 065 if (!roles.isEmpty()) 066 builder.addPropertyValue("roles", roles); 067 } 068 069 @Override 070 protected String getBeanClassName(Element element) { 071 return "org.granite.spring.RemoteDestination"; 072 } 073 074 static void mapOptionalAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs) { 075 for (String attr : attrs) { 076 String value = element.getAttribute(attr); 077 if (StringUtils.hasText(value)) { 078 String propertyName = Conventions.attributeNameToPropertyName(attr); 079 if (validateProperty(element, parserContext, propertyName, attr)) { 080 builder.addPropertyValue(propertyName, value); 081 } 082 } 083 } 084 } 085 086 private static boolean validateProperty(Element element, ParserContext parserContext, String propertyName, String attr) { 087 if (!StringUtils.hasText(propertyName)) { 088 parserContext.getReaderContext().error( 089 "Illegal property name trying to convert from attribute '" + attr + "' : cannot be null or empty.", 090 parserContext.extractSource(element)); 091 return false; 092 } 093 return true; 094 } 095}