Use PlatformDependent#normalizedOs() instead of reading os.name prop (#11239)

Motivation:

`PlatformDependent#normalizedOs()` already caches normalized variant of
the value of `os.name` system property. Instead of inconsistently
normalizing it in every case, use the utility method.

Modifications:

- `PlatformDependent`: `isWindows0()` and `isOsx0()` use `NORMALIZED_OS`;
- `PlatformDependent#normalizeOs(String)` define `darwin` as `osx`;
- `OpenSsl#loadTcNative()` does not require `equalsIgnoreCase` bcz `os`
is already normalized;
- Epoll and KQueue: `Native#loadNativeLibrary()` use `normalizedOs()`;
- Use consistent `Locale.US` for lower case conversion of `os.name`;
- `MacOSDnsServerAddressStreamProvider#loadNativeLibrary()` uses
`PlatformDependent.isOsx()`;

Result:

Consistent approach for `os.name` parsing.
This commit is contained in:
Idel Pivnitskiy 2021-05-10 23:52:29 -07:00 committed by GitHub
parent 0d009033eb
commit 3ecb32357c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 29 deletions

View File

@ -83,11 +83,6 @@ public final class PlatformDependent {
private static final Pattern MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN = Pattern.compile(
"\\s*-XX:MaxDirectMemorySize\\s*=\\s*([0-9]+)\\s*([kKmMgG]?)\\s*$");
private static final boolean IS_WINDOWS = isWindows0();
private static final boolean IS_OSX = isOsx0();
private static final boolean IS_J9_JVM = isJ9Jvm0();
private static final boolean IS_IVKVM_DOT_NET = isIkvmDotNet0();
private static final boolean MAYBE_SUPER_USER;
private static final boolean CAN_ENABLE_TCP_NODELAY_BY_DEFAULT = !isAndroid();
@ -112,6 +107,11 @@ public final class PlatformDependent {
private static final String[] ALLOWED_LINUX_OS_CLASSIFIERS = {"fedora", "suse", "arch"};
private static final Set<String> LINUX_OS_CLASSIFIERS;
private static final boolean IS_WINDOWS = isWindows0();
private static final boolean IS_OSX = isOsx0();
private static final boolean IS_J9_JVM = isJ9Jvm0();
private static final boolean IS_IVKVM_DOT_NET = isIkvmDotNet0();
private static final int ADDRESS_SIZE = addressSize0();
private static final boolean USE_DIRECT_BUFFER_NO_CLEANER;
private static final AtomicLong DIRECT_MEMORY_COUNTER;
@ -1058,7 +1058,7 @@ public final class PlatformDependent {
}
private static boolean isWindows0() {
boolean windows = SystemPropertyUtil.get("os.name", "").toLowerCase(Locale.US).contains("win");
boolean windows = "windows".equals(NORMALIZED_OS);
if (windows) {
logger.debug("Platform: Windows");
}
@ -1066,10 +1066,7 @@ public final class PlatformDependent {
}
private static boolean isOsx0() {
String osname = SystemPropertyUtil.get("os.name", "").toLowerCase(Locale.US)
.replaceAll("[^a-z0-9]+", "");
boolean osx = osname.startsWith("macosx") || osname.startsWith("osx");
boolean osx = "osx".equals(NORMALIZED_OS);
if (osx) {
logger.debug("Platform: MacOS");
}
@ -1517,7 +1514,7 @@ public final class PlatformDependent {
if (value.startsWith("linux")) {
return "linux";
}
if (value.startsWith("macosx") || value.startsWith("osx")) {
if (value.startsWith("macosx") || value.startsWith("osx") || value.startsWith("darwin")) {
return "osx";
}
if (value.startsWith("freebsd")) {

View File

@ -575,7 +575,7 @@ public final class OpenSsl {
// First, try loading the platform-specific library. Platform-specific
// libraries will be available if using a tcnative uber jar.
if ("linux".equalsIgnoreCase(os)) {
if ("linux".equals(os)) {
Set<String> classifiers = PlatformDependent.normalizedLinuxClassifiers();
for (String classifier : classifiers) {
libNames.add(staticLibName + "_" + os + '_' + arch + "_" + classifier);

View File

@ -23,7 +23,6 @@ import io.netty.util.internal.ClassInitializerUtil;
import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.ThrowableUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
@ -33,7 +32,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
@ -84,9 +82,8 @@ public final class MacOSDnsServerAddressStreamProvider implements DnsServerAddre
}
private static void loadNativeLibrary() {
String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim();
if (!name.startsWith("mac")) {
throw new IllegalStateException("Only supported on MacOS");
if (PlatformDependent.isOsx()) {
throw new IllegalStateException("Only supported on MacOS/OSX");
}
String staticLibName = "netty_resolver_dns_native_macos";
String sharedLibName = staticLibName + '_' + PlatformDependent.normalizedArch();

View File

@ -23,7 +23,6 @@ import org.junit.rules.TestName;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.XZOutputStream;
import javax.management.MBeanServer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -39,6 +38,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import javax.management.MBeanServer;
public final class TestUtils {
@ -73,7 +73,7 @@ public final class TestUtils {
*
*/
public static boolean isSctpSupported() {
String os = System.getProperty("os.name").toLowerCase(Locale.UK);
String os = System.getProperty("os.name").toLowerCase(Locale.US);
if ("unix".equals(os) || "linux".equals(os) || "sun".equals(os) || "solaris".equals(os)) {
try {
// Try to open a SCTP Channel, by using reflection to make it compile also on

View File

@ -23,7 +23,6 @@ import io.netty.channel.unix.Unix;
import io.netty.util.internal.ClassInitializerUtil;
import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.ThrowableUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
@ -31,7 +30,6 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.Selector;
import java.util.Locale;
import static io.netty.channel.epoll.NativeStaticallyReferencedJniMethods.epollerr;
import static io.netty.channel.epoll.NativeStaticallyReferencedJniMethods.epollet;
@ -296,8 +294,8 @@ public final class Native {
public static native int offsetofEpollData();
private static void loadNativeLibrary() {
String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim();
if (!name.startsWith("linux")) {
String name = PlatformDependent.normalizedOs();
if (!"linux".equals(name)) {
throw new IllegalStateException("Only supported on Linux");
}
String staticLibName = "netty_transport_native_epoll";

View File

@ -22,14 +22,12 @@ import io.netty.channel.unix.Unix;
import io.netty.util.internal.ClassInitializerUtil;
import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.ThrowableUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Locale;
import static io.netty.channel.kqueue.KQueueStaticallyReferencedJniMethods.evAdd;
import static io.netty.channel.kqueue.KQueueStaticallyReferencedJniMethods.evClear;
@ -135,9 +133,9 @@ final class Native {
static native int offsetofKeventData();
private static void loadNativeLibrary() {
String name = SystemPropertyUtil.get("os.name").toLowerCase(Locale.UK).trim();
if (!name.startsWith("mac") && !name.contains("bsd") && !name.startsWith("darwin")) {
throw new IllegalStateException("Only supported on BSD");
String name = PlatformDependent.normalizedOs();
if (!"osx".equals(name) && !name.contains("bsd")) {
throw new IllegalStateException("Only supported on OSX/BSD");
}
String staticLibName = "netty_transport_native_kqueue";
String sharedLibName = staticLibName + '_' + PlatformDependent.normalizedArch();

View File

@ -29,6 +29,7 @@ import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.IntBuffer;
import java.util.HashSet;
import java.util.Locale;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
@ -69,7 +70,7 @@ public final class UnitHelp {
* Measure ping time to a host.
*/
public static long ping(final String host) throws Exception {
final String name = System.getProperty("os.name").toLowerCase();
final String name = System.getProperty("os.name").toLowerCase(Locale.US);
final String command;
if (name.contains("linux")) {