Coverage Report - org.acegisecurity.ui.session.HttpSessionEventPublisher
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpSessionEventPublisher
85% 
100% 
1.667
 
 1  
 /* Copyright 2004, 2005, 2006 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  
 
 25  
 import javax.servlet.ServletContext;
 26  
 import javax.servlet.http.HttpSessionEvent;
 27  
 import javax.servlet.http.HttpSessionListener;
 28  
 
 29  
 
 30  
 /**
 31  
  * Declared in web.xml as
 32  
  * <pre>
 33  
  * &lt;listener&gt;
 34  
  *     &lt;listener-class&gt;org.acegisecurity.ui.session.HttpSessionEventPublisher&lt;/listener-class&gt;
 35  
  * &lt;/listener&gt;
 36  
  * </pre>
 37  
  *
 38  
  * Publishes <code>HttpSessionApplicationEvent</code>s to the Spring Root WebApplicationContext. Maps
 39  
  * javax.servlet.http.HttpSessionListener.sessionCreated() to {@link HttpSessionCreatedEvent}. Maps
 40  
  * javax.servlet.http.HttpSessionListener.sessionDestroyed() to {@link HttpSessionDestroyedEvent}.
 41  
  *
 42  
  * @author Ray Krueger
 43  
  */
 44  1
 public class HttpSessionEventPublisher implements HttpSessionListener {
 45  
     //~ Static fields/initializers =====================================================================================
 46  
 
 47  2
     private static final Log log = LogFactory.getLog(HttpSessionEventPublisher.class);
 48  
 
 49  
     //~ Instance fields ================================================================================================
 50  
 
 51  
     //~ Methods ========================================================================================================
 52  
 
 53  
     ApplicationContext getContext(ServletContext servletContext) {
 54  2
         return WebApplicationContextUtils.getWebApplicationContext(servletContext);
 55  
     }
 56  
 
 57  
     /**
 58  
      * Handles the HttpSessionEvent by publishing a {@link HttpSessionCreatedEvent} to the application
 59  
      * appContext.
 60  
      *
 61  
      * @param event HttpSessionEvent passed in by the container
 62  
      */
 63  
     public void sessionCreated(HttpSessionEvent event) {
 64  1
         HttpSessionCreatedEvent e = new HttpSessionCreatedEvent(event.getSession());
 65  
 
 66  1
         if (log.isDebugEnabled()) {
 67  0
             log.debug("Publishing event: " + e);
 68  
         }
 69  
 
 70  1
         getContext(event.getSession().getServletContext()).publishEvent(e);
 71  1
     }
 72  
 
 73  
     /**
 74  
      * Handles the HttpSessionEvent by publishing a {@link HttpSessionDestroyedEvent} to the application
 75  
      * appContext.
 76  
      *
 77  
      * @param event The HttpSessionEvent pass in by the container
 78  
      */
 79  
     public void sessionDestroyed(HttpSessionEvent event) {
 80  1
         HttpSessionDestroyedEvent e = new HttpSessionDestroyedEvent(event.getSession());
 81  
 
 82  1
         if (log.isDebugEnabled()) {
 83  0
             log.debug("Publishing event: " + e);
 84  
         }
 85  
 
 86  1
         getContext(event.getSession().getServletContext()).publishEvent(e);
 87  1
     }
 88  
 }