<commit_before>/**
 * 
 */
package org.openforis.collect.web.listener;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openforis.collect.manager.SessionManager;
import org.openforis.collect.model.User;
import org.openforis.collect.web.session.InvalidSessionException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author <NAME>
 * @author <NAME>
 * 
 */
public class SessionListener implements HttpSessionListener {

	private static Log LOG = LogFactory.getLog(SessionListener.class);
	
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		SessionManager sessionManager = getSessionManager(se);
		sessionManager.createSessionState(se.getSession());
		
		if ( LOG.isInfoEnabled() ) {
			LOG.info("Session created: " + se.getSession().getId());
		}
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		SessionManager sessionManager = getSessionManager(se);
		try {
			User user = sessionManager.getSessionState().getUser();
			if (user != null) {
				sessionManager.sessionDestroyed();
			}
			if ( LOG.isInfoEnabled() ) {
				String message = "Session destroyed: " + se.getSession().getId();
				if ( user != null ) {
					message += " username: " +user.getUsername();
				}
				LOG.info(message);
			}
		} catch(InvalidSessionException e) {
			//ignore it, session was anonymous
		}
//		HttpSession session = se.getSession();
//		Object sessionStateObj = session.getAttribute(SessionState.SESSION_ATTRIBUTE_NAME);
//		User user = null;
//		if (sessionStateObj != null) {
//			SessionState sessionState = (SessionState) sessionStateObj;
//			CollectRecord record = sessionState.getActiveRecord();
//			user = sessionState.getUser();
//			if (record != null && record.getId() != null && user != null) {
//				RecordManager recordManager = getBean(se, RecordManager.class);
//				recordManager.releaseLock(record.getId());
//			}
//			
//		}
	}

	private SessionManager getSessionManager(HttpSessionEvent se) {
		return getBean(se, "sessionManager");
	}

	@SuppressWarnings("unchecked")
	private <T extends Object> T getBean(HttpSessionEvent se,
			String name) {
		ServletContext sc = se.getSession().getServletContext();
		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
		return (T) applicationContext.getBean(name);
	}
	
}
<commit_msg>Fix session destroyed NPE<commit_after>/**
 * 
 */
package org.openforis.collect.web.listener;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openforis.collect.manager.SessionManager;
import org.openforis.collect.model.User;
import org.openforis.collect.web.session.InvalidSessionException;
import org.openforis.collect.web.session.SessionState;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author <NAME>
 * @author <NAME>
 * 
 */
public class SessionListener implements HttpSessionListener {

	private static Log LOG = LogFactory.getLog(SessionListener.class);
	
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		SessionManager sessionManager = getSessionManager(se);
		sessionManager.createSessionState(se.getSession());
		
		if ( LOG.isInfoEnabled() ) {
			LOG.info("Session created: " + se.getSession().getId());
		}
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		SessionManager sessionManager = getSessionManager(se);
		try {
			SessionState sessionState = sessionManager.getSessionState();
			User user = null;
			if (sessionState != null) {
				user = sessionState.getUser();
				if (user != null) {
					sessionManager.sessionDestroyed();
				}
			}
			if ( LOG.isInfoEnabled() ) {
				String message = "Session destroyed: " + se.getSession().getId();
				if ( user != null ) {
					message += " username: " +user.getUsername();
				}
				LOG.info(message);
			}
		} catch(InvalidSessionException e) {
			//ignore it, session was anonymous
		}
	}

	private SessionManager getSessionManager(HttpSessionEvent se) {
		return getBean(se, "sessionManager");
	}

	@SuppressWarnings("unchecked")
	private <T extends Object> T getBean(HttpSessionEvent se,
			String name) {
		ServletContext sc = se.getSession().getServletContext();
		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
		return (T) applicationContext.getBean(name);
	}
	
}

{'fraction_non_alphanumeric': 0.11859893486276116, 'fraction_numerical': 0.0, 'mean_word_length': 4.284632034632034, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5531490', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1413, 'n_words': 274}