view src/main/java/de/mpiwg/web/jsp/utils/SessionCollectorListener.java @ 60:90d5e86c157d

new: auto refresh page when there's new version saved from Ext-Interface
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Wed, 25 May 2016 11:11:32 +0200
parents 3e62083dbcbf
children
line wrap: on
line source

package de.mpiwg.web.jsp.utils;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

import de.mpiwg.web.jsp.SessionBean;

public class SessionCollectorListener implements HttpSessionListener {
	
	
	private static Logger logger = Logger.getLogger(SessionCollectorListener.class);
	
	public static String SESSION_COLLECTOR = "sessionCollector";
	public static String SESSION_TIMER = "sessionTimer";
	
	@Override
    public void sessionCreated(final HttpSessionEvent se) {
    	final HttpSession session = se.getSession();
    	final ServletContext context = session.getServletContext();
    	
    	Map<String, HttpSession> sessionCollector = (Map<String, HttpSession>)context.getAttribute(SESSION_COLLECTOR);
    	if(sessionCollector == null){
    		sessionCollector = new HashMap<String, HttpSession>();
    		context.setAttribute(SESSION_COLLECTOR, sessionCollector);
    	}
    	
    	Map<String, Long> sessionTimer = (Map<String, Long>)context.getAttribute(SESSION_TIMER);
    	if(sessionTimer == null){
    		sessionTimer = new HashMap<String, Long>();
    		context.setAttribute(SESSION_TIMER, sessionTimer);
    	}
    	
    	
    	
    	sessionCollector.put(session.getId(), session);
    	sessionTimer.put(session.getId(), System.currentTimeMillis());
    	logger.info("sessionCreated: " + session.getId());
    }

    @Override
    public void sessionDestroyed(final HttpSessionEvent se) {
    	final HttpSession session = se.getSession();
    	final ServletContext context = session.getServletContext();
    	
    	Map<String, HttpSession> sessionCollector = (Map<String, HttpSession>)context.getAttribute(SESSION_COLLECTOR);
    	sessionCollector.remove(session.getId());
    	
    	Map<String, Long> sessionTimer = (Map<String, Long>)context.getAttribute(SESSION_TIMER);
    	
    	logger.info("sessionDestroyed: " + session.getId() + " timeOut [ms]: " + (System.currentTimeMillis() - sessionTimer.get(session.getId())));
    	
    }

}