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;
022    
023    import java.io.IOException;
024    import java.util.Map;
025    import java.util.Map.Entry;
026    
027    import javax.servlet.ServletContext;
028    
029    import org.granite.config.AbstractFrameworkGraniteConfig;
030    import org.granite.logging.Logger;
031    import org.granite.messaging.service.security.SecurityService;
032    import org.granite.util.ClassUtil;
033    import org.springframework.beans.factory.InitializingBean;
034    import org.springframework.context.ApplicationContext;
035    import org.springframework.context.ApplicationContextAware;
036    import org.springframework.web.context.ServletContextAware;
037    import org.xml.sax.SAXException;
038    
039    
040    public class SpringGraniteConfig extends AbstractFrameworkGraniteConfig implements InitializingBean, ServletContextAware, ApplicationContextAware {
041            
042            private static final Logger log = Logger.getLogger(SpringGraniteConfig.class);
043    
044        private ServletContext servletContext = null;
045        private ApplicationContext applicationContext = null;
046    
047        
048            // Determine Spring Security version
049        public static boolean isSpringSecurity3Present() {
050            try {
051                    SpringGraniteConfig.class.getClassLoader().loadClass("org.springframework.security.access.AccessDeniedException");
052                    return true;
053            }
054            catch (ClassNotFoundException e) {
055            }
056            return false;
057        }
058        public static boolean isSpringSecurity2Present() {
059            try {
060                    SpringGraniteConfig.class.getClassLoader().loadClass("org.springframework.security.context.SecurityContext");
061                    return true;
062            }
063            catch (ClassNotFoundException e) {
064            }
065            return false;
066        }
067        
068        public void afterPropertiesSet() throws IOException, SAXException {
069            init(servletContext, "org/granite/spring/granite-config-spring.xml");
070            
071            if (getGraniteConfig().getSecurityService() == null) {
072                    @SuppressWarnings("unchecked")
073                    Map<String, Object> servicesMap = applicationContext.getBeansOfType(SecurityService.class);
074                    if (servicesMap.size() == 1) {
075                            Entry<String, Object> se = servicesMap.entrySet().iterator().next();
076                            log.info("Found security service %s in Spring context", se.getKey());
077                            getGraniteConfig().setSecurityService((SecurityService)se.getValue());
078                    }
079                    else {
080                            if (servicesMap.size() > 1)
081                                    log.warn("Multiple security services found in Spring context, will use default config");
082                            
083                            // Autodetect and configure Spring Security service
084                            // Load dynamically to avoid runtime dependency on SS
085                            try {
086                                    if (isSpringSecurity3Present())
087                                            getGraniteConfig().setSecurityService(ClassUtil.newInstance("org.granite.spring.security.SpringSecurity3Service", SecurityService.class));
088                                    else if (isSpringSecurity2Present())
089                                            getGraniteConfig().setSecurityService(ClassUtil.newInstance("org.granite.messaging.service.security.SpringSecurityService", SecurityService.class));
090                            }
091                            catch (Exception e) {
092                                    throw new IOException("Could not configure Spring Security service");
093                            }
094                    }
095            }
096        }
097        
098        public void setServletContext(ServletContext servletContext) {
099            this.servletContext = servletContext;
100        }
101        
102        public void setApplicationContext(ApplicationContext applicationContext) {
103            this.applicationContext = applicationContext;
104        }
105    }