<commit_before>/*
 * version 1.0 - MUSES prototype software
 * Copyright MUSES project (European Commission FP7) - 2013 
 * 
 */
package eu.musesproject.server.connectionmanager;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * This class handles all http session, called every time a 
 * request come from the client for both POST/GET. 
 * 
 * @author <NAME>
 * @version Jan 27, 2014
 */

public class SessionHandler implements ServletContextListener , HttpSessionListener, ServletRequestListener{
	
	private static Set<String> sessionIDs = new HashSet<String>();
	private static Map<Date,Cookie> cookieSet = new ConcurrentHashMap<Date,Cookie>();
	private static final String ATTRIBUTE_NAME = "com.swedenconnectivity.comserver.SessionHandler";
	private Logger logger = Logger.getLogger(SessionHandler.class.getName());

	
	public SessionHandler() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void requestDestroyed(ServletRequestEvent sre) {
		
	}
	
	/**
	 * This methods check for request method POST/GET, retrieve and set
	 * poll-interval value from the header 
	 * @param ServletRequestEvent
	 * @return void
	 */
	
	@Override
	public void requestInitialized(ServletRequestEvent sre) {
		
		int interval=0;
		HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
		if (request.getMethod().equalsIgnoreCase("POST")){
			String pollIntervalTimeout = request.getHeader("poll-interval");
			HttpSession session = request.getSession();
			if (pollIntervalTimeout != null) {
				interval = Integer.parseInt(pollIntervalTimeout) * 2;
				session.setMaxInactiveInterval(interval);
			}
		}
		
	}
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		sce.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
		
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
	
	}

	@Override
	public void sessionCreated(HttpSessionEvent event) {
		removeExpiredCookies();
	}

	/** 
	 * Called every time a session is destroyed, if the session
	 * is in the table remove it from the list and call the session callback method
	 * to inform other users
	 * @param HttpSessionEvent event
	 * @return void
	 */
	
	@Override<commit_msg>unregistering jdbc driver in servlet context listener<commit_after>/*
 * version 1.0 - MUSES prototype software
 * Copyright MUSES project (European Commission FP7) - 2013 
 * 
 */
package eu.musesproject.server.connectionmanager;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * This class handles all http session, called every time a 
 * request come from the client for both POST/GET. 
 * 
 * @author <NAME>
 * @version Jan 27, 2014
 */

public class SessionHandler implements ServletContextListener , HttpSessionListener, ServletRequestListener{
	
	private static Set<String> sessionIDs = new HashSet<String>();
	private static Map<Date,Cookie> cookieSet = new ConcurrentHashMap<Date,Cookie>();
	private static final String ATTRIBUTE_NAME = "com.swedenconnectivity.comserver.SessionHandler";
	private Logger logger = Logger.getLogger(SessionHandler.class.getName());

	
	public SessionHandler() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void requestDestroyed(ServletRequestEvent sre) {
		
	}
	
	/**
	 * This methods check for request method POST/GET, retrieve and set
	 * poll-interval value from the header 
	 * @param ServletRequestEvent
	 * @return void
	 */
	
	@Override
	public void requestInitialized(ServletRequestEvent sre) {
		
		int interval=0;
		HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
		if (request.getMethod().equalsIgnoreCase("POST")){
			String pollIntervalTimeout = request.getHeader("poll-interval");
			HttpSession session = request.getSession();
			if (pollIntervalTimeout != null) {
				interval = Integer.parseInt(pollIntervalTimeout) * 2;
				session.setMaxInactiveInterval(interval);
			}
		}
		
	}
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		sce.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
		
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		 Enumeration<java.sql.Driver> drivers = DriverManager.getDrivers();     

	        java.sql.Driver driver = null;

	        // clear drivers
	        while(drivers.hasMoreElements()) {
	            try {
	                driver = drivers.nextElement();
	                DriverManager.deregisterDriver(driver);

	            } catch (SQLException ex) {
	                // deregistration failed, might want to do something, log at the very least
	            }
	        }
	}

	@Override
	public void sessionCreated(HttpSessionEvent event) {
		removeExpiredCookies();
	}

	/** 
	 * Called every time a session is destroyed, if the session
	 * is in the table remove it from the list and call the session callback method
	 * to inform other users
	 * @param HttpSessionEvent event
	 * @return void
	 */
	
	@Override
{'fraction_non_alphanumeric': 0.0905587668593449, 'fraction_numerical': 0.005459216441875401, 'mean_word_length': 4.346781115879828, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '831814', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1673, 'n_words': 465}