You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2020/06/16 05:24:18 UTC

[GitHub] [incubator-dolphinscheduler] CalvinKirs commented on a change in pull request #2924: In the case of multiple network cards, a valid host is returned

CalvinKirs commented on a change in pull request #2924:
URL: https://github.com/apache/incubator-dolphinscheduler/pull/2924#discussion_r440593109



##########
File path: dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.dolphinscheduler.common.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.*;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+import java.util.regex.Pattern;
+
+import static java.util.Collections.emptyList;
+
+/**
+ * NetUtils
+ */
+public class NetUtils {
+
+    private static Logger logger = LoggerFactory.getLogger(NetUtils.class);
+
+
+    private static final Pattern ADDRESS_PATTERN = Pattern.compile("^\\d{1,3}(\\.\\d{1,3}){3}\\:\\d{1,5}$");
+
+    private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$");
+
+    private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
+
+    private static String ANY_HOST_VALUE = "0.0.0.0";
+
+    private static String LOCALHOST_KEY = "localhost";
+
+    private static String LOCALHOST_VALUE = "127.0.0.1";
+
+    private static  InetAddress LOCAL_ADDRESS = null;
+
+    private static volatile String HOST_ADDRESS;
+
+    public static String getHost() {
+        if (HOST_ADDRESS != null) {
+            return HOST_ADDRESS;
+        }
+
+        InetAddress address = getLocalAddress();
+        if (address != null) {
+            return HOST_ADDRESS = address.getHostAddress();
+        }
+        return LOCALHOST_VALUE;
+    }
+
+    /**
+     * Find first valid IP from local network card
+     *
+     * @return first valid local IP
+     */
+    public static synchronized InetAddress getLocalAddress() {
+
+        if (null != LOCAL_ADDRESS) {
+            return LOCAL_ADDRESS;

Review comment:
       > what's purpose of this variable LOCAL_ADDRESS ? never assigned a value
   
   Sorry, this is my oversight, I have completed the change, please review it




----------------------------------------------------------------
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:
users@infra.apache.org