You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/04/13 13:36:44 UTC

[3/3] camel git commit: [CAMEL-9862] Fix potential NPE in UndertowComponent.unregisterConsumer

[CAMEL-9862] Fix potential NPE in UndertowComponent.unregisterConsumer


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3f2dcd40
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3f2dcd40
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3f2dcd40

Branch: refs/heads/camel-2.16.x
Commit: 3f2dcd403a6f6ae327d88a8c12560b101c1dac9e
Parents: 933e7f2
Author: James Netherton <ja...@gmail.com>
Authored: Wed Apr 13 11:48:11 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Apr 13 13:36:33 2016 +0200

----------------------------------------------------------------------
 .../component/undertow/UndertowComponent.java   | 27 +++++-----
 .../UndertowConsumerUnregisterTest.java         | 54 ++++++++++++++++++++
 2 files changed, 68 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3f2dcd40/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
index 4538243..abfc950 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
@@ -188,11 +188,10 @@ public class UndertowComponent extends UriEndpointComponent implements RestConsu
     public void registerConsumer(UndertowConsumer consumer) {
         int port = consumer.getEndpoint().getHttpURI().getPort();
         if (serversRegistry.containsKey(port)) {
-            //server listens on port, we need add configuration for path
             UndertowRegistry undertowRegistry = serversRegistry.get(port);
             undertowRegistry.registerConsumer(consumer);
         } else {
-            //create new server to listen on specified port
+            // Create a new server to listen on the specified port
             serversRegistry.put(port, new UndertowRegistry(consumer, port));
         }
     }
@@ -200,18 +199,20 @@ public class UndertowComponent extends UriEndpointComponent implements RestConsu
     public void unregisterConsumer(UndertowConsumer consumer) {
         int port = consumer.getEndpoint().getHttpURI().getPort();
         if (serversRegistry.containsKey(port)) {
-            serversRegistry.get(port).unregisterConsumer(consumer);
-        }
-        if (serversRegistry.get(port).isEmpty()) {
-            //if there no Consumer left, we can shut down server
-            Undertow server = serversRegistry.get(port).getServer();
-            if (server != null) {
-                server.stop();
+            UndertowRegistry undertowRegistry = serversRegistry.get(port);
+            undertowRegistry.unregisterConsumer(consumer);
+
+            if (undertowRegistry.isEmpty()) {
+                // If there are no consumers left, we can shut down the server
+                Undertow server = undertowRegistry.getServer();
+                if (server != null) {
+                    server.stop();
+                }
+                serversRegistry.remove(port);
+            } else {
+                // Else, rebuild the server
+                startServer(consumer);
             }
-            serversRegistry.remove(port);
-        } else {
-            //call startServer to rebuild otherwise
-            startServer(consumer);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/3f2dcd40/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowConsumerUnregisterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowConsumerUnregisterTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowConsumerUnregisterTest.java
new file mode 100644
index 0000000..1d6c0a2
--- /dev/null
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowConsumerUnregisterTest.java
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.undertow;
+
+import java.net.ConnectException;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class UndertowConsumerUnregisterTest extends BaseUndertowTest {
+
+    @Test
+    public void testUnregisterUndertowConsumersForPort() throws Exception {
+        UndertowComponent component = context.getComponent("undertow", UndertowComponent.class);
+        UndertowConsumer consumerFoo = (UndertowConsumer) context.getRoute("route-foo").getConsumer();
+        UndertowConsumer consumerBar = (UndertowConsumer) context.getRoute("route-bar").getConsumer();
+
+        component.unregisterConsumer(consumerFoo);
+        component.unregisterConsumer(consumerBar);
+
+        try {
+            template.requestBody("undertow:http://localhost:{{port}}/foo", null, String.class);
+            fail("Expected exception when connecting to undertow endpoint");
+        } catch (CamelExecutionException e) {
+            // Expected because unregistering all consumers should shut down the Undertow server
+            assertTrue(e.getExchange().getException() instanceof ConnectException);
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("undertow:http://localhost:{{port}}/foo").id("route-foo").to("mock:foo");
+                from("undertow:http://localhost:{{port}}/bar").id("route-bar").to("mock:bar");
+            }
+        };
+    }
+}