[ 
https://issues.apache.org/jira/browse/CAMEL-12005?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16288961#comment-16288961
 ] 

ASF GitHub Bot commented on CAMEL-12005:
----------------------------------------

davsclaus commented on a change in pull request #2144: CAMEL-12005: Add 
websocket support to camel-undertow
URL: https://github.com/apache/camel/pull/2144#discussion_r156601922
 
 

 ##########
 File path: 
components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/CamelMethodHandler.java
 ##########
 @@ -16,76 +16,127 @@
  */
 package org.apache.camel.component.undertow.handlers;
 
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import io.undertow.server.HttpHandler;
 import io.undertow.server.HttpServerExchange;
 import io.undertow.util.MimeMappings;
 import io.undertow.util.StatusCodes;
+
 import org.apache.camel.component.undertow.ExchangeHeaders;
 
 /**
  * A HttpHandler build a mapping between HTTP methods and handlers and 
dispatch requests along the map.
  */
 public class CamelMethodHandler implements HttpHandler {
-    private Map<String, HttpHandler> methodMap = new HashMap<String, 
HttpHandler>();
-    private HttpHandler defaultHandler;
+    /**
+     * A key to use for handlers with no method specified
+     */
+    private static final String DEFAULT_HANDLER_KEY = "";
+    private static final String[] DEFAULT_METHODS;
+    static {
+        DEFAULT_METHODS = new String[] {DEFAULT_HANDLER_KEY};
+    }
+
+    private final Map<String, MethodEntry> methodMap = new 
ConcurrentHashMap<>();
     private String handlerString;
 
+    CamelMethodHandler() {
+    }
+
     @Override
     public void handleRequest(HttpServerExchange exchange) throws Exception {
-        HttpHandler handler = 
methodMap.get(exchange.getRequestMethod().toString());
-        if (handler != null) {
+        HttpHandler handler = null;
+        /* No need to lock methodMap for read access in this method */
+        MethodEntry entry = 
methodMap.get(exchange.getRequestMethod().toString());
+        if (entry != null && (handler = entry.handler) != null) {
             handler.handleRequest(exchange);
-        } else if (defaultHandler != null) {
-            defaultHandler.handleRequest(exchange);
         } else {
-            exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
-            exchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_TYPE, 
MimeMappings.DEFAULT_MIME_MAPPINGS.get("txt"));
-            exchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_LENGTH, 
0);
-            exchange.endExchange();
+            entry = methodMap.get(DEFAULT_HANDLER_KEY);
+            if (entry != null && (handler = entry.handler) != null) {
+                handler.handleRequest(exchange);
+            } else {
+                exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
+                
exchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_TYPE, 
MimeMappings.DEFAULT_MIME_MAPPINGS.get("txt"));
+                
exchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_LENGTH, 0);
+                exchange.endExchange();
+            }
         }
     }
 
-    public synchronized void add(String[] methods, HttpHandler handler) {
-        Map<String, HttpHandler> adding = new HashMap<String, HttpHandler>();
-        for (String method : methods) {
-            adding.put(method, handler);
+    public HttpHandler add(String methods, HttpHandler handler) {
+        HttpHandler result = null;
+        synchronized (methodMap) { // we lock on methodMap to get a reliable 
sum of refCounts in remove(String)
+            for (String method : splitMethods(methods)) {
+                MethodEntry en = methodMap.computeIfAbsent(method, m -> new 
MethodEntry());
+                result = en.addRef(handler, method);
+            }
         }
-        methodMap.putAll(adding);
         handlerString = null;
+        return result;
     }
 
-    public synchronized void remove(String[] methods) {
-        for (String method : methods) {
-            methodMap.remove(method);
+
+    public boolean remove(String methods) {
+        boolean result;
+        synchronized (methodMap) { // we lock on methodMap to get a reliable 
sum of refCounts
+            for (String method : splitMethods(methods)) {
+                final MethodEntry en = methodMap.get(method);
+                if (en != null) {
+                    en.removeRef();
+                }
+            }
+            result = methodMap.values().stream().mapToInt(en -> 
en.refCount).sum() == 0;
         }
         handlerString = null;
+        return result;
     }
 
-    public synchronized void addDefault(HttpHandler handler) {
-        if (defaultHandler != null) {
-            throw new IllegalArgumentException(String.format(
-                "Duplicate default handler: '%s', '%s'", defaultHandler, 
handler));
+    public String toString() {
+        if (handlerString == null) {
+            handlerString = "CamelMethodHandler[" + methodMap + "]";
         }
-        defaultHandler = handler;
-        handlerString = null;
+        return handlerString;
     }
 
-    public synchronized void removeDefault() {
-        defaultHandler = null;
-        handlerString = null;
+    private String[] splitMethods(String methods) {
+        String[] result = methods != null ? methods.split(",") : 
DEFAULT_METHODS;
+        return result.length == 0 ? DEFAULT_METHODS : result;
     }
 
-    public boolean isEmpty() {
-        return defaultHandler == null && methodMap.isEmpty();
-    }
+    static class MethodEntry {
+        /**
+         * The number of references pointing to {@link #handler}
+         */
+        private int refCount;
 
 Review comment:
   Add a bit of empty lines so this code is formatted nice between fields, 
constructor, and methods

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Add websocket support to camel-undertow
> ---------------------------------------
>
>                 Key: CAMEL-12005
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12005
>             Project: Camel
>          Issue Type: New Feature
>            Reporter: Thomas Diesler
>            Assignee: Peter Palaga
>
> CrossRef: https://github.com/wildfly-extras/wildfly-camel/issues/2298



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to