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.tide.spring;
023    
024    import org.springframework.beans.factory.config.BeanDefinition;
025    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
026    import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
027    import org.springframework.beans.factory.xml.ParserContext;
028    import org.springframework.core.Conventions;
029    import org.springframework.util.StringUtils;
030    import org.w3c.dom.Element;
031    
032    /**
033     * @author William Drai
034     */
035    public class TideIdentityBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
036    
037        @Override
038        protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
039            
040            element.setAttribute(ID_ATTRIBUTE, "identity");
041            
042            builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
043            
044            String aclService = element.getAttribute("acl-service");
045            if (aclService != null && aclService.trim().length() > 0)
046                    builder.addPropertyReference("aclService", aclService);
047            
048            String sidRetrievalStrategy = element.getAttribute("sid-retrieval-strategy");
049            if (sidRetrievalStrategy != null && sidRetrievalStrategy.trim().length() > 0)
050                    builder.addPropertyReference("sidRetrievalStrategy", sidRetrievalStrategy);
051            
052            String objectIdentityRetrievalStrategy = element.getAttribute("object-identity-retrieval-strategy");
053            if (objectIdentityRetrievalStrategy != null && objectIdentityRetrievalStrategy.trim().length() > 0)
054                    builder.addPropertyReference("objectIdentityRetrievalStrategy", objectIdentityRetrievalStrategy);
055        }
056    
057        @Override
058        protected String getBeanClassName(Element element) {
059            String aclService = element.getAttribute("acl-service");
060            return "org.granite.tide.spring.security." 
061                    + (aclService != null && aclService.trim().length() > 0 ? "AclIdentity" : "Identity");
062        }
063    
064        static void mapOptionalAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs) {
065            for (String attr : attrs) {
066                String value = element.getAttribute(attr);
067                if (StringUtils.hasText(value)) {
068                    String propertyName = Conventions.attributeNameToPropertyName(attr);
069                    if (validateProperty(element, parserContext, propertyName, attr)) {
070                        builder.addPropertyValue(propertyName, value);
071                    }
072                }
073            }
074        }
075    
076        private static boolean validateProperty(Element element, ParserContext parserContext, String propertyName, String attr) {
077            if (!StringUtils.hasText(propertyName)) {
078                parserContext.getReaderContext().error(
079                    "Illegal property name trying to convert from attribute '" + attr + "' : cannot be null or empty.",
080                    parserContext.extractSource(element));
081                return false;
082            }
083            return true;
084        }
085    }