1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.ui.session;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import org.springframework.context.ApplicationContext;
22
23 import org.springframework.web.context.support.WebApplicationContextUtils;
24 import org.springframework.util.Assert;
25
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28 import javax.servlet.http.HttpSessionEvent;
29 import javax.servlet.http.HttpSessionListener;
30
31
32 /***
33 * Declared in web.xml as <br>
34 * <code> <listener><br>
35 * <listener-class>org.acegisecurity.ui.session.HttpSessionEventPublisher</listener-class><br>
36 * </listener><br>
37 * </code> Publishes <code>HttpSessionApplicationEvent</code>s to the Spring
38 * Root WebApplicationContext. <br>
39 * Maps javax.servlet.http.HttpSessionListener.sessionCreated() to {@link
40 * HttpSessionCreatedEvent}. <br>
41 * Maps javax.servlet.http.HttpSessionListener.sessionDestroyed() to {@link
42 * HttpSessionDestroyedEvent}. <br>
43 *
44 * @author Ray Krueger
45 */
46 public class HttpSessionEventPublisher implements HttpSessionListener,
47 ServletContextListener {
48
49
50 private static final Log log = LogFactory.getLog(HttpSessionEventPublisher.class);
51
52
53
54 private ApplicationContext context;
55
56
57
58 /***
59 * Not implemented
60 *
61 * @param event
62 */
63 public void contextDestroyed(ServletContextEvent event) {}
64
65 /***
66 * Handled internally by a call to {@link
67 * org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext(javax.servlet.ServletContext)}
68 *
69 * @param event the ServletContextEvent passed in by the container,
70 * event.getServletContext() will be used to get the
71 * WebApplicationContext
72 */
73 public void contextInitialized(ServletContextEvent event) {
74 if (log.isDebugEnabled())
75 log.debug("Received ServletContextEvent: " + event);
76 setContext(WebApplicationContextUtils.getRequiredWebApplicationContext(
77 event.getServletContext()));
78 }
79
80 /***
81 * Handles the HttpSessionEvent by publishing a {@link
82 * HttpSessionCreatedEvent} to the application context.
83 *
84 * @param event HttpSessionEvent passed in by the container
85 */
86 public void sessionCreated(HttpSessionEvent event) {
87 HttpSessionCreatedEvent e = new HttpSessionCreatedEvent(event
88 .getSession());
89
90 log.debug("Publishing event: " + e);
91
92 getContext().publishEvent(e);
93 }
94
95 /***
96 * Handles the HttpSessionEvent by publishing a {@link
97 * HttpSessionDestroyedEvent} to the application context.
98 *
99 * @param event The HttpSessionEvent pass in by the container
100 */
101 public void sessionDestroyed(HttpSessionEvent event) {
102 HttpSessionDestroyedEvent e = new HttpSessionDestroyedEvent(event
103 .getSession());
104
105 log.debug("Publishing event: " + e);
106
107 getContext().publishEvent(e);
108 }
109
110 /***
111 * Package level method for testing and internal usage
112 *
113 * @param context The ApplicationContext this class will use to publish
114 * events
115 */
116 void setContext(ApplicationContext context) {
117 this.context = context;
118 log.debug("Using context: " + context);
119 }
120
121 ApplicationContext getContext() {
122 Assert.notNull(context, "setContext(...) never called, ApplicationContext must not be null");
123 return context;
124 }
125 }