From: <fza...@us...> - 2005-11-29 23:08:40
|
Update of /cvsroot/struts/ajaxchat/WEB-INF/src/org/apache/struts/apps/ajaxchat/listener In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21020/WEB-INF/src/org/apache/struts/apps/ajaxchat/listener Added Files: ContextListener.java SessionListener.java package.html Log Message: --- NEW FILE: ContextListener.java --- /* * Copyright 2005 Frank W. Zammetti * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.struts.apps.ajaxchat.listener; import java.io.InputStream; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.apps.ajaxchat.dao.AjaxChatDAO; /** * This ContextListener performs some simple initialization of the application. * Namely, it gets a stream on our rooms configuration file and passes it * along to the DAO's init() method so it can be read in. * * @author <a href="mailto:fza...@om...">Frank W. Zammetti</a>. */ public class ContextListener implements ServletContextListener { /** * Log instance. */ private static Log log = LogFactory.getLog(ContextListener.class); /** * Name of the config file, including context-relarive path. */ private static final String CONFIG_FILE = "/WEB-INF/rooms-config.xml"; /** * Execute at app startup. * * @param event ServletContextEvent. */ public void contextInitialized(ServletContextEvent event) { log.info("ContextListener contextInitialized()..."); // Initialize DAO. AjaxChatDAO dao = AjaxChatDAO.getInstance(); // Get a stream on the config file and initialize the DAO so we'll have // some rooms to chat in. ServletContext servletContext = event.getServletContext(); InputStream isConfigFile = servletContext.getResourceAsStream(CONFIG_FILE); dao.init(isConfigFile); log.info("ContextListener Done"); } // End contextInitialized(); /** * Execute at app shutdown. * * @param event ServletContextEvent. */ public void contextDestroyed(ServletContextEvent event) { } // End contextDestroyed(). } // Ebd class. --- NEW FILE: SessionListener.java --- /* * Copyright 2005 Frank W. Zammetti * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.struts.apps.ajaxchat.listener; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.apps.ajaxchat.dao.AjaxChatDAO; import org.apache.struts.apps.ajaxchat.dto.UserDTO; /** * This SessionListener serves one essential purpose: when a session expires, * either from a timeout or a call to invalidate() during logoff, the user * has to be removed from any room they are in. This should be redundant under * normal circumstances as when the user leaves a room they are removed from it * obviously, and to log off they must leave the room and return to the lobby. * But it a timeout occurs, this takes care of the user otherwise lingering. * * @author <a href="mailto:fza...@om...">Frank W. Zammetti</a>. */ public class SessionListener implements HttpSessionListener { /** * Log instance. */ private static Log log = LogFactory.getLog(SessionListener.class); /** * Unregistering the session when it is destroyed. * * @param event HttpSessionEvent. */ public void sessionCreated(HttpSessionEvent event) { } // End sessionCreated(). /** * Unregistering the session when it is destroyed. * * @param event HttpSessionEvent. */ public void sessionDestroyed(HttpSessionEvent event) { log.info("SessionListener sessionDestroyed()..."); // Get the user from session, if present, and remove them from all rooms. HttpSession session = event.getSession(); AjaxChatDAO dao = AjaxChatDAO.getInstance(); UserDTO user = (UserDTO)session.getAttribute("user"); if (user != null) { dao.removeUserFromAllRooms(user); } log.info("SessionListener sessionDestroyed() Done"); } // End sessionDestroyed(). } // Ebd class. --- NEW FILE: package.html --- <body> The org.apache.struts.apps.ajaxchat.listener package contains listeners needed by the AjaxChat application, including context listeners and session listeners. </body> |