<commit_before>/*
 * Copyright (c) JForum Team. All rights reserved.
 *
 * The software in this package is published under the terms of the LGPL
 * license a copy of which has been included with this distribution in the
 * license.txt file.
 *
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;

import net.jforum.core.exceptions.ForumException;
import net.jforum.entities.Session;
import net.jforum.entities.User;
import net.jforum.entities.UserSession;
import net.jforum.repository.SessionRepository;
import net.jforum.repository.UserRepository;
import net.jforum.security.RoleManager;
import net.jforum.sso.SSO;
import net.jforum.sso.SSOUtils;
import net.jforum.util.ConfigKeys;
import net.jforum.util.JForumConfig;
import net.jforum.util.MD5;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;

/**
 * Manages all user sessions
 * @author <NAME>
 */
@Component
@ApplicationScoped
public class SessionManager {
	private static final Logger logger = Logger.getLogger(SessionManager.class);
	private Map<String, UserSession> loggedSessions = new HashMap<String, UserSession>();
	private Map<String, UserSession> anonymousSessions = new HashMap<String, UserSession>();
	private UserRepository userRepository;
	private SessionRepository sessionRepository;
	private JForumConfig config;
	private int moderatorsOnline;

	public SessionManager(JForumConfig config, UserRepository userRepository, SessionRepository sessionRepository) {
		this.config = config;
		this.userRepository = userRepository;
		this.sessionRepository = sessionRepository;
	}

	/**
	 * Registers a new {@link UserSession}.
	 *
	 * @param userSession The user session to add
	 */
	public synchronized void add(UserSession userSession) {
		if (StringUtils.isEmpty(userSession.getSessionId())) {
			throw new ForumException("An UserSession instance must have a session ID");
		}

		if (!userSession.isBot()) {
			this.preventDuplicates(userSession);

			if (userSession.getUser().getId() == this.config.getInt(ConfigKeys.ANONYMOUS_USER_ID)) {
				this.anonymousSessions.put(userSession.getSessionId(), userSession);
			}
			else {
				UserSession existing = this.isUserInSession(userSession.getUser().getId());<commit_msg>Left SessionManager as request scoped component, until I find a better approach<commit_after>/*
 * Copyright (c) JForum Team. All rights reserved.
 *
 * The software in this package is published under the terms of the LGPL
 * license a copy of which has been included with this distribution in the
 * license.txt file.
 *
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;

import net.jforum.core.exceptions.ForumException;
import net.jforum.entities.Session;
import net.jforum.entities.User;
import net.jforum.entities.UserSession;
import net.jforum.repository.SessionRepository;
import net.jforum.repository.UserRepository;
import net.jforum.security.RoleManager;
import net.jforum.sso.SSO;
import net.jforum.sso.SSOUtils;
import net.jforum.util.ConfigKeys;
import net.jforum.util.JForumConfig;
import net.jforum.util.MD5;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import br.com.caelum.vraptor.ioc.Component;

/**
 * Manages all user sessions
 * @author <NAME>
 */
@Component
public class SessionManager {
	private static final Logger logger = Logger.getLogger(SessionManager.class);
	private static Map<String, UserSession> loggedSessions = new HashMap<String, UserSession>();
	private static Map<String, UserSession> anonymousSessions = new HashMap<String, UserSession>();
	private UserRepository userRepository;
	private SessionRepository sessionRepository;
	private JForumConfig config;
	private int moderatorsOnline;

	public SessionManager(JForumConfig config, SessionRepository sessionRepository, UserRepository userRepository) {
		this.config = config;
		this.userRepository = userRepository;
		this.sessionRepository = sessionRepository;
	}

	/**
	 * Registers a new {@link UserSession}.
	 *
	 * @param userSession The user session to add
	 */
	public synchronized void add(UserSession userSession) {
		if (StringUtils.isEmpty(userSession.getSessionId())) {
			throw new ForumException("An UserSession instance must have a session ID");
		}

		if (!userSession.isBot()) {
			this.preventDuplicates(userSession);

			if (userSession.getUser().getId() == this.config.getInt(ConfigKeys.ANONYMOUS_USER_ID)) {
				this.anonymousSessions.put(userSession.getSessionId(), userSession);
			}
			else {
				UserSession existing = this.isUserInSession(userSession.getUser().getId());
{'fraction_non_alphanumeric': 0.10112581473434723, 'fraction_numerical': 0.0007900454276120877, 'mean_word_length': 5.2518518518518515, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4257614', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1485, 'n_words': 379}