01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
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:
25: import javax.servlet.ServletContext;
26: import javax.servlet.http.HttpSessionEvent;
27: import javax.servlet.http.HttpSessionListener;
28:
29: /**
30: * Declared in web.xml as
31: * <pre>
32: * <listener>
33: * <listener-class>org.acegisecurity.ui.session.HttpSessionEventPublisher</listener-class>
34: * </listener>
35: * </pre>
36: *
37: * Publishes <code>HttpSessionApplicationEvent</code>s to the Spring Root WebApplicationContext. Maps
38: * javax.servlet.http.HttpSessionListener.sessionCreated() to {@link HttpSessionCreatedEvent}. Maps
39: * javax.servlet.http.HttpSessionListener.sessionDestroyed() to {@link HttpSessionDestroyedEvent}.
40: *
41: * @author Ray Krueger
42: */
43: public class HttpSessionEventPublisher implements HttpSessionListener {
44: //~ Static fields/initializers =====================================================================================
45:
46: private static final Log log = LogFactory
47: .getLog(HttpSessionEventPublisher.class);
48:
49: //~ Instance fields ================================================================================================
50:
51: //~ Methods ========================================================================================================
52:
53: ApplicationContext getContext(ServletContext servletContext) {
54: return WebApplicationContextUtils
55: .getWebApplicationContext(servletContext);
56: }
57:
58: /**
59: * Handles the HttpSessionEvent by publishing a {@link HttpSessionCreatedEvent} to the application
60: * appContext.
61: *
62: * @param event HttpSessionEvent passed in by the container
63: */
64: public void sessionCreated(HttpSessionEvent event) {
65: HttpSessionCreatedEvent e = new HttpSessionCreatedEvent(event
66: .getSession());
67:
68: if (log.isDebugEnabled()) {
69: log.debug("Publishing event: " + e);
70: }
71:
72: getContext(event.getSession().getServletContext())
73: .publishEvent(e);
74: }
75:
76: /**
77: * Handles the HttpSessionEvent by publishing a {@link HttpSessionDestroyedEvent} to the application
78: * appContext.
79: *
80: * @param event The HttpSessionEvent pass in by the container
81: */
82: public void sessionDestroyed(HttpSessionEvent event) {
83: HttpSessionDestroyedEvent e = new HttpSessionDestroyedEvent(
84: event.getSession());
85:
86: if (log.isDebugEnabled()) {
87: log.debug("Publishing event: " + e);
88: }
89:
90: getContext(event.getSession().getServletContext())
91: .publishEvent(e);
92: }
93: }
|