⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contextlistener.java

📁 该源码是使用ajax技术实现的一个聊天室功能。如果想对程序进行修改
💻 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.daemon.UserClearerDaemonThread;
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:fzammetti@omnytex.com">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.debug("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);

    // Lastly, start a background daemon thread that will periodically clear
    // out inactive users from rooms.  This was originally done via
    // SessionListener, but because of some problems seem in some container
    // implementations (Resin, I'm looking at you!), this had to be done
    // instead.
    Thread userClearerDaemonThread = new UserClearerDaemonThread();
    userClearerDaemonThread.setPriority(Thread.MIN_PRIORITY);
    userClearerDaemonThread.setDaemon(true);
    userClearerDaemonThread.start();

    log.info("AjaxChat configured and ready for use");

  } // End contextInitialized();


  /**
   * Execute at app shutdown.
   *
   * @param event ServletContextEvent.
   */
  public void contextDestroyed(ServletContextEvent event) {

  } // End contextDestroyed().


} // Ebd class.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -