KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > spring > lifecycle > ApplicationContextSessionListener


1 /*
2  * Copyright (c) 2005 Opensymphony. All Rights Reserved.
3  */

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 JavaDoc;
15 import javax.servlet.http.HttpSessionEvent JavaDoc;
16 import javax.servlet.http.HttpSession JavaDoc;
17 import javax.servlet.ServletContext JavaDoc;
18 import java.io.Serializable JavaDoc;
19
20 /**
21  * Used to set up a Spring {@link org.springframework.context.ApplicationContext}
22  * in the HttpSession. It can be configured similarly to the
23  *
24  * @author Jason Carreira <jcarreira@eplus.com>
25  */

26 public class ApplicationContextSessionListener implements HttpSessionListener JavaDoc, Serializable JavaDoc {
27
28     /**
29      * Config param for the root WebApplicationContext implementation class to
30      * use: "sessionContextClass"
31      */

32     public static final String JavaDoc SESSION_CONTEXT_CLASS_PARAM = "sessionContextClass";
33
34     /**
35      * Default context class for ContextLoader.
36      * @see org.springframework.web.context.support.XmlWebApplicationContext
37      */

38     public static final Class JavaDoc DEFAULT_SESSION_CONTEXT_CLASS = XmlWebApplicationContext.class;
39
40     /**
41      * Name of servlet context parameter that can specify the config location
42      * for the root context, falling back to the implementation's default
43      * otherwise.
44      * @see org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION
45      */

46     public static final String JavaDoc SESSION_CONFIG_LOCATION_PARAM = "sessionContextConfigLocation";
47
48     /**
49      * Key to map the session-scoped application context into the session attributes.
50      */

51     public static final String JavaDoc APP_CONTEXT_SESSION_KEY = "com.opensymphony.webwork.spring.ApplicationContextSessionListener_APP_CONTEXT";
52
53     /**
54      * The default session context configuration string:"classpath:session.xml"
55      */

56     public static final String JavaDoc[] DEFAULT_SESSION_CONFIG = {"classpath:session.xml"};
57
58     /**
59      * The application context instance created when the session is created, to be cleaned up when the session is destroyed
60      */

61     private ConfigurableWebApplicationContext sessionContext;
62
63     public void sessionCreated(HttpSessionEvent JavaDoc httpSessionEvent) {
64         final HttpSession JavaDoc session = httpSessionEvent.getSession();
65         final ServletContext JavaDoc servletContext = session.getServletContext();
66         final WebApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
67
68         String JavaDoc contextClassName = servletContext.getInitParameter(SESSION_CONTEXT_CLASS_PARAM);
69         Class JavaDoc contextClass = DEFAULT_SESSION_CONTEXT_CLASS;
70         if (contextClassName != null) {
71             try {
72                 contextClass = Class.forName(contextClassName, true, Thread.currentThread().getContextClassLoader());
73             }
74             catch (ClassNotFoundException JavaDoc 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 JavaDoc 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 JavaDoc httpSessionEvent) {
99         sessionContext.close();
100         this.sessionContext = null;
101     }
102 }
103
Popular Tags