This is an automated email from the ASF dual-hosted git repository. mblow pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/asterixdb.git
commit bc6fa47a60f7198f3148021268e41812ff76ec61 Author: Michael Blow <[email protected]> AuthorDate: Thu Apr 16 19:57:11 2020 -0400 [NO ISSUE][HYR][NET] Add utility method & tests to default on unspecified network port Change-Id: I7c76aabe07d8e3268d0e3aa4247fe86865f82356 Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/5846 Integration-Tests: Jenkins <[email protected]> Tested-by: Jenkins <[email protected]> Reviewed-by: Murtadha Hubail <[email protected]> --- .../java/org/apache/hyracks/util/NetworkUtil.java | 11 ++++++ .../org/apache/hyracks/util/NetworkUtilTest.java | 42 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/NetworkUtil.java b/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/NetworkUtil.java index 3f6e90c..1b56440 100644 --- a/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/NetworkUtil.java +++ b/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/NetworkUtil.java @@ -103,6 +103,17 @@ public class NetworkUtil { return hosts; } + public static String defaultPort(String maybeHostPort, int defaultPort) { + String encodedInput = encodeIPv6LiteralHost(maybeHostPort); + int lastColon = encodedInput.lastIndexOf(':'); + int closeBracket = encodedInput.lastIndexOf(']'); + if (lastColon > 0 && lastColon > closeBracket) { + return maybeHostPort; + } else { + return encodedInput + ":" + defaultPort; + } + } + public static String encodeIPv6LiteralHost(String hostname) { return InetAddressUtils.isIPv6Address(hostname) ? "[" + hostname + "]" : hostname; } diff --git a/hyracks-fullstack/hyracks/hyracks-util/src/test/java/org/apache/hyracks/util/NetworkUtilTest.java b/hyracks-fullstack/hyracks/hyracks-util/src/test/java/org/apache/hyracks/util/NetworkUtilTest.java new file mode 100644 index 0000000..c5d42c5 --- /dev/null +++ b/hyracks-fullstack/hyracks/hyracks-util/src/test/java/org/apache/hyracks/util/NetworkUtilTest.java @@ -0,0 +1,42 @@ +/* + * 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.hyracks.util; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Assert; +import org.junit.Test; + +public class NetworkUtilTest { + + private static final Logger LOGGER = LogManager.getLogger(); + + @Test + public void testDefaultPort() { + Assert.assertEquals("127.0.0.1:1234", NetworkUtil.defaultPort("127.0.0.1:1234", 9999)); + Assert.assertEquals("127.0.0.1:9999", NetworkUtil.defaultPort("127.0.0.1", 9999)); + Assert.assertEquals("[::1]:1234", NetworkUtil.defaultPort("[::1]:1234", 9999)); + Assert.assertEquals("[::1]:9999", NetworkUtil.defaultPort("::1", 9999)); + Assert.assertEquals("localhost.localdomain.local:9999", + NetworkUtil.defaultPort("localhost.localdomain.local", 9999)); + Assert.assertEquals("localhost.localdomain.local:1234", + NetworkUtil.defaultPort("localhost.localdomain.local:1234", 9999)); + + } +}
