Author: markt
Date: Thu Jun 27 21:32:39 2013
New Revision: 1497569

URL: http://svn.apache.org/r1497569
Log:
With an eye to the future, fix some thread-safety issues

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java

Modified: 
tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java?rev=1497569&r1=1497568&r2=1497569&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsServerContainer.java 
Thu Jun 27 21:32:39 2013
@@ -20,11 +20,11 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.EnumSet;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.WeakHashMap;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.servlet.DispatcherType;
 import javax.servlet.FilterRegistration;
@@ -88,10 +88,9 @@ public class WsServerContainer extends W
 
     private volatile ServletContext servletContext = null;
     private final Map<String,ServerEndpointConfig> configExactMatchMap =
-            new HashMap<>();
-    private final Map<Integer,SortedSet<TemplatePathMatch>>
-            configTemplateMatchMap = new HashMap<>();
-
+            new ConcurrentHashMap<>();
+    private final ConcurrentHashMap<Integer,SortedSet<TemplatePathMatch>>
+            configTemplateMatchMap = new ConcurrentHashMap<>();
 
     private WsServerContainer() {
         // Hide default constructor
@@ -231,9 +230,12 @@ public class WsServerContainer extends W
             SortedSet<TemplatePathMatch> templateMatches =
                     configTemplateMatchMap.get(key);
             if (templateMatches == null) {
+                // Ensure that if concurrent threads execute this block they
+                // both end up using the same TreeSet instance
                 templateMatches = new TreeSet<>(
                         TemplatePathMatchComparator.getInstance());
-                configTemplateMatchMap.put(key, templateMatches);
+                configTemplateMatchMap.putIfAbsent(key, templateMatches);
+                templateMatches = configTemplateMatchMap.get(key);
             }
             templateMatches.add(new TemplatePathMatch(sec, uriTemplate));
         } else {
@@ -245,6 +247,7 @@ public class WsServerContainer extends W
 
     public WsMappingResult findMapping(String path) {
 
+
         // Check an exact match. Simple case as there are no templates.
         ServerEndpointConfig sec = configExactMatchMap.get(path);
         if (sec != null) {



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to