关于监听器的设置


package com.sdjt.security;

//import jtml.manage.Setting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.web.context.ContextLoaderListener;
import com.sdjt.bean.system.UserPO;
import com.sdjt.util.Constant;
//extends ContextLoaderListener implements HttpSessionListener,HttpSessionAttributeListener  {
//public class ListenerReq extends HttpServlet implements ServletContextListener,
public class ListenerReq extends ContextLoaderListener implements HttpSessionListener,
						HttpSessionAttributeListener {
	private  IcrmSessionContext  ircmSessionContent =  IcrmSessionContext.getInstance();
	
	private static int count = 0;

	private ServletContext context;


	private static List list = new ArrayList(); //在线用户列表

	private static Map map_menu = new HashMap();//菜单目录

	private static Map map = new HashMap();//登录人员列表
	
	private static Map mapSessionTime = new HashMap(); // session 创建的时间
	
	//private static Setting setting = new Setting();
	
	public static int[] sender_temp = null; // 上次的比较值
	
	public static int[] sender = null; // 控件的运行状态
	
	public static int[] sendercom = null; // 对应的控件后缀
	
	public static String senderExeName = "sdjtsender"; // 控件的进程前缀名称;后缀跟端口号
	
	public static String closerExeName = "closesender"; // 关闭的控件的进程前缀名称;后缀跟端口号
	
	public static String usecom = "1;"; // 使用的端口号;控件名称格式:1号端口sdjtsender1.exe
	
	public static String ProjectName = "sms"; // 项目名称(用于辨别数据库)
	
	public static String preNumberColor = "green";
	
	public static String preNumberCount = "0";
	
	public static String preNumberYe = "0";
	
	public static boolean check = false;
	
	public static String filePath = "";
	
	public void init() throws ServletException {
	}
	 private ServletConfig config;

	 // Init the servlet
	 final public void init(ServletConfig config) throws ServletException {
	   this.config = config;
	 }

	 final public ServletConfig getServletConfig() {
	   return config;
	 }
	 
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("ListenerReq----++---contextInitialized:---------jinru");
		String ENCODING = sce.getServletContext().getInitParameter("ENCODING");
		senderExeName = sce.getServletContext().getInitParameter("sendEXEname");
		closerExeName = sce.getServletContext().getInitParameter("closeEXEname");
		usecom = sce.getServletContext().getInitParameter("usecom");
		try {
			String[] temcom = usecom.split(";");
			sender = new int[temcom.length];
			sender_temp = new int[temcom.length];
			sendercom = new int[temcom.length];
			
			for (int ii = 0; ii < temcom.length; ii++) {
				sendercom[ii] = Integer.parseInt(temcom[ii]);
			}
		} catch (Exception e) {
			sender = new int[20];
			sender_temp = new int[20];
			sendercom = new int[20];
		}
		ProjectName = sce.getServletContext().getInitParameter("projectName");
		
		try {
			this.filePath = sce.getServletContext().getRealPath("/");
			
			// System.out.println("contextInitialized:---------connection");
			//DBServer.getConnection().close();
			// System.out.println("contextInitialized:---------close");
		
			// System.out.println("contextInitialized:---------EmployeeTool");
			// jtml.climgr.AreaIDContainer.load();
//			java.net.InetAddress ia = java.net.InetAddress.getLocalHost();
//			String addr = ia.getHostAddress();
			
			System.out.println("监听打开连接");
			}catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("监听的finally");
		}
	}

	// /Notification that the servlet context is about to be shut down
	public void contextDestroyed(ServletContextEvent sce) {
		if (Constant.debug)
			System.out.println("contextDestroyed");
	}



	// Notification that a session was created
	public void sessionCreated(HttpSessionEvent se) {
		ircmSessionContent.AddSession(se.getSession());
		//if (jtml.util.db.Parameter.isDEBUG()) {
			if (Constant.debug) {
				count++;
				System.out.println("sessionCreated创建-line:"+count);
			}
//	}

	}

	// Notification that a session was invalidated
	public void sessionDestroyed(HttpSessionEvent se) {
		HttpSession session = se.getSession();
		ircmSessionContent.DelSession(session);
		String userID ="";
		if (se.getSession().getAttribute(Constant.UserId) != null) {
			userID = se.getSession().getAttribute(Constant.UserId).toString();
		}
			if (Constant.debug) {
				count--;
				System.out.println("sessionDestroyed销毁--line:"+count);
				System.out.println("sessionDestroyed--map:");
			}
	}
	// Notification that a new attribute has been added to a session
	public synchronized void attributeAdded(HttpSessionBindingEvent se) {
			if (Constant.debug){
				if(Constant.SessionUser.equals(se.getName())) {
					UserPO user = (UserPO)se.getValue();
					map.put(user.getUser_id(), se.getSession());
					mapSessionTime.put(user.getUser_id(), Long.toString(se.getSession().getCreationTime()));
					System.out.println("list chandu =" + list.size());
					if(list.indexOf(String.valueOf(user.getUser_id())) < 0)
						list.add(String.valueOf(user.getUser_id())); 
				} 
				System.out.println("sdfasdf********sename" + se.getName());
		}
	}

	// Notification that an attribute has been removed from a session
	public synchronized void attributeRemoved(HttpSessionBindingEvent se) {
 			if (Constant.debug){
				if(Constant.SessionUser.equals(se.getName())) { 
					UserPO user = (UserPO)se.getValue();
					map.remove(user.getUser_id());
					mapSessionTime.remove(user.getUser_id());
					System.out.println("list chandu2 =" + list.size());
					list.remove(String.valueOf(user.getUser_id()));
				} 
				System.out.println("attributeRemoved*");
 			}
	}

	// Notification that an attribute of a session has been replaced
	public synchronized void attributeReplaced(HttpSessionBindingEvent se) {

				if(Constant.UserId.equals(se.getName())) { 
					list.remove(se.getValue());
				    list.add(se.getSession().getAttribute(Constant.UserId));
					map.remove(se.getValue());
					mapSessionTime.remove(se.getValue());
				}
 				System.out.println("session attributeReplaced*"+se.getValue());
	}
	
	//统计在线人数
	public static int getCount() {
//		return (count);
		return (list.size());
	} 
	
	public synchronized static List getList() {
		return (list);
	}
	
	public synchronized static Map getMap() {
		return (map);
	}

	public synchronized static Map getMap_menu() {
		return map_menu;
	}

	public synchronized static void setMap_menu(Map map_menu) {
		ListenerReq.map_menu = map_menu;
	}

	/*public static Setting getSetting() {
		return setting;
	}

	public static void setSetting(Setting setting) {
		ListenerReq.setting = setting;
	}*/

	public synchronized static Map getMapSessionTime() {
		return mapSessionTime;
	}

	public synchronized static void setMapSessionTime(Map mapSessionTime) {
		ListenerReq.mapSessionTime = mapSessionTime;
	} 
}


你可能感兴趣的:(监听器)