Repository: camel Updated Branches: refs/heads/camel-2.16.x 933e7f21e -> 3f2dcd403 refs/heads/camel-2.17.x 0a5ac8ce9 -> f043e472a refs/heads/master 5f7cf6271 -> c67d362fb
[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/c67d362f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c67d362f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c67d362f Branch: refs/heads/master Commit: c67d362fbb96f2143d6b180d425ab1c19ba8268b Parents: 5f7cf62 Author: James Netherton <jamesnether...@gmail.com> Authored: Wed Apr 13 11:48:11 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Apr 13 13:32:57 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/c67d362f/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 4641651..67f7107 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 @@ -242,11 +242,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)); } } @@ -254,18 +253,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/c67d362f/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"); + } + }; + } +}