diff --git a/build.gradle b/build.gradle index 8d8c283..09a9979 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,11 @@ plugins { id "io.codearte.nexus-staging" version "0.21.1" } +wrapper { + gradleVersion = "${rootProject.property('gradle.wrapper.version')}" + distributionType = Wrapper.DistributionType.ALL +} + ext { user = 'jprante' name = 'netty-http' diff --git a/gradle.properties b/gradle.properties index 6496f1c..c345224 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ group = org.xbib name = netty-http version = 4.1.52.1 -gradle.wrapper.version = 6.4.1 +gradle.wrapper.version = 6.6.1 netty.version = 4.1.52.Final tcnative.version = 2.0.34.Final bouncycastle.version = 1.66 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 62d4c05..e708b1c 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b42eb3a..33682bb 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Wed May 20 11:53:57 CEST 2020 -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index fbd7c51..4f906e0 100755 --- a/gradlew +++ b/gradlew @@ -130,7 +130,7 @@ fi if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - + JAVACMD=`cygpath --unix "$JAVACMD"` # We build the pattern for arguments to be converted via cygpath diff --git a/gradlew.bat b/gradlew.bat index a9f778a..ac1b06f 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init +if "%ERRORLEVEL%" == "0" goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -54,7 +54,7 @@ goto fail set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe -if exist "%JAVA_EXE%" goto init +if exist "%JAVA_EXE%" goto execute echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% @@ -64,21 +64,6 @@ echo location of your Java installation. goto fail -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - :execute @rem Setup the command line @@ -86,7 +71,7 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* :end @rem End local scope for the variables with windows NT shell diff --git a/netty-http-common/src/main/java/org/xbib/netty/http/common/NetworkUtils.java b/netty-http-common/src/main/java/org/xbib/netty/http/common/NetworkUtils.java index 7860943..1f01fff 100644 --- a/netty-http-common/src/main/java/org/xbib/netty/http/common/NetworkUtils.java +++ b/netty-http-common/src/main/java/org/xbib/netty/http/common/NetworkUtils.java @@ -19,6 +19,11 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.Logger; @@ -28,8 +33,6 @@ import java.util.logging.Logger; */ public class NetworkUtils { - private static final Logger logger = Logger.getLogger(NetworkUtils.class.getName()); - private static final String lf = System.lineSeparator(); private static final char[] hexDigit = new char[]{ @@ -40,14 +43,22 @@ public class NetworkUtils { private static final String IPV6_SETTING = "java.net.preferIPv6Addresses"; + private static final CountDownLatch latch = new CountDownLatch(1); + + private static final InterfaceWaiter interfaceWaiter = new InterfaceWaiter(); + private static InetAddress localAddress; + private NetworkUtils() { + throw new UnsupportedOperationException(); + } + public static void extendSystemProperties() { InetAddress address; try { address = InetAddress.getLocalHost(); } catch (Exception e) { - logger.log(Level.WARNING, e.getMessage(), e); + Logger.getLogger("network").log(Level.WARNING, e.getMessage(), e); address = InetAddress.getLoopbackAddress(); } localAddress = address; @@ -67,18 +78,14 @@ public class NetworkUtils { map.put("net." + networkInterface.getDisplayName(), inetAddress.getCanonicalHostName()); } } - logger.log(Level.FINE, "found network properties for system properties: " + map); for (Map.Entry entry : map.entrySet()) { System.setProperty(entry.getKey(), entry.getValue()); } } catch (Throwable e) { - logger.log(Level.WARNING, e.getMessage(), e); + Logger.getLogger("network").log(Level.WARNING, e.getMessage(), e); } } - private NetworkUtils() { - } - public static boolean isPreferIPv4() { return Boolean.getBoolean(System.getProperty(IPV4_SETTING)); } @@ -205,7 +212,15 @@ public class NetworkUtils { return false; } - public static InetAddress getFirstNonLoopbackAddress(NetworkProtocolVersion ipversion) { + public static List getAllNetworkInterfaces() throws InterruptedException { + return getInterfaces(n -> true); + } + + public static List getAllRunningAndUpInterfaces() throws InterruptedException { + return getInterfaces(NetworkUtils::isUp); + } + + public static InetAddress getFirstNonLoopbackAddress(NetworkProtocolVersion ipversion) throws InterruptedException { InetAddress address; for (NetworkInterface networkInterface : getAllNetworkInterfaces()) { try { @@ -213,7 +228,7 @@ public class NetworkUtils { continue; } } catch (Exception e) { - logger.log(Level.WARNING, e.getMessage(), e); + Logger.getLogger("network").log(Level.WARNING, e.getMessage(), e); continue; } address = getFirstNonLoopbackAddress(networkInterface, ipversion); @@ -268,7 +283,7 @@ public class NetworkUtils { return supportsVersion; } - public static NetworkProtocolVersion getProtocolVersion() { + public static NetworkProtocolVersion getProtocolVersion() throws InterruptedException { switch (findAvailableProtocols()) { case IPV4: return NetworkProtocolVersion.IPV4; @@ -288,7 +303,7 @@ public class NetworkUtils { return NetworkProtocolVersion.NONE; } - public static NetworkProtocolVersion findAvailableProtocols() { + public static NetworkProtocolVersion findAvailableProtocols() throws InterruptedException { boolean hasIPv4 = false; boolean hasIPv6 = false; for (InetAddress addr : getAllAvailableAddresses()) { @@ -311,7 +326,7 @@ public class NetworkUtils { return NetworkProtocolVersion.NONE; } - public static InetAddress resolveInetAddress(String hostname, String defaultValue) throws IOException { + public static InetAddress resolveInetAddress(String hostname, String defaultValue) throws IOException, InterruptedException { String host = hostname; if (host == null) { host = defaultValue; @@ -335,14 +350,15 @@ public class NetworkUtils { } } else { NetworkProtocolVersion networkProtocolVersion = getProtocolVersion(); + String reducedHost = host.substring(0, host.length() - 5); if (host.toLowerCase(Locale.ROOT).endsWith(":ipv4")) { networkProtocolVersion = NetworkProtocolVersion.IPV4; - host = host.substring(0, host.length() - 5); + host = reducedHost; } else if (host.toLowerCase(Locale.ROOT).endsWith(":ipv6")) { networkProtocolVersion = NetworkProtocolVersion.IPV6; - host = host.substring(0, host.length() - 5); + host = reducedHost; } - for (NetworkInterface ni : getInterfaces(NetworkUtils::isUp)) { + for (NetworkInterface ni : getAllRunningAndUpInterfaces()) { if (host.equals(ni.getName()) || host.equals(ni.getDisplayName())) { if (ni.isLoopback()) { return getFirstAddress(ni, networkProtocolVersion); @@ -357,7 +373,7 @@ public class NetworkUtils { return InetAddress.getByName(host); } - public static InetAddress resolvePublicHostAddress(String host) throws IOException { + public static InetAddress resolvePublicHostAddress(String host) throws IOException, InterruptedException { InetAddress address = resolveInetAddress(host, null); if (address == null || address.isAnyLocalAddress()) { address = getFirstNonLoopbackAddress(NetworkProtocolVersion.IPV4); @@ -374,24 +390,9 @@ public class NetworkUtils { return address; } - private static List getAllNetworkInterfaces() { - return getInterfaces(n -> true); - } - - public static List getAllRunningAndUpInterfaces() { - return getInterfaces(NetworkUtils::isUp); - } - - public static List getInterfaces(Predicate predicate) { + public static List getInterfaces(Predicate predicate) throws InterruptedException { List networkInterfaces = new ArrayList<>(); - Enumeration interfaces; - try { - interfaces = NetworkInterface.getNetworkInterfaces(); - } catch (Exception e) { - return networkInterfaces; - } - while (interfaces.hasMoreElements()) { - NetworkInterface networkInterface = interfaces.nextElement(); + for (NetworkInterface networkInterface : waitForNetworkInterfaces()) { if (predicate.test(networkInterface)) { networkInterfaces.add(networkInterface); Enumeration subInterfaces = networkInterface.getSubInterfaces(); @@ -404,7 +405,7 @@ public class NetworkUtils { return networkInterfaces; } - public static List getAllAvailableAddresses() { + public static List getAllAvailableAddresses() throws InterruptedException { List allAddresses = new ArrayList<>(); for (NetworkInterface networkInterface : getAllNetworkInterfaces()) { Enumeration addrs = networkInterface.getInetAddresses(); @@ -416,15 +417,61 @@ public class NetworkUtils { return allAddresses; } - public static String displayNetworkInterfaces() { + public static List waitForNetworkInterfaces() throws InterruptedException { + return waitForNetworkInterfaces(5L, TimeUnit.SECONDS); + } + + public static List waitForNetworkInterfaces(long period, TimeUnit timeUnit) throws InterruptedException { + if (latch.getCount() == 0L) { + return interfaceWaiter.interfaces; + } + ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); + ScheduledFuture future = service.scheduleAtFixedRate(interfaceWaiter, 0L, period, timeUnit); + latch.await(); + future.cancel(true); + service.shutdownNow(); + return interfaceWaiter.interfaces; + } + + private static class InterfaceWaiter implements Runnable { + + private final List interfaces = new ArrayList<>(); + + private final Logger logger = Logger.getLogger("network"); + + @Override + public void run() { + try { + interfaces.clear(); + logger.log(Level.INFO, "waiting for network interfaces"); + Enumeration networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); + if (networkInterfaceEnumeration.hasMoreElements()) { + do { + NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); + logger.log(Level.INFO, "found " + networkInterface.getDisplayName()); + interfaces.add(networkInterface); + } while (networkInterfaceEnumeration.hasMoreElements()); + logger.log(Level.INFO, "got network interfaces: " + interfaces.size()); + if (!interfaces.isEmpty()) { + latch.countDown(); + } + } + } catch (Exception e) { + // getNetworkInterfaces() throws socket exception if no network is configured + logger.log(Level.WARNING, e.getMessage()); + } + } + } + + public static String getNetworkInterfacesAsString() throws InterruptedException { StringBuilder sb = new StringBuilder(); for (NetworkInterface nic : getAllNetworkInterfaces()) { - sb.append(displayNetworkInterface(nic)); + sb.append(getNetworkInterfaceAsString(nic)); } return sb.toString(); } - public static String displayNetworkInterface(NetworkInterface nic) { + public static String getNetworkInterfaceAsString(NetworkInterface nic) { StringBuilder sb = new StringBuilder(); sb.append(lf).append(nic.getName()).append(lf); if (!nic.getName().equals(nic.getDisplayName())) { @@ -449,13 +496,13 @@ public class NetworkUtils { flags.add("VIRTUAL"); } } catch (Exception e) { - logger.log(Level.WARNING, e.getMessage(), e); + Logger.getLogger("network").log(Level.WARNING, e.getMessage(), e); } sb.append(String.join(",", flags)); try { sb.append(" mtu ").append(nic.getMTU()).append(lf); } catch (SocketException e) { - logger.log(Level.WARNING, e.getMessage(), e); + Logger.getLogger("network").log(Level.WARNING, e.getMessage(), e); } List addresses = nic.getInterfaceAddresses(); for (InterfaceAddress address : addresses) { @@ -474,7 +521,7 @@ public class NetworkUtils { sb.append(lf); } } catch (SocketException e) { - logger.log(Level.WARNING, e.getMessage(), e); + Logger.getLogger("network").log(Level.WARNING, e.getMessage(), e); } return sb.toString(); } @@ -505,7 +552,7 @@ public class NetworkUtils { try { sb.append(" netmask:").append(format(InetAddress.getByAddress(b))); } catch (UnknownHostException e) { - logger.log(Level.WARNING, e.getMessage(), e); + Logger.getLogger("network").log(Level.WARNING, e.getMessage(), e); } InetAddress broadcast = interfaceAddress.getBroadcast(); if (broadcast != null) { diff --git a/netty-http-common/src/test/java/org/xbib/netty/http/common/test/HttpParametersTest.java b/netty-http-common/src/test/java/org/xbib/netty/http/common/test/HttpParametersTest.java index 0a608e9..b7088dc 100644 --- a/netty-http-common/src/test/java/org/xbib/netty/http/common/test/HttpParametersTest.java +++ b/netty-http-common/src/test/java/org/xbib/netty/http/common/test/HttpParametersTest.java @@ -2,10 +2,8 @@ package org.xbib.netty.http.common.test; import org.junit.jupiter.api.Test; import org.xbib.netty.http.common.HttpParameters; - import java.nio.charset.MalformedInputException; import java.nio.charset.UnmappableCharacterException; - import static org.junit.jupiter.api.Assertions.assertEquals; class HttpParametersTest { diff --git a/netty-http-common/src/test/java/org/xbib/netty/http/common/test/NetworkUtilsTest.java b/netty-http-common/src/test/java/org/xbib/netty/http/common/test/NetworkUtilsTest.java new file mode 100644 index 0000000..49a310c --- /dev/null +++ b/netty-http-common/src/test/java/org/xbib/netty/http/common/test/NetworkUtilsTest.java @@ -0,0 +1,14 @@ +package org.xbib.netty.http.common.test; + +import org.junit.jupiter.api.Test; +import org.xbib.netty.http.common.NetworkUtils; +import java.util.logging.Level; +import java.util.logging.Logger; + +class NetworkUtilsTest { + + @Test + void testInterfaces() throws InterruptedException { + Logger.getLogger("test").log(Level.INFO, NetworkUtils.getNetworkInterfacesAsString()); + } +} diff --git a/netty-http-server/src/main/java/org/xbib/netty/http/server/Server.java b/netty-http-server/src/main/java/org/xbib/netty/http/server/Server.java index ac71400..0b50b2e 100644 --- a/netty-http-server/src/main/java/org/xbib/netty/http/server/Server.java +++ b/netty-http-server/src/main/java/org/xbib/netty/http/server/Server.java @@ -17,7 +17,6 @@ import io.netty.util.DomainWildcardMappingBuilder; import io.netty.util.Mapping; import org.xbib.net.URL; import org.xbib.netty.http.common.HttpAddress; -import org.xbib.netty.http.common.NetworkUtils; import org.xbib.netty.http.common.HttpChannelInitializer; import org.xbib.netty.http.common.TransportProvider; import org.xbib.netty.http.server.api.Domain; @@ -57,10 +56,6 @@ public final class Server implements AutoCloseable { private static final Logger logger = Logger.getLogger(Server.class.getName()); static { - // extend Java system properties by detected network interfaces - if (System.getProperty("xbib.netty.http.client.extendsystemproperties") != null) { - NetworkUtils.extendSystemProperties(); - } // change Netty defaults to safer ones, but still allow override from arg line if (System.getProperty("io.netty.noUnsafe") == null) { System.setProperty("io.netty.noUnsafe", Boolean.toString(true));