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.security;
022    
023    import org.granite.spring.SpringGraniteConfig;
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.util.StringUtils;
029    import org.w3c.dom.Element;
030    
031    /**
032     * @author William Drai
033     */
034    public class SecurityServiceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
035    
036        @Override
037        protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
038            if (!SpringGraniteConfig.isSpringSecurity3Present())
039                    throw new RuntimeException("Cannot use graniteds:security-service with Spring 2.x");
040            
041            // Set the default ID if necessary
042            if (!StringUtils.hasText(element.getAttribute(ID_ATTRIBUTE)))
043                element.setAttribute(ID_ATTRIBUTE, "org.granite.spring.security.SpringSecurity3Service");
044            
045            builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
046            
047            String authenticationManager = element.getAttribute("authentication-manager");
048            if (authenticationManager != null && authenticationManager.trim().length() > 0)
049                    builder.addPropertyReference("authenticationManager", authenticationManager);
050            
051            boolean allowAnonymousAccess = Boolean.valueOf(element.hasAttribute("allow-anonymous-access"));
052                    builder.addPropertyValue("allowAnonymousAccess", allowAnonymousAccess);
053            
054            String securityContextRepository = element.getAttribute("security-context-repository");
055            if (securityContextRepository != null && securityContextRepository.trim().length() > 0)
056                    builder.addPropertyReference("securityContextRepository", securityContextRepository);
057            
058            String securityMetadataSource = element.getAttribute("security-metadata-source");
059            if (securityMetadataSource != null && securityMetadataSource.trim().length() > 0)
060                    builder.addPropertyReference("securityMetadataSource", securityMetadataSource);
061            
062            String securityInterceptor = element.getAttribute("security-interceptor");
063            if (securityInterceptor != null && securityInterceptor.trim().length() > 0)
064                    builder.addPropertyReference("securityInterceptor", securityInterceptor);
065        }
066    
067        @Override
068        protected String getBeanClassName(Element element) {
069            return "org.granite.spring.security.SpringSecurity3Service";
070        }
071    }