gt: 		for (UserSession us : this.loggedSessions.values()) {
context: /*
   * 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 Rafael Steil
   */
  @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());
  
  				if (existing != null) {
  					userSession.setLastVisit(existing.getLastVisit());
  					this.remove(existing.getSessionId());
  				}
  				else {
  					Session session = this.sessionRepository.get(userSession.getUser().getId());
  
  					if (session != null && session.getLastVisit() != null) {
  						userSession.setLastVisit(session.getLastVisit().getTime());
  					}
  				}
  
  				this.checkIfIsModerator(userSession);
  
  				this.loggedSessions.put(userSession.getSessionId(), userSession);
  			}
  		}
  	}
  
  	private void checkIfIsModerator(UserSession userSession) {
  		RoleManager roleManager = new RoleManager();
  		roleManager.setGroups(userSession.getUser().getGroups());
  
  		if (roleManager.isModerator()) {
  			this.moderatorsOnline++;
  		}
  	}
  
  	public void computeAllOnlineModerators() {
  		this.moderatorsOnline = 0;
  		Collection<UserSession> sessions = this.loggedSessions.values();
  
  		for (UserSession session : sessions) {
  			this.checkIfIsModerator(session);
  		}
  	}
  
  	public boolean isModeratorOnline() {
  		return this.moderatorsOnline > 0;
  	}
  
  	/**
  	 * Make sure we'll not add a session that was already registered
  	 * @param us
  	 */
  	private void preventDuplicates(UserSession us) {
  		if (this.getUserSession(us.getSessionId()) != null) {
  			this.remove(us.getSessionId());
  		}
  	}
  
  	/**
  	 * Remove an entry fro the session map
  	 *
  	 * @param sessionId The session id to remove
  	 */
  	public synchronized void remove(String sessionId) {
  		if (this.loggedSessions.containsKey(sessionId)) {
  			UserSession userSession = this.getUserSession(sessionId);
  
  			if (userSession.getRoleManager() != null
  				&& userSession.getRoleManager().isModerator() && this.moderatorsOnline > 0) {
  				this.moderatorsOnline--;
  			}
  
  			this.loggedSessions.remove(sessionId);
  		}
  		else {
  			this.anonymousSessions.remove(sessionId);
  		}
  	}
  
  	/**
  	 * Get all registered sessions
  	 *
  	 * @return <code>ArrayList</code> with the sessions. Each entry is an <code>UserSession</code> object.
  	 */
  	public List<UserSession> getAllSessions() {
  		List<UserSession> list = new ArrayList<UserSession>(this.loggedSessions.values());
  		list.addAll(this.anonymousSessions.values());
  
  		return list;
  	}
  
  	/**
  	 * Gets the {@link UserSession} instance of all logged users
  	 *
  	 * @return A list with the user sessions
  	 */
  	public Collection<UserSession> getLoggedSessions() {
  		return this.loggedSessions.values();
  	}
  
  	/**
  	 * Get the number of logged users
  	 *
  	 * @return the number of logged users
  	 */
  	public int getTotalLoggedUsers() {
  		return this.loggedSessions.size();
  	}
  
  	/**
  	 * Get the number of anonymous users
  	 *
  	 * @return the nuber of anonymous users
  	 */
  	public int getTotalAnonymousUsers() {
  		return this.anonymousSessions.size();
  	}
  
  	/**
  	 * Gets an {@link UserSession} by the session id.
  	 *
  	 * @param sessionId the session's id
  	 * @return the user session
  	 */
  	public UserSession getUserSession(String sessionId) {
  		UserSession us = this.anonymousSessions.get(sessionId);
  		return us != null ? us : this.loggedSessions.get(sessionId);
  	}
  
  	/**
  	 * Gets the number of session elements.
  	 *
  	 * @return The number of session elements currently online (without bots)
  	 */
  	public int getTotalUsers() {
  		return this.anonymousSessions.size() + this.loggedSessions.size();
  	}
  
  	/**
  	 * Check if a given user in in the session
  	 *
  	 * @param userId The user id to check for existance in the session
  	 * @return The respective {@link UserSession} if the user is already registered, or <code>null</code> otherwise.
  	 */
  	public UserSession isUserInSession(int userId) {
  