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 { ...@@ -94,7 +94,8 @@ public class SessionManager extends BasicModule {
* list of IncomingServerSession that will keep each session created by a remote server to * list of IncomingServerSession that will keep each session created by a remote server to
* this server. * 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 * The sessions contained in this Map are server sessions originated from this server to remote
...@@ -163,7 +164,7 @@ public class SessionManager extends BasicModule { ...@@ -163,7 +164,7 @@ public class SessionManager extends BasicModule {
*/ */
private class SessionMap { private class SessionMap {
private Map<String,ClientSession> resources = new ConcurrentHashMap<String,ClientSession>(); 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. * Add a session to the manager.
...@@ -521,12 +522,11 @@ public class SessionManager extends BasicModule { ...@@ -521,12 +522,11 @@ public class SessionManager extends BasicModule {
/** /**
* Add a new session to be managed. * Add a new session to be managed.
*/ */
public boolean addSession(ClientSession session) { public void addSession(ClientSession session) {
boolean success = false;
String username = session.getAddress().getNode(); String username = session.getAddress().getNode();
SessionMap resources = null; SessionMap resources;
synchronized(username.intern()) { synchronized (username.intern()) {
resources = sessions.get(username); resources = sessions.get(username);
if (resources == null) { if (resources == null) {
resources = new SessionMap(); resources = new SessionMap();
...@@ -535,14 +535,10 @@ public class SessionManager extends BasicModule { ...@@ -535,14 +535,10 @@ public class SessionManager extends BasicModule {
resources.addSession(session); resources.addSession(session);
// Remove the pre-Authenticated session but remember to use the temporary ID as the key // Remove the pre-Authenticated session but remember to use the temporary ID as the key
preAuthenticatedSessions.remove(session.getStreamID().toString()); 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 { ...@@ -561,7 +557,7 @@ public class SessionManager extends BasicModule {
} }
else { else {
// A non-anonymous session is now available // A non-anonymous session is now available
Session defaultSession = null; Session defaultSession;
try { try {
SessionMap sessionMap = sessions.get(session.getUsername()); SessionMap sessionMap = sessions.get(session.getUsername());
if (sessionMap == null) { if (sessionMap == null) {
...@@ -594,7 +590,7 @@ public class SessionManager extends BasicModule { ...@@ -594,7 +590,7 @@ public class SessionManager extends BasicModule {
*/ */
private void broadcastPresenceToOtherResource(ClientSession session) private void broadcastPresenceToOtherResource(ClientSession session)
throws UserNotFoundException { throws UserNotFoundException {
Presence presence = null; Presence presence;
Collection<ClientSession> availableSession; Collection<ClientSession> availableSession;
SessionMap sessionMap = sessions.get(session.getUsername()); SessionMap sessionMap = sessions.get(session.getUsername());
if (sessionMap != null) { if (sessionMap != null) {
...@@ -801,7 +797,7 @@ public class SessionManager extends BasicModule { ...@@ -801,7 +797,7 @@ public class SessionManager extends BasicModule {
if (from == null) { if (from == null) {
return 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 { ...@@ -820,36 +816,6 @@ public class SessionManager extends BasicModule {
if (serverName == null || !serverName.equals(domain)) { if (serverName == null || !serverName.equals(domain)) {
return null; 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; ClientSession session = null;
// Initially Check preAuthenticated Sessions // Initially Check preAuthenticated Sessions
...@@ -1174,7 +1140,7 @@ public class SessionManager extends BasicModule { ...@@ -1174,7 +1140,7 @@ public class SessionManager extends BasicModule {
*/ */
public void broadcast(Packet packet) throws UnauthorizedException { public void broadcast(Packet packet) throws UnauthorizedException {
for (SessionMap sessionMap : sessions.values()) { for (SessionMap sessionMap : sessions.values()) {
((SessionMap) sessionMap).broadcast(packet); sessionMap.broadcast(packet);
} }
for (Session session : anonymousSessions.values()) { for (Session session : anonymousSessions.values()) {
...@@ -1209,7 +1175,7 @@ public class SessionManager extends BasicModule { ...@@ -1209,7 +1175,7 @@ public class SessionManager extends BasicModule {
if (session == null || serverName == null) { if (session == null || serverName == null) {
return; return;
} }
SessionMap sessionMap = null; SessionMap sessionMap;
if (anonymousSessions.containsValue(session)) { if (anonymousSessions.containsValue(session)) {
anonymousSessions.remove(session.getAddress().getResource()); anonymousSessions.remove(session.getAddress().getResource());
sessionCount--; sessionCount--;
...@@ -1231,12 +1197,13 @@ public class SessionManager extends BasicModule { ...@@ -1231,12 +1197,13 @@ public class SessionManager extends BasicModule {
if (sessionMap.isEmpty()) { if (sessionMap.isEmpty()) {
sessions.remove(username); 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 // If the user is still available then send an unavailable presence
......
...@@ -37,21 +37,20 @@ public class UserManager implements IQResultListener { ...@@ -37,21 +37,20 @@ public class UserManager implements IQResultListener {
/** /**
* Cache of local users. * Cache of local users.
*/ */
private static Cache userCache; private static Cache<String, User> userCache;
/** /**
* Cache if a local or remote user exists. * Cache if a local or remote user exists.
*/ */
private static Cache registeredUsersCache; private static Cache<String, Boolean> registeredUsersCache;
private static UserProvider provider; private static UserProvider provider;
private static UserManager instance = new UserManager(); private static UserManager instance = new UserManager();
static { static {
// Initialize caches. // Initialize caches.
CacheManager.initializeCache("User", "userCache", 512 * 1024); userCache = CacheManager.initializeCache("User", "userCache", 512 * 1024);
CacheManager.initializeCache("Users Existence", "registeredUsersCache", 512 * 1024); registeredUsersCache =
CacheManager.initializeCache("Users Existence", "registeredUsersCache", 512 * 1024);
CacheManager.initializeCache("Roster", "username2roster", 512 * 1024); CacheManager.initializeCache("Roster", "username2roster", 512 * 1024);
userCache = CacheManager.getCache("User");
registeredUsersCache = CacheManager.getCache("Users Existence");
// Load a user provider. // Load a user provider.
String className = JiveGlobals.getXMLProperty("provider.user.className", String className = JiveGlobals.getXMLProperty("provider.user.className",
"org.jivesoftware.wildfire.user.DefaultUserProvider"); "org.jivesoftware.wildfire.user.DefaultUserProvider");
...@@ -164,10 +163,10 @@ public class UserManager implements IQResultListener { ...@@ -164,10 +163,10 @@ public class UserManager implements IQResultListener {
public User getUser(String username) throws UserNotFoundException { public User getUser(String username) throws UserNotFoundException {
// Make sure that the username is valid. // Make sure that the username is valid.
username = username.trim().toLowerCase(); username = username.trim().toLowerCase();
User user = (User) userCache.get(username); User user = userCache.get(username);
if (user == null) { if (user == null) {
synchronized(username.intern()) { synchronized (username.intern()) {
user = (User) userCache.get(username); user = userCache.get(username);
if (user == null) { if (user == null) {
user = provider.loadUser(username); user = provider.loadUser(username);
userCache.put(username, user); userCache.put(username, user);
...@@ -262,7 +261,7 @@ public class UserManager implements IQResultListener { ...@@ -262,7 +261,7 @@ public class UserManager implements IQResultListener {
*/ */
public String getUserProperty(String username, String propertyName) { public String getUserProperty(String username, String propertyName) {
username = username.trim().toLowerCase(); username = username.trim().toLowerCase();
User user = (User) userCache.get(username); User user = userCache.get(username);
if (user == null) { if (user == null) {
return User.getPropertyValue(username, propertyName); return User.getPropertyValue(username, propertyName);
} }
...@@ -283,7 +282,7 @@ public class UserManager implements IQResultListener { ...@@ -283,7 +282,7 @@ public class UserManager implements IQResultListener {
return false; return false;
} }
// Look up in the cache // Look up in the cache
Boolean isRegistered = (Boolean) registeredUsersCache.get(username); Boolean isRegistered = registeredUsersCache.get(username);
if (isRegistered == null) { if (isRegistered == null) {
// No information is cached so check user identity and cache it // No information is cached so check user identity and cache it
try { try {
...@@ -296,7 +295,7 @@ public class UserManager implements IQResultListener { ...@@ -296,7 +295,7 @@ public class UserManager implements IQResultListener {
// Cache "discovered" information // Cache "discovered" information
registeredUsersCache.put(username, isRegistered); registeredUsersCache.put(username, isRegistered);
} }
return isRegistered.booleanValue(); return isRegistered;
} }
/** /**
...@@ -308,17 +307,17 @@ public class UserManager implements IQResultListener { ...@@ -308,17 +307,17 @@ public class UserManager implements IQResultListener {
* @return true if the specified JID belongs to a local or remote registered user. * @return true if the specified JID belongs to a local or remote registered user.
*/ */
public boolean isRegisteredUser(JID user) { public boolean isRegisteredUser(JID user) {
Boolean isRegistered = null; Boolean isRegistered;
XMPPServer server = XMPPServer.getInstance(); XMPPServer server = XMPPServer.getInstance();
if (server.isLocal(user)) { if (server.isLocal(user)) {
isRegistered = (Boolean) registeredUsersCache.get(user.getNode()); isRegistered = registeredUsersCache.get(user.getNode());
} }
else { else {
// Look up in the cache using the full JID // Look up in the cache using the full JID
isRegistered = (Boolean) registeredUsersCache.get(user.toString()); isRegistered = registeredUsersCache.get(user.toString());
if (isRegistered == null) { if (isRegistered == null) {
// Check if the bare JID of the user is cached // 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 { ...@@ -352,10 +351,12 @@ public class UserManager implements IQResultListener {
try { try {
user.toBareJID().intern().wait(600000); user.toBareJID().intern().wait(600000);
} }
catch (InterruptedException e) {} catch (InterruptedException e) {
// Do nothing
}
} }
// Get the discovered result // Get the discovered result
isRegistered = (Boolean) registeredUsersCache.get(user.toBareJID()); isRegistered = registeredUsersCache.get(user.toBareJID());
if (isRegistered == null) { if (isRegistered == null) {
// Disco failed for some reason (i.e. we timed out before getting a result) // Disco failed for some reason (i.e. we timed out before getting a result)
// so assume that user is not anonymous and cache result // so assume that user is not anonymous and cache result
...@@ -364,7 +365,7 @@ public class UserManager implements IQResultListener { ...@@ -364,7 +365,7 @@ public class UserManager implements IQResultListener {
} }
} }
} }
return isRegistered.booleanValue(); return isRegistered;
} }
public void receivedAnswer(IQ packet) { 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