diff options
author | Anna Koskinen <anna@vaadin.com> | 2012-12-07 14:14:31 +0200 |
---|---|---|
committer | Anna Koskinen <anna@vaadin.com> | 2012-12-07 14:14:31 +0200 |
commit | 1697a74ffb36e99a4e8659ade06d5b5553ab849e (patch) | |
tree | e46d235e93d085111257262dc3a77dbf61fd2aa1 /server/src/com/vaadin/ui/ConnectorTracker.java | |
parent | 2007f0d76511dd10125fe1de835437e6e40d126a (diff) | |
download | vaadin-framework-1697a74ffb36e99a4e8659ade06d5b5553ab849e.tar.gz vaadin-framework-1697a74ffb36e99a4e8659ade06d5b5553ab849e.zip |
Moved StreamVariable handling from AbstractCommunicationManager to
ConnectorTracker to prevent untimely unregistrations through other UIs
within the same session (#10112)
Change-Id: Id04c97970325be65b0b3c63756a2f2e731dd60d2
Diffstat (limited to 'server/src/com/vaadin/ui/ConnectorTracker.java')
-rw-r--r-- | server/src/com/vaadin/ui/ConnectorTracker.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/ConnectorTracker.java b/server/src/com/vaadin/ui/ConnectorTracker.java index 8b1a940c4b..91f9ac451c 100644 --- a/server/src/com/vaadin/ui/ConnectorTracker.java +++ b/server/src/com/vaadin/ui/ConnectorTracker.java @@ -23,6 +23,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -33,6 +34,7 @@ import com.vaadin.server.AbstractClientConnector; import com.vaadin.server.AbstractCommunicationManager; import com.vaadin.server.ClientConnector; import com.vaadin.server.GlobalResourceHandler; +import com.vaadin.server.StreamVariable; /** * A class which takes care of book keeping of {@link ClientConnector}s for a @@ -65,6 +67,11 @@ public class ConnectorTracker implements Serializable { private UI uI; private transient Map<ClientConnector, JSONObject> diffStates = new HashMap<ClientConnector, JSONObject>(); + /** Maps connectorIds to a map of named StreamVariables */ + private Map<String, Map<String, StreamVariable>> pidToNameToStreamVariable; + + private Map<StreamVariable, String> streamVariableToSeckey; + /** * Gets a logger for this class * @@ -259,6 +266,7 @@ public class ConnectorTracker implements Serializable { } } + cleanStreamVariables(); } /** @@ -497,4 +505,111 @@ public class ConnectorTracker implements Serializable { } } + + /** + * Checks if the indicated connector has a StreamVariable of the given name + * and returns the variable if one is found. + * + * @param connectorId + * @param variableName + * @return variable if a matching one exists, otherwise null + */ + public StreamVariable getStreamVariable(String connectorId, + String variableName) { + if (pidToNameToStreamVariable == null) { + return null; + } + Map<String, StreamVariable> map = pidToNameToStreamVariable + .get(connectorId); + if (map == null) { + return null; + } + StreamVariable streamVariable = map.get(variableName); + return streamVariable; + } + + /** + * Adds a StreamVariable of the given name to the indicated connector. + * + * @param connectorId + * @param variableName + * @param variable + */ + public void addStreamVariable(String connectorId, String variableName, + StreamVariable variable) { + if (pidToNameToStreamVariable == null) { + pidToNameToStreamVariable = new HashMap<String, Map<String, StreamVariable>>(); + } + Map<String, StreamVariable> nameToStreamVariable = pidToNameToStreamVariable + .get(connectorId); + if (nameToStreamVariable == null) { + nameToStreamVariable = new HashMap<String, StreamVariable>(); + pidToNameToStreamVariable.put(connectorId, nameToStreamVariable); + } + nameToStreamVariable.put(variableName, variable); + + if (streamVariableToSeckey == null) { + streamVariableToSeckey = new HashMap<StreamVariable, String>(); + } + String seckey = streamVariableToSeckey.get(variable); + if (seckey == null) { + seckey = UUID.randomUUID().toString(); + streamVariableToSeckey.put(variable, seckey); + } + } + + /** + * Removes StreamVariables that belong to connectors that are no longer + * attached to the session. + */ + private void cleanStreamVariables() { + if (pidToNameToStreamVariable != null) { + Iterator<String> iterator = pidToNameToStreamVariable.keySet() + .iterator(); + while (iterator.hasNext()) { + String connectorId = iterator.next(); + if (uI.getConnectorTracker().getConnector(connectorId) == null) { + // Owner is no longer attached to the session + Map<String, StreamVariable> removed = pidToNameToStreamVariable + .get(connectorId); + for (String key : removed.keySet()) { + streamVariableToSeckey.remove(removed.get(key)); + } + iterator.remove(); + } + } + } + } + + /** + * Removes any StreamVariable of the given name from the indicated + * connector. + * + * @param connectorId + * @param variableName + */ + public void cleanStreamVariable(String connectorId, String variableName) { + if (pidToNameToStreamVariable == null) { + return; + } + Map<String, StreamVariable> nameToStreamVar = pidToNameToStreamVariable + .get(connectorId); + nameToStreamVar.remove(variableName); + if (nameToStreamVar.isEmpty()) { + pidToNameToStreamVariable.remove(connectorId); + } + } + + /** + * Returns the security key associated with the given StreamVariable. + * + * @param variable + * @return matching security key if one exists, null otherwise + */ + public String getSeckey(StreamVariable variable) { + if (streamVariableToSeckey == null) { + return null; + } + return streamVariableToSeckey.get(variable); + } } |