From b6c36a12e50e8117b59b36cc49704079696b623f Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 1 Mar 2015 18:05:35 -0800 Subject: [PATCH] GUAC-1101: Manage active connection records using sets rather than lists. --- .../jdbc/connection/ConnectionService.java | 2 ++ .../AbstractGuacamoleSocketService.java | 4 +-- .../jdbc/socket/ActiveConnectionMultimap.java | 36 ++++++++++--------- .../jdbc/socket/GuacamoleSocketService.java | 28 ++++++++------- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connection/ConnectionService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connection/ConnectionService.java index f7d0b5ac5..9bf566770 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connection/ConnectionService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connection/ConnectionService.java @@ -26,6 +26,7 @@ import com.google.inject.Inject; import com.google.inject.Provider; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -367,6 +368,7 @@ public class ConnectionService extends DirectoryObjectService records = new ArrayList(socketService.getActiveConnections(connection)); + Collections.reverse(records); // Add past connections from model objects for (ConnectionRecordModel model : models) diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/AbstractGuacamoleSocketService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/AbstractGuacamoleSocketService.java index 78f4d61ca..d684f29c9 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/AbstractGuacamoleSocketService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/AbstractGuacamoleSocketService.java @@ -268,7 +268,7 @@ public abstract class AbstractGuacamoleSocketService implements GuacamoleSocketS } @Override - public List getActiveConnections(Connection connection) { + public Collection getActiveConnections(Connection connection) { return activeConnections.get(connection.getIdentifier()); } @@ -305,7 +305,7 @@ public abstract class AbstractGuacamoleSocketService implements GuacamoleSocketS } @Override - public List getActiveConnections(ConnectionGroup connectionGroup) { + public Collection getActiveConnections(ConnectionGroup connectionGroup) { // If not a balancing group, assume no connections if (connectionGroup.getType() != ConnectionGroup.Type.BALANCING) diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/ActiveConnectionMultimap.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/ActiveConnectionMultimap.java index 5c6213965..e20ed733f 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/ActiveConnectionMultimap.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/ActiveConnectionMultimap.java @@ -22,11 +22,12 @@ package org.glyptodon.guacamole.auth.jdbc.socket; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; import org.glyptodon.guacamole.net.auth.ConnectionRecord; @@ -43,8 +44,8 @@ public class ActiveConnectionMultimap { /** * All active connections to a connection having a given identifier. */ - private final Map> records = - new HashMap>(); + private final Map> records = + new HashMap>(); /** * Stores the given connection record in the list of active connections @@ -60,14 +61,14 @@ public class ActiveConnectionMultimap { synchronized (records) { // Get set of active connection records, creating if necessary - LinkedList connections = records.get(identifier); + Set connections = records.get(identifier); if (connections == null) { - connections = new LinkedList(); + connections = Collections.newSetFromMap(new LinkedHashMap()); records.put(identifier, connections); } // Add active connection - connections.addFirst(record); + connections.add(record); } } @@ -86,7 +87,7 @@ public class ActiveConnectionMultimap { synchronized (records) { // Get set of active connection records - LinkedList connections = records.get(identifier); + Set connections = records.get(identifier); assert(connections != null); // Remove old record @@ -100,25 +101,26 @@ public class ActiveConnectionMultimap { } /** - * Returns the current list of active connection records associated with - * the object having the given identifier. The list will be sorted in - * ascending order of connection age. If there are no such connections, an - * empty list is returned. + * Returns a collection of active connection records associated with the + * object having the given identifier. The collection will be sorted in + * insertion order. If there are no such connections, an empty collection is + * returned. * * @param identifier * The identifier of the object to check. * * @return - * An immutable list of records associated with the object having the - * given identifier, or an empty list if there are no such records. + * An immutable collection of records associated with the object having + * the given identifier, or an empty collection if there are no such + * records. */ - public List get(String identifier) { + public Collection get(String identifier) { synchronized (records) { // Get set of active connection records - LinkedList connections = records.get(identifier); + Collection connections = records.get(identifier); if (connections != null) - return Collections.unmodifiableList(connections); + return Collections.unmodifiableCollection(connections); return Collections.EMPTY_LIST; diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/GuacamoleSocketService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/GuacamoleSocketService.java index 31e240ba2..e8daf4653 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/GuacamoleSocketService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/socket/GuacamoleSocketService.java @@ -22,7 +22,7 @@ package org.glyptodon.guacamole.auth.jdbc.socket; -import java.util.List; +import java.util.Collection; import org.glyptodon.guacamole.auth.jdbc.user.AuthenticatedUser; import org.glyptodon.guacamole.auth.jdbc.connection.ModeledConnection; import org.glyptodon.guacamole.auth.jdbc.connectiongroup.ModeledConnectionGroup; @@ -72,18 +72,19 @@ public interface GuacamoleSocketService { throws GuacamoleException; /** - * Returns a list containing connection records representing all currently- - * active connections using the given connection. These records will have - * usernames and start dates, but no end date. + * Returns a connection containing connection records representing all + * currently-active connections using the given connection. These records + * will have usernames and start dates, but no end date, and will be + * sorted in ascending order by start date. * * @param connection * The connection to check. * * @return - * A list containing connection records representing all currently- - * active connections. + * A connection containing connection records representing all + * currently-active connections. */ - public List getActiveConnections(Connection connection); + public Collection getActiveConnections(Connection connection); /** * Creates a socket for the given user which connects to the given @@ -116,17 +117,18 @@ public interface GuacamoleSocketService { throws GuacamoleException; /** - * Returns a list containing connection records representing all currently- - * active connections using the given connection group. These records will - * have usernames and start dates, but no end date. + * Returns a collection containing connection records representing all + * currently-active connections using the given connection group. These + * records will have usernames and start dates, but no end date, and will + * be sorted in ascending order by start date. * * @param connectionGroup * The connection group to check. * * @return - * A list containing connection records representing all currently- - * active connections. + * A collection containing connection records representing all + * currently-active connections. */ - public List getActiveConnections(ConnectionGroup connectionGroup); + public Collection getActiveConnections(ConnectionGroup connectionGroup); }