ptupitsyn commented on a change in pull request #8591:
URL: https://github.com/apache/ignite/pull/8591#discussion_r548241712



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+            }
+            host = addrStr.substring(1, hostEndIdx);
+            if (hostEndIdx == addrStr.length() - 1) { // no port specified, 
using default
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            } else { // port specified

Review comment:
       Code style: `else` should start on a new line

##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/util/HostAndPortRangeTest.java
##########
@@ -0,0 +1,170 @@
+/*
+ * 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.ignite.internal.util;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.apache.ignite.testframework.junits.common.GridCommonTest;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+/**
+ * Tests HostAndPortRange parse method.
+ */
+@GridCommonTest(group = "Utils")
+public class HostAndPortRangeTest extends GridCommonAbstractTest {
+

Review comment:
       Redundant blank line

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+            }
+            host = addrStr.substring(1, hostEndIdx);
+            if (hostEndIdx == addrStr.length() - 1) { // no port specified, 
using default
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            } else { // port specified
+                portStr = addrStr.substring(hostEndIdx + 2);
+
+                int[] ports = verifyPortStr(addrStr, errMsgPrefix, portStr);
+                portFrom = ports[0];
+                portTo = ports[1];
+            }
+        } else { //IPv4 || IPv6 without port || empty host

Review comment:
       Code style: add space after `//`

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+            }
+            host = addrStr.substring(1, hostEndIdx);
+            if (hostEndIdx == addrStr.length() - 1) { // no port specified, 
using default
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            } else { // port specified
+                portStr = addrStr.substring(hostEndIdx + 2);
+
+                int[] ports = verifyPortStr(addrStr, errMsgPrefix, portStr);
+                portFrom = ports[0];
+                portTo = ports[1];
+            }
+        } else { //IPv4 || IPv6 without port || empty host
+            final int colIdx = addrStr.lastIndexOf(':');
+            if (colIdx > 0) {
+                if (addrStr.substring(0, colIdx).contains(":")) { // IPv6 
without [] and port
+                    try {
+                        Inet6Address.getByName(addrStr);
+                        host = addrStr;
+                        portFrom = dfltPortFrom;
+                        portTo = dfltPortTo;
+                    }
+                    catch (UnknownHostException e) {
+                        throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+                    }
+                }
+                else {
+                    host = addrStr.substring(0, colIdx);
+                    portStr = addrStr.substring(colIdx + 1);
+                    int[] ports = verifyPortStr(addrStr, errMsgPrefix, 
portStr);
+                    portFrom = ports[0];
+                    portTo = ports[1];
+                }
+            } else if (colIdx == 0) {
+                throw createParseError(addrStr, errMsgPrefix, "Host name is 
empty");
+            } else { // Port is not specified, use defaults.
+                host = addrStr;
 
-            host = addrStr.substring(0, colIdx);
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            }
+        }
 
-            String portStr = addrStr.substring(colIdx + 1, addrStr.length());
+        return new HostAndPortRange(host, portFrom, portTo);
+    }
 
-            if (F.isEmpty(portStr))
-                throw createParseError(addrStr, errMsgPrefix, "port range is 
not specified");
+    /**
+     *

Review comment:
       Missing javadoc

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+            }
+            host = addrStr.substring(1, hostEndIdx);
+            if (hostEndIdx == addrStr.length() - 1) { // no port specified, 
using default
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            } else { // port specified
+                portStr = addrStr.substring(hostEndIdx + 2);
+
+                int[] ports = verifyPortStr(addrStr, errMsgPrefix, portStr);
+                portFrom = ports[0];
+                portTo = ports[1];
+            }
+        } else { //IPv4 || IPv6 without port || empty host
+            final int colIdx = addrStr.lastIndexOf(':');
+            if (colIdx > 0) {
+                if (addrStr.substring(0, colIdx).contains(":")) { // IPv6 
without [] and port
+                    try {
+                        Inet6Address.getByName(addrStr);
+                        host = addrStr;
+                        portFrom = dfltPortFrom;
+                        portTo = dfltPortTo;
+                    }
+                    catch (UnknownHostException e) {
+                        throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");

Review comment:
       Pass `e` as `cause`.

##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/util/HostAndPortRangeTest.java
##########
@@ -0,0 +1,170 @@
+/*
+ * 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.ignite.internal.util;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.apache.ignite.testframework.junits.common.GridCommonTest;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+/**
+ * Tests HostAndPortRange parse method.
+ */
+@GridCommonTest(group = "Utils")
+public class HostAndPortRangeTest extends GridCommonAbstractTest {
+
+    /**
+     * tests correct input address with IPv4 host and port range.

Review comment:
       Comments should start with a capital letter

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+            }
+            host = addrStr.substring(1, hostEndIdx);
+            if (hostEndIdx == addrStr.length() - 1) { // no port specified, 
using default
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            } else { // port specified
+                portStr = addrStr.substring(hostEndIdx + 2);
+
+                int[] ports = verifyPortStr(addrStr, errMsgPrefix, portStr);
+                portFrom = ports[0];
+                portTo = ports[1];
+            }
+        } else { //IPv4 || IPv6 without port || empty host
+            final int colIdx = addrStr.lastIndexOf(':');
+            if (colIdx > 0) {

Review comment:
       Code style: add blank line between semantic units

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");

Review comment:
       Let's make the error message more helpful, something along the lines of 
`Failed to parse IPv6 address, missing ']'`

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+            }

Review comment:
       Code style: add blank line after closing curly brace

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {

Review comment:
       Code style: remove curly braces for single-line statement
   https://cwiki.apache.org/confluence/display/IGNITE/Coding+Guidelines

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+            }
+            host = addrStr.substring(1, hostEndIdx);
+            if (hostEndIdx == addrStr.length() - 1) { // no port specified, 
using default
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            } else { // port specified
+                portStr = addrStr.substring(hostEndIdx + 2);
+
+                int[] ports = verifyPortStr(addrStr, errMsgPrefix, portStr);
+                portFrom = ports[0];
+                portTo = ports[1];
+            }
+        } else { //IPv4 || IPv6 without port || empty host
+            final int colIdx = addrStr.lastIndexOf(':');
+            if (colIdx > 0) {
+                if (addrStr.substring(0, colIdx).contains(":")) { // IPv6 
without [] and port

Review comment:
       Can be shorter and avoid extra allocation: `addrStr.lastIndexOf(":", 
colIdx)`

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +55,99 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
-
-        if (colIdx > 0) {
-            String portFromStr;
-            String portToStr;
+        if (addrStr.charAt(0) == '[') { // IPv6 with port(s)
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+            }
+            host = addrStr.substring(1, hostEndIdx);
+            if (hostEndIdx == addrStr.length() - 1) { // no port specified, 
using default
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            } else { // port specified
+                portStr = addrStr.substring(hostEndIdx + 2);
+
+                int[] ports = verifyPortStr(addrStr, errMsgPrefix, portStr);
+                portFrom = ports[0];
+                portTo = ports[1];
+            }
+        } else { //IPv4 || IPv6 without port || empty host
+            final int colIdx = addrStr.lastIndexOf(':');
+            if (colIdx > 0) {
+                if (addrStr.substring(0, colIdx).contains(":")) { // IPv6 
without [] and port
+                    try {
+                        Inet6Address.getByName(addrStr);
+                        host = addrStr;
+                        portFrom = dfltPortFrom;
+                        portTo = dfltPortTo;
+                    }
+                    catch (UnknownHostException e) {
+                        throw createParseError(addrStr, errMsgPrefix, "IPv6 is 
incorrect");
+                    }
+                }
+                else {
+                    host = addrStr.substring(0, colIdx);
+                    portStr = addrStr.substring(colIdx + 1);
+                    int[] ports = verifyPortStr(addrStr, errMsgPrefix, 
portStr);
+                    portFrom = ports[0];
+                    portTo = ports[1];
+                }
+            } else if (colIdx == 0) {
+                throw createParseError(addrStr, errMsgPrefix, "Host name is 
empty");
+            } else { // Port is not specified, use defaults.
+                host = addrStr;
 
-            host = addrStr.substring(0, colIdx);
+                portFrom = dfltPortFrom;
+                portTo = dfltPortTo;
+            }
+        }
 
-            String portStr = addrStr.substring(colIdx + 1, addrStr.length());
+        return new HostAndPortRange(host, portFrom, portTo);
+    }
 
-            if (F.isEmpty(portStr))
-                throw createParseError(addrStr, errMsgPrefix, "port range is 
not specified");
+    /**
+     *
+     * @param addrStr Address String.
+     * @param errMsgPrefix Error message prefix.
+     * @param portStr Port or port range string.
+     * @return Array of int[portFrom, portTo].
+     * @throws IgniteCheckedException If failed.
+     */
 
-            int portRangeIdx = portStr.indexOf("..");
+    private static int[] verifyPortStr(String addrStr, String errMsgPrefix, 
String portStr) throws IgniteCheckedException {
+        String portFromStr;
+        String portToStr;
 
-            if (portRangeIdx >= 0) {
-                // Port range is specified.
-                portFromStr = portStr.substring(0, portRangeIdx);
-                portToStr = portStr.substring(portRangeIdx + 2, 
portStr.length());
-            }
-            else {
-                // Single port is specified.
-                portFromStr = portStr;
-                portToStr = portStr;
-            }
+        if (F.isEmpty(portStr))
+            throw createParseError(addrStr, errMsgPrefix, "port range is not 
specified");
 
-            portFrom = parsePort(portFromStr, addrStr, errMsgPrefix);
-            portTo = parsePort(portToStr, addrStr, errMsgPrefix);
+        int portRangeIdx = portStr.indexOf("..");
 
-            if (portFrom > portTo)
-                throw createParseError(addrStr, errMsgPrefix, "start port 
cannot be less than end port");
+        if (portRangeIdx >= 0) {
+            // Port range is specified.
+            portFromStr = portStr.substring(0, portRangeIdx);
+            portToStr = portStr.substring(portRangeIdx + 2);
         }
         else {
-            // Host name not specified.
-            if (colIdx == 0)
-                throw createParseError(addrStr, errMsgPrefix, "Host name is 
empty");
-
-            // Port is not specified, use defaults.
-            host = addrStr;
-
-            portFrom = dfltPortFrom;
-            portTo = dfltPortTo;
+            // Single port is specified.
+            portFromStr = portStr;
+            portToStr = portStr;
         }
 
-        if (F.isEmpty(host))
-            throw createParseError(addrStr, errMsgPrefix, "Host name is 
empty");
+        int portFrom = parsePort(portFromStr, addrStr, errMsgPrefix);
+        int portTo = parsePort(portToStr, addrStr, errMsgPrefix);
 
-        return new HostAndPortRange(host, portFrom, portTo);
+        if (portFrom > portTo)
+            throw createParseError(addrStr, errMsgPrefix, "start port cannot 
be less than end port");
+
+        return new int[]{portFrom, portTo};

Review comment:
       Code style: add space after `[]`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to