Commit f859b9d3 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

1) Fire events outside of the synch block

2) Small refactoring
3) Code clean up

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3658 b35dd754-fafc-0310-a699-88a17e54d16e
parent 29e38f98
......@@ -94,7 +94,8 @@ public class SessionManager extends BasicModule {
* list of IncomingServerSession that will keep each session created by a remote server to
* this server.
*/
private Map<String, List<IncomingServerSession>> incomingServerSessions = new ConcurrentHashMap<String, List<IncomingServerSession>>();
private final Map<String, List<IncomingServerSession>> incomingServerSessions =
new ConcurrentHashMap<String, List<IncomingServerSession>>();
/**
* The sessions contained in this Map are server sessions originated from this server to remote
......@@ -163,7 +164,7 @@ public class SessionManager extends BasicModule {
*/
private class SessionMap {
private Map<String,ClientSession> resources = new ConcurrentHashMap<String,ClientSession>();
private LinkedList<String> priorityList = new LinkedList<String>();
private final LinkedList<String> priorityList = new LinkedList<String>();
/**
* Add a session to the manager.
......@@ -521,12 +522,11 @@ public class SessionManager extends BasicModule {
/**
* Add a new session to be managed.
*/
public boolean addSession(ClientSession session) {
boolean success = false;
public void addSession(ClientSession session) {
String username = session.getAddress().getNode();
SessionMap resources = null;
SessionMap resources;
synchronized(username.intern()) {
synchronized (username.intern()) {
resources = sessions.get(username);
if (resources == null) {
resources = new SessionMap();
......@@ -535,14 +535,10 @@ public class SessionManager extends BasicModule {
resources.addSession(session);
// Remove the pre-Authenticated session but remember to use the temporary ID as the key
preAuthenticatedSessions.remove(session.getStreamID().toString());
// Fire session created event.
SessionEventDispatcher.dispatchEvent(session,
SessionEventDispatcher.EventType.session_created);
success = true;
}
return success;
// Fire session created event.
SessionEventDispatcher
.dispatchEvent(session, SessionEventDispatcher.EventType.session_created);
}
/**
......@@ -561,7 +557,7 @@ public class SessionManager extends BasicModule {
}
else {
// A non-anonymous session is now available
Session defaultSession = null;
Session defaultSession;
try {
SessionMap sessionMap = sessions.get(session.getUsername());
if (sessionMap == null) {
......@@ -594,7 +590,7 @@ public class SessionManager extends BasicModule {
*/
private void broadcastPresenceToOtherResource(ClientSession session)
throws UserNotFoundException {
Presence presence = null;
Presence presence;
Collection<ClientSession> availableSession;
SessionMap sessionMap = sessions.get(session.getUsername());
if (sessionMap != null) {
......@@ -801,7 +797,7 @@ public class SessionManager extends BasicModule {
if (from == null) {
return null;
}
return getSession(from.toString(), from.getNode(), from.getDomain(), from.getResource());
return getSession(from.getNode(), from.getDomain(), from.getResource());
}
/**
......@@ -820,36 +816,6 @@ public class SessionManager extends BasicModule {
if (serverName == null || !serverName.equals(domain)) {
return null;
}
// Build a JID represention based on the given JID data
StringBuilder buf = new StringBuilder(40);
if (username != null) {
username = username.toLowerCase();
buf.append(username).append("@");
}
buf.append(domain);
if (resource != null) {
buf.append("/").append(resource);
}
return getSession(buf.toString(), username, domain, resource);
}
/**
* Returns the session responsible for this JID data. The returned Session may have never sent
* an available presence (thus not have a route) or could be a Session that hasn't
* authenticated yet (i.e. preAuthenticatedSessions).
*
* @param jid the full representation of the JID.
* @param username the username of the JID.
* @param domain the username of the JID.
* @param resource the username of the JID.
* @return the <code>Session</code> associated with the JID data.
*/
private ClientSession getSession(String jid, String username, String domain, String resource) {
// Return null if the JID's data belongs to a foreign server. If the server is
// shutting down then serverName will be null so answer null too in this case.
if (serverName == null || !serverName.equals(domain)) {
return null;
}
ClientSession session = null;
// Initially Check preAuthenticated Sessions
......@@ -1174,7 +1140,7 @@ public class SessionManager extends BasicModule {
*/
public void broadcast(Packet packet) throws UnauthorizedException {
for (SessionMap sessionMap : sessions.values()) {
((SessionMap) sessionMap).broadcast(packet);
sessionMap.broadcast(packet);
}
for (Session session : anonymousSessions.values()) {
......@@ -1209,7 +1175,7 @@ public class SessionManager extends BasicModule {
if (session == null || serverName == null) {
return;
}
SessionMap sessionMap = null;
SessionMap sessionMap;
if (anonymousSessions.containsValue(session)) {
anonymousSessions.remove(session.getAddress().getResource());
sessionCount--;
......@@ -1231,12 +1197,13 @@ public class SessionManager extends BasicModule {
if (sessionMap.isEmpty()) {
sessions.remove(username);
}
// Fire session event.
SessionEventDispatcher.dispatchEvent(session,
SessionEventDispatcher.EventType.session_destroyed);
}
}
if (sessionMap != null) {
// Fire session event.
SessionEventDispatcher.dispatchEvent(session,
SessionEventDispatcher.EventType.session_destroyed);
}
}
}
// If the user is still available then send an unavailable presence
......
......@@ -37,21 +37,20 @@ public class UserManager implements IQResultListener {
/**
* Cache of local users.
*/
private static Cache userCache;
private static Cache<String, User> userCache;
/**
* Cache if a local or remote user exists.
*/
private static Cache registeredUsersCache;
private static Cache<String, Boolean> registeredUsersCache;
private static UserProvider provider;
private static UserManager instance = new UserManager();
static {
// Initialize caches.
CacheManager.initializeCache("User", "userCache", 512 * 1024);
CacheManager.initializeCache("Users Existence", "registeredUsersCache", 512 * 1024);
userCache = CacheManager.initializeCache("User", "userCache", 512 * 1024);
registeredUsersCache =
CacheManager.initializeCache("Users Existence", "registeredUsersCache", 512 * 1024);
CacheManager.initializeCache("Roster", "username2roster", 512 * 1024);
userCache = CacheManager.getCache("User");
registeredUsersCache = CacheManager.getCache("Users Existence");
// Load a user provider.
String className = JiveGlobals.getXMLProperty("provider.user.className",
"org.jivesoftware.wildfire.user.DefaultUserProvider");
......@@ -164,10 +163,10 @@ public class UserManager implements IQResultListener {
public User getUser(String username) throws UserNotFoundException {
// Make sure that the username is valid.
username = username.trim().toLowerCase();
User user = (User) userCache.get(username);
User user = userCache.get(username);
if (user == null) {
synchronized(username.intern()) {
user = (User) userCache.get(username);
synchronized (username.intern()) {
user = userCache.get(username);
if (user == null) {
user = provider.loadUser(username);
userCache.put(username, user);
......@@ -262,7 +261,7 @@ public class UserManager implements IQResultListener {
*/
public String getUserProperty(String username, String propertyName) {
username = username.trim().toLowerCase();
User user = (User) userCache.get(username);
User user = userCache.get(username);
if (user == null) {
return User.getPropertyValue(username, propertyName);
}
......@@ -283,7 +282,7 @@ public class UserManager implements IQResultListener {
return false;
}
// Look up in the cache
Boolean isRegistered = (Boolean) registeredUsersCache.get(username);
Boolean isRegistered = registeredUsersCache.get(username);
if (isRegistered == null) {
// No information is cached so check user identity and cache it
try {
......@@ -296,7 +295,7 @@ public class UserManager implements IQResultListener {
// Cache "discovered" information
registeredUsersCache.put(username, isRegistered);
}
return isRegistered.booleanValue();
return isRegistered;
}
/**
......@@ -308,17 +307,17 @@ public class UserManager implements IQResultListener {
* @return true if the specified JID belongs to a local or remote registered user.
*/
public boolean isRegisteredUser(JID user) {
Boolean isRegistered = null;
Boolean isRegistered;
XMPPServer server = XMPPServer.getInstance();
if (server.isLocal(user)) {
isRegistered = (Boolean) registeredUsersCache.get(user.getNode());
isRegistered = registeredUsersCache.get(user.getNode());
}
else {
// Look up in the cache using the full JID
isRegistered = (Boolean) registeredUsersCache.get(user.toString());
isRegistered = registeredUsersCache.get(user.toString());
if (isRegistered == null) {
// Check if the bare JID of the user is cached
isRegistered = (Boolean) registeredUsersCache.get(user.toBareJID());
isRegistered = registeredUsersCache.get(user.toBareJID());
}
}
......@@ -352,10 +351,12 @@ public class UserManager implements IQResultListener {
try {
user.toBareJID().intern().wait(600000);
}
catch (InterruptedException e) {}
catch (InterruptedException e) {
// Do nothing
}
}
// Get the discovered result
isRegistered = (Boolean) registeredUsersCache.get(user.toBareJID());
isRegistered = registeredUsersCache.get(user.toBareJID());
if (isRegistered == null) {
// Disco failed for some reason (i.e. we timed out before getting a result)
// so assume that user is not anonymous and cache result
......@@ -364,7 +365,7 @@ public class UserManager implements IQResultListener {
}
}
}
return isRegistered.booleanValue();
return isRegistered;
}
public void receivedAnswer(IQ packet) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment