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