This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-samples.git

commit 0b23d694a35a1241b473c06f4f6986b5c16ccd84
Author: Ian Luo <[email protected]>
AuthorDate: Wed Jun 5 16:19:16 2019 +0800

    introduce a maven plugin to detect dubbo local address to register
---
 dubbo-maven-address-plugin/pom.xml                 |  74 +++++++++++
 .../org/apache/dubbo/DubboLocalIpDetector.java     | 137 +++++++++++++++++++++
 .../java/org/apache/dubbo/LocalAddressMojo.java    |  49 ++++++++
 pom.xml                                            |   1 +
 4 files changed, 261 insertions(+)

diff --git a/dubbo-maven-address-plugin/pom.xml 
b/dubbo-maven-address-plugin/pom.xml
new file mode 100644
index 0000000..ba422cb
--- /dev/null
+++ b/dubbo-maven-address-plugin/pom.xml
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.apache.dubbo</groupId>
+    <version>1.0-SNAPSHOT</version>
+    <artifactId>dubbo-maven-address-plugin</artifactId>
+    <packaging>maven-plugin</packaging>
+
+    <properties>
+        <maven-plugin.version>3.6.0</maven-plugin.version>
+        <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
+        <source>1.8</source>
+        <target>1.8</target>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-plugin-api</artifactId>
+            <version>${maven-plugin.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven.plugin-tools</groupId>
+            <artifactId>maven-plugin-annotations</artifactId>
+            <version>${maven-plugin.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-core</artifactId>
+            <version>${maven-plugin.version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>${maven-compiler-plugin.version}</version>
+                <configuration>
+                    <source>${source}</source>
+                    <target>${target}</target>
+                </configuration>
+            </plugin>
+
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-plugin-plugin</artifactId>
+                <version>${maven-plugin.version}</version>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git 
a/dubbo-maven-address-plugin/src/main/java/org/apache/dubbo/DubboLocalIpDetector.java
 
b/dubbo-maven-address-plugin/src/main/java/org/apache/dubbo/DubboLocalIpDetector.java
new file mode 100644
index 0000000..4a5b466
--- /dev/null
+++ 
b/dubbo-maven-address-plugin/src/main/java/org/apache/dubbo/DubboLocalIpDetector.java
@@ -0,0 +1,137 @@
+/*
+ * 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.dubbo;
+
+import java.io.IOException;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.UnknownHostException;
+import java.util.Enumeration;
+import java.util.Optional;
+import java.util.regex.Pattern;
+
+public class DubboLocalIpDetector {
+    private static final Pattern IP_PATTERN = 
Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
+    private static final String ANYHOST_VALUE = "0.0.0.0";
+    private static final String LOCALHOST_VALUE = "127.0.0.1";
+
+    public static void main(String[] args) {
+        System.out.println(getAddress());
+    }
+
+    public static String getAddress() {
+        return getLocalAddress().getHostAddress();
+    }
+
+    private static InetAddress getLocalAddress() {
+        InetAddress localAddress = null;
+        try {
+            localAddress = InetAddress.getLocalHost();
+            Optional<InetAddress> addressOp = toValidAddress(localAddress);
+            if (addressOp.isPresent()) {
+                return addressOp.get();
+            }
+        } catch (Throwable e) {
+            e.printStackTrace();
+        }
+
+        try {
+            Enumeration<NetworkInterface> interfaces = 
NetworkInterface.getNetworkInterfaces();
+            if (null == interfaces) {
+                return localAddress;
+            }
+            while (interfaces.hasMoreElements()) {
+                try {
+                    NetworkInterface network = interfaces.nextElement();
+                    if (network.isLoopback() || network.isVirtual() || 
!network.isUp()) {
+                        continue;
+                    }
+                    Enumeration<InetAddress> addresses = 
network.getInetAddresses();
+                    while (addresses.hasMoreElements()) {
+                        try {
+                            Optional<InetAddress> addressOp = 
toValidAddress(addresses.nextElement());
+                            if (addressOp.isPresent()) {
+                                try {
+                                    if (addressOp.get().isReachable(100)) {
+                                        return addressOp.get();
+                                    }
+                                } catch (IOException e) {
+                                    // ignore
+                                }
+                            }
+                        } catch (Throwable e) {
+                            e.printStackTrace();
+                        }
+                    }
+                } catch (Throwable e) {
+                    e.printStackTrace();
+                }
+            }
+        } catch (Throwable e) {
+            e.printStackTrace();
+        }
+        return localAddress;
+    }
+
+    private static Optional<InetAddress> toValidAddress(InetAddress address) {
+        if (address instanceof Inet6Address) {
+            Inet6Address v6Address = (Inet6Address) address;
+            if (isPreferIPV6Address()) {
+                return Optional.ofNullable(normalizeV6Address(v6Address));
+            }
+        }
+        if (isValidV4Address(address)) {
+            return Optional.of(address);
+        }
+        return Optional.empty();
+    }
+
+    private static boolean isPreferIPV6Address() {
+        boolean preferIpv6 = 
Boolean.getBoolean("java.net.preferIPv6Addresses");
+        if (!preferIpv6) {
+            return false;
+        }
+        return false;
+    }
+
+    private static InetAddress normalizeV6Address(Inet6Address address) {
+        String addr = address.getHostAddress();
+        int i = addr.lastIndexOf('%');
+        if (i > 0) {
+            try {
+                return InetAddress.getByName(addr.substring(0, i) + '%' + 
address.getScopeId());
+            } catch (UnknownHostException e) {
+                e.printStackTrace();
+            }
+        }
+        return address;
+    }
+
+    private static boolean isValidV4Address(InetAddress address) {
+        if (address == null || address.isLoopbackAddress()) {
+            return false;
+        }
+        String name = address.getHostAddress();
+        boolean result = (name != null
+                && IP_PATTERN.matcher(name).matches()
+                && !ANYHOST_VALUE.equals(name)
+                && !LOCALHOST_VALUE.equals(name));
+        return result;
+    }
+}
diff --git 
a/dubbo-maven-address-plugin/src/main/java/org/apache/dubbo/LocalAddressMojo.java
 
b/dubbo-maven-address-plugin/src/main/java/org/apache/dubbo/LocalAddressMojo.java
new file mode 100644
index 0000000..faa48f5
--- /dev/null
+++ 
b/dubbo-maven-address-plugin/src/main/java/org/apache/dubbo/LocalAddressMojo.java
@@ -0,0 +1,49 @@
+/*
+ * 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.dubbo;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.project.MavenProject;
+
+@Mojo(name = "local-address")
+public class LocalAddressMojo extends AbstractMojo {
+    @Parameter(defaultValue = "${project}")
+    private MavenProject project;
+
+    @Parameter(name = "localAddress", defaultValue = "dubbo.local.address")
+    private String localAddress;
+
+
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        String address = DubboLocalIpDetector.getAddress();
+        getLog().info("set dubbo local address " + address + " to property '" 
+ localAddress + "'");
+        project.getProperties().setProperty(localAddress, 
DubboLocalIpDetector.getAddress());
+    }
+
+    public String getLocalAddress() {
+        return localAddress;
+    }
+
+    public void setLocalAddress(String localAddress) {
+        this.localAddress = localAddress;
+    }
+}
diff --git a/pom.xml b/pom.xml
index 8655a6a..5c25944 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,6 +25,7 @@
     <packaging>pom</packaging>
     <version>1.0-SNAPSHOT</version>
     <modules>
+        <module>dubbo-maven-address-plugin</module>
         <module>dubbo-samples-annotation</module>
         <module>dubbo-samples-api-client</module>
         <module>dubbo-samples-api</module>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to