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