1 4 package com.opensymphony.webwork.spring.lifecycle; 5 6 import org.springframework.web.context.support.WebApplicationContextUtils; 7 import org.springframework.web.context.support.XmlWebApplicationContext; 8 import org.springframework.web.context.WebApplicationContext; 9 import org.springframework.web.context.ConfigurableWebApplicationContext; 10 import org.springframework.beans.BeanUtils; 11 import org.springframework.util.StringUtils; 12 import org.springframework.context.ApplicationContextException; 13 14 import javax.servlet.http.HttpSessionListener ; 15 import javax.servlet.http.HttpSessionEvent ; 16 import javax.servlet.http.HttpSession ; 17 import javax.servlet.ServletContext ; 18 import java.io.Serializable ; 19 20 26 public class ApplicationContextSessionListener implements HttpSessionListener , Serializable { 27 28 32 public static final String SESSION_CONTEXT_CLASS_PARAM = "sessionContextClass"; 33 34 38 public static final Class DEFAULT_SESSION_CONTEXT_CLASS = XmlWebApplicationContext.class; 39 40 46 public static final String SESSION_CONFIG_LOCATION_PARAM = "sessionContextConfigLocation"; 47 48 51 public static final String APP_CONTEXT_SESSION_KEY = "com.opensymphony.webwork.spring.ApplicationContextSessionListener_APP_CONTEXT"; 52 53 56 public static final String [] DEFAULT_SESSION_CONFIG = {"classpath:session.xml"}; 57 58 61 private ConfigurableWebApplicationContext sessionContext; 62 63 public void sessionCreated(HttpSessionEvent httpSessionEvent) { 64 final HttpSession session = httpSessionEvent.getSession(); 65 final ServletContext servletContext = session.getServletContext(); 66 final WebApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 67 68 String contextClassName = servletContext.getInitParameter(SESSION_CONTEXT_CLASS_PARAM); 69 Class contextClass = DEFAULT_SESSION_CONTEXT_CLASS; 70 if (contextClassName != null) { 71 try { 72 contextClass = Class.forName(contextClassName, true, Thread.currentThread().getContextClassLoader()); 73 } 74 catch (ClassNotFoundException ex) { 75 throw new ApplicationContextException("Failed to load context class [" + contextClassName + "]", ex); 76 } 77 if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { 78 throw new ApplicationContextException("Custom context class [" + contextClassName + 79 "] is not of type ConfigurableWebApplicationContext"); 80 } 81 } 82 83 sessionContext = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); 84 sessionContext.setParent(appContext); 85 sessionContext.setServletContext(servletContext); 86 String configLocation = servletContext.getInitParameter(SESSION_CONFIG_LOCATION_PARAM); 87 if (configLocation != null) { 88 sessionContext.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation, 89 ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS)); 90 } else { 91 sessionContext.setConfigLocations(DEFAULT_SESSION_CONFIG); 92 } 93 94 sessionContext.refresh(); 95 session.setAttribute(APP_CONTEXT_SESSION_KEY, sessionContext); 96 } 97 98 public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { 99 sessionContext.close(); 100 this.sessionContext = null; 101 } 102 } 103 | Popular Tags |