001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2009 ADEQUATE SYSTEMS SARL
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.io.IOException;
024    
025    import javax.servlet.ServletContext;
026    
027    import org.granite.config.AbstractFrameworkGraniteConfig;
028    import org.granite.messaging.service.security.SecurityService;
029    import org.granite.util.ClassUtil;
030    import org.springframework.beans.factory.InitializingBean;
031    import org.springframework.web.context.ServletContextAware;
032    import org.xml.sax.SAXException;
033    
034    
035    public class SpringGraniteConfig extends AbstractFrameworkGraniteConfig implements InitializingBean, ServletContextAware {
036    
037        private ServletContext servletContext = null;
038    
039        
040            // Determine Spring Security version
041        public static boolean isSpringSecurity3Present() {
042            try {
043                    SpringGraniteConfig.class.getClassLoader().loadClass("org.springframework.security.access.AccessDeniedException");
044                    return true;
045            }
046            catch (ClassNotFoundException e) {
047            }
048            return false;
049        }
050        public static boolean isSpringSecurity2Present() {
051            try {
052                    SpringGraniteConfig.class.getClassLoader().loadClass("org.springframework.security.context.SecurityContext");
053                    return true;
054            }
055            catch (ClassNotFoundException e) {
056            }
057            return false;
058        }
059        
060        public void afterPropertiesSet() throws IOException, SAXException {
061            init(servletContext, "org/granite/spring/granite-config-spring.xml");
062            
063            if (getGraniteConfig().getSecurityService() == null) {
064                    // Autodetect and configure Spring Security service
065                    // Load dynamically to avoid runtime dependency on SS
066                    try {
067                            if (isSpringSecurity3Present())
068                                    getGraniteConfig().setSecurityService(ClassUtil.newInstance("org.granite.spring.security.SpringSecurity3Service", SecurityService.class));
069                            else if (isSpringSecurity2Present())
070                                    getGraniteConfig().setSecurityService(ClassUtil.newInstance("org.granite.messaging.service.security.SpringSecurityService", SecurityService.class));
071                    }
072                    catch (Exception e) {
073                            throw new IOException("Could not configure Spring Security service");
074                    }
075            }
076        }
077        
078        public void setServletContext(ServletContext servletContext) {
079            this.servletContext = servletContext;
080        }
081    }