Repository: knox Updated Branches: refs/heads/master dcd25a056 -> 532baa049
KNOX-1114 - In case of port conflict log error and move on Project: http://git-wip-us.apache.org/repos/asf/knox/repo Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/532baa04 Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/532baa04 Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/532baa04 Branch: refs/heads/master Commit: 532baa04961174c978b5b7e14f70bd4d6b96874a Parents: dcd25a0 Author: Sandeep More <[email protected]> Authored: Thu May 3 16:52:30 2018 -0400 Committer: Sandeep More <[email protected]> Committed: Thu May 3 16:55:28 2018 -0400 ---------------------------------------------------------------------- .../apache/knox/gateway/GatewayMessages.java | 10 ++++++ .../org/apache/knox/gateway/GatewayServer.java | 35 +++++++++++--------- .../gateway/GatewayPortMappingConfigTest.java | 33 +++++++++++++++--- 3 files changed, 58 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/knox/blob/532baa04/gateway-server/src/main/java/org/apache/knox/gateway/GatewayMessages.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayMessages.java b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayMessages.java index 78a390a..e14668a 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayMessages.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayMessages.java @@ -506,6 +506,16 @@ public interface GatewayMessages { text = "Started gateway, topology \"{0}\" listening on port \"{1}\".") void startedGateway(final String topology, final int port); + /** + * Log topology and port + * + * @param topology + * @param port + */ + @Message(level = MessageLevel.ERROR, + text = "Topology \"{0}\" failed to start listening on port \"{1}\".") + void startedGatewayPortConflict(final String topology, final int port); + @Message(level = MessageLevel.ERROR, text = " Could not find topology \"{0}\" mapped to port \"{1}\" configured in gateway-config.xml. " http://git-wip-us.apache.org/repos/asf/knox/blob/532baa04/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java index 40a167d..8ebaff8 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayServer.java @@ -350,7 +350,7 @@ public class GatewayServer { * @throws NoSuchAlgorithmException * @throws KeyStoreException */ - private static Connector createConnector(final Server server, + private Connector createConnector(final Server server, final GatewayConfig config, final int port, final String topologyName) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException { @@ -481,7 +481,7 @@ public class GatewayServer { * @param config * @throws IOException */ - public static void checkPortConflict(final int port, + public void checkPortConflict(final int port, final String topologyName, final GatewayConfig config) throws IOException { @@ -508,20 +508,16 @@ public class GatewayServer { port)); } } else { - // Topology name is not blank so check amongst other ports if we have a conflict - for (final Map.Entry<String, Integer> entry : config - .getGatewayPortMappings().entrySet()) { - if (entry.getKey().equalsIgnoreCase(topologyName)) { - continue; - } - - if (entry.getValue() == port) { + /* check for port conflict */ + final Connector[] connectors = jetty.getConnectors(); + for (int i = 0; i < connectors.length; i++) { + if (connectors[i] instanceof ServerConnector + && ((ServerConnector) connectors[i]).getPort() == port) { log.portAlreadyInUse(port, topologyName); throw new IOException(String.format( - " Topologies %s and %s use the same port %d, ports for topologies (if defined) have to be unique. ", - entry.getKey(), topologyName, port)); + " Port %d used by topology %s is used by other topology, ports for topologies (if defined) have to be unique. ", + port, topologyName)); } - } } @@ -582,8 +578,17 @@ public class GatewayServer { // and NOT for Default Topology listening on standard gateway port. if(deployedTopologyList.contains(entry.getKey()) && (entry.getValue().intValue() != config.getGatewayPort()) ) { log.createJettyConnector(entry.getKey().toLowerCase(), entry.getValue()); - jetty.addConnector(createConnector(jetty, config, entry.getValue(), - entry.getKey().toLowerCase())); + try { + jetty.addConnector(createConnector(jetty, config, entry.getValue(), + entry.getKey().toLowerCase())); + } catch(final IOException e) { + /* in case of port conflict we log error and move on */ + if( e.toString().contains("ports for topologies (if defined) have to be unique.") ) { + log.startedGatewayPortConflict(entry.getKey().toLowerCase(), entry.getValue()); + } else { + throw e; + } + } } } } http://git-wip-us.apache.org/repos/asf/knox/blob/532baa04/gateway-server/src/test/java/org/apache/knox/gateway/GatewayPortMappingConfigTest.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/GatewayPortMappingConfigTest.java b/gateway-server/src/test/java/org/apache/knox/gateway/GatewayPortMappingConfigTest.java index 5e808d0..b7d69d5 100644 --- a/gateway-server/src/test/java/org/apache/knox/gateway/GatewayPortMappingConfigTest.java +++ b/gateway-server/src/test/java/org/apache/knox/gateway/GatewayPortMappingConfigTest.java @@ -19,6 +19,7 @@ package org.apache.knox.gateway; */ import org.apache.knox.gateway.config.GatewayConfig; +import org.apache.knox.gateway.config.impl.GatewayConfigImpl; import org.apache.knox.gateway.services.DefaultGatewayServices; import org.apache.knox.gateway.services.topology.TopologyService; import org.apache.velocity.VelocityContext; @@ -35,6 +36,8 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Collections; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; @@ -152,14 +155,33 @@ public class GatewayPortMappingConfigTest { * Test case where topologies "eerie" and "huron" use same ports. */ @Test - public void testCheckPortConflict() throws IOException { + public void testCheckPortConflict() + throws IOException, NoSuchFieldException, IllegalAccessException { /* Check port conflict with default port */ exception.expect(IOException.class); exception.expectMessage(String.format( - " Topologies %s and %s use the same port %d, ports for topologies (if defined) have to be unique. ", - "huron", "eerie", huronPort)); + " Port %d used by topology %s is used by other topology, ports for topologies (if defined) have to be unique. ", + huronPort, "eerie")); - GatewayServer.checkPortConflict(huronPort, "eerie", gatewayConfig); + GatewayServer gatewayServer = new GatewayServer(gatewayConfig); + + Server mockedJetty = EasyMock.createNiceMock(Server.class); + + ServerConnector mockConnector = EasyMock.createNiceMock(ServerConnector.class); + EasyMock.expect(mockConnector.getPort()).andReturn(huronPort).anyTimes(); + EasyMock.replay(mockConnector); + + ServerConnector[] mockConnectorArray = new ServerConnector[] {mockConnector}; + + EasyMock.expect(mockedJetty.getConnectors()).andReturn(mockConnectorArray).anyTimes(); + + EasyMock.replay(mockedJetty); + + Field field = gatewayServer.getClass().getDeclaredField("jetty"); + field.setAccessible(true); + field.set(gatewayServer, mockedJetty); + + gatewayServer.checkPortConflict(huronPort, "eerie", gatewayConfig); } @@ -176,7 +198,8 @@ public class GatewayPortMappingConfigTest { exception .expectMessage(String.format("Port %d already in use.", defaultPort)); - GatewayServer.checkPortConflict(defaultPort, null, gatewayConfig); + final GatewayServer gatewayServer = new GatewayServer(gatewayConfig); + gatewayServer.checkPortConflict(defaultPort, null, gatewayConfig); }
