GUACAMOLE-38: Make connection ID incrementing thread-safe.

This commit is contained in:
Nick Couchman
2018-03-18 20:42:37 -04:00
parent a3d851228e
commit 8fa0cea793

View File

@@ -20,6 +20,7 @@
package org.apache.guacamole.auth.quickconnect; package org.apache.guacamole.auth.quickconnect;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.auth.quickconnect.utility.QCParser; import org.apache.guacamole.auth.quickconnect.utility.QCParser;
import org.apache.guacamole.net.auth.ConnectionGroup; import org.apache.guacamole.net.auth.ConnectionGroup;
@@ -46,7 +47,7 @@ public class QuickConnectDirectory extends SimpleConnectionDirectory {
/** /**
* The internal counter for connection IDs. * The internal counter for connection IDs.
*/ */
private int CONNECTION_ID = 0; private AtomicInteger connectionId;
/** /**
* Creates a new QuickConnectDirectory which provides access to the * Creates a new QuickConnectDirectory which provides access to the
@@ -61,6 +62,7 @@ public class QuickConnectDirectory extends SimpleConnectionDirectory {
public QuickConnectDirectory(Collection<Connection> connections, ConnectionGroup rootGroup) { public QuickConnectDirectory(Collection<Connection> connections, ConnectionGroup rootGroup) {
super(connections); super(connections);
this.rootGroup = (QuickConnectConnectionGroup)rootGroup; this.rootGroup = (QuickConnectConnectionGroup)rootGroup;
this.connectionId = new AtomicInteger();
} }
/** /**
@@ -70,8 +72,8 @@ public class QuickConnectDirectory extends SimpleConnectionDirectory {
* An Integer representing the next available connection * An Integer representing the next available connection
* ID to get used when adding connections. * ID to get used when adding connections.
*/ */
private Integer getNextConnectionID() { private int getNextConnectionID() {
return CONNECTION_ID++; return connectionId.getAndIncrement();
} }
@Override @Override
@@ -99,7 +101,7 @@ public class QuickConnectDirectory extends SimpleConnectionDirectory {
public String put(QuickConnection connection) throws GuacamoleException { public String put(QuickConnection connection) throws GuacamoleException {
// Get the next connection ID. // Get the next connection ID.
String connectionId = getNextConnectionID().toString(); String connectionId = Integer.toString(getNextConnectionID());
// Set up identifier and parent on object. // Set up identifier and parent on object.
connection.setIdentifier(connectionId); connection.setIdentifier(connectionId);
@@ -132,7 +134,7 @@ public class QuickConnectDirectory extends SimpleConnectionDirectory {
public String create(GuacamoleConfiguration config) throws GuacamoleException { public String create(GuacamoleConfiguration config) throws GuacamoleException {
// Get the next connection ID // Get the next connection ID
String connectionId = getNextConnectionID().toString(); String connectionId = Integer.toString(getNextConnectionID());
// Generate a name for the configuration // Generate a name for the configuration
String name = QCParser.getName(config); String name = QCParser.getName(config);