001package org.apache.turbine.services.session; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024import java.io.Serializable; 025 026import javax.servlet.http.HttpSessionActivationListener; 027import javax.servlet.http.HttpSessionEvent; 028import javax.servlet.http.HttpSessionListener; 029 030import org.apache.turbine.services.TurbineServices; 031 032/** 033 * This class is a listener for both session creation and destruction, 034 * and for session activation and passivation. It must be configured 035 * via your web application's <code>web.xml</code> deployment 036 * descriptor as follows for the container to call it: 037 * 038 * <pre> 039 * <listener> 040 * <listener-class> 041 * org.apache.turbine.session.SessionListener 042 * </listener-class> 043 * </listener> 044 * </pre> 045 * 046 * <code><listener></code> elements can occur between 047 * <code><context-param></code> and <code><servlet></code> 048 * elements in your deployment descriptor. 049 * 050 * The {@link #sessionCreated(HttpSessionEvent)} callback will 051 * automatically add an instance of this listener to any newly created 052 * <code>HttpSession</code> for detection of session passivation and 053 * re-activation. 054 * 055 * @since 2.3 056 * @version $Id$ 057 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> 058 * @author <a href="mailto:dlr@apache.org">Daniel Rall</a> 059 * @see javax.servlet.http.HttpSessionListener 060 */ 061public class SessionListener 062 implements HttpSessionListener, HttpSessionActivationListener, Serializable 063{ 064 // ---- HttpSessionListener implementation ----------------------------- 065 066 /** 067 * Serial version. 068 */ 069 private static final long serialVersionUID = -8083730704842809870L; 070 071 /** 072 * The session service. 073 */ 074 private SessionService sessionService; 075 076 /** 077 * Lazy initialization 078 * 079 * @return the sessionService 080 */ 081 private SessionService getSessionService() 082 { 083 // don't care about synchronization, lookup is cheap 084 if (sessionService == null) 085 { 086 sessionService = (SessionService)TurbineServices.getInstance().getService(SessionService.SERVICE_NAME); 087 } 088 089 return sessionService; 090 } 091 092 /** 093 * Called by the servlet container when a new session is created 094 * 095 * @param event Session creation event. 096 */ 097 @Override 098 public void sessionCreated(HttpSessionEvent event) 099 { 100 getSessionService().addSession(event.getSession()); 101 event.getSession().setAttribute(getClass().getName(), this); 102 } 103 104 /** 105 * Called by the servlet container when a session is destroyed 106 * 107 * @param event Session destruction event. 108 */ 109 @Override 110 public void sessionDestroyed(HttpSessionEvent event) 111 { 112 getSessionService().removeSession(event.getSession()); 113 } 114 115 116 // ---- HttpSessionActivationListener implementation ------------------- 117 118 /** 119 * Called by the servlet container when an existing session is 120 * (re-)activated. 121 * 122 * @param event Session activation event. 123 */ 124 @Override 125 public void sessionDidActivate(HttpSessionEvent event) 126 { 127 getSessionService().addSession(event.getSession()); 128 } 129 130 /** 131 * Called by the servlet container when a an existing session is 132 * passivated. 133 * 134 * @param event Session passivation event. 135 */ 136 @Override 137 public void sessionWillPassivate(HttpSessionEvent event) 138 { 139 getSessionService().removeSession(event.getSession()); 140 } 141}