View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * 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  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> &lt;listener&gt;<br>
35   * &lt;listener-class&gt;org.acegisecurity.ui.session.HttpSessionEventPublisher&lt;/listener-class&gt;<br>
36   * &lt;/listener&gt;<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      //~ Static fields/initializers =============================================
49  
50      private static final Log log = LogFactory.getLog(HttpSessionEventPublisher.class);
51  
52      //~ Instance fields ========================================================
53  
54      private ApplicationContext context;
55  
56      //~ Methods ================================================================
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 }