Skip to content

Commit

Permalink
Remove code-duplication from NativeLibraryLoader
Browse files Browse the repository at this point in the history
Motivation:

NativeLibraryLoader has some code-duplication that can be removed.

Modifications:

Remove duplicated code and just use provided methods of PlatformDependent.

Result:

Less code duplication, fixes [netty#3756].
  • Loading branch information
normanmaurer committed Aug 8, 2017
1 parent db47812 commit bdb0a39
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;

/**
Expand All @@ -43,13 +42,10 @@ public final class NativeLibraryLoader {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(NativeLibraryLoader.class);

private static final String NATIVE_RESOURCE_HOME = "META-INF/native/";
private static final String OSNAME;
private static final File WORKDIR;
private static final boolean DELETE_NATIVE_LIB_AFTER_LOADING;

static {
OSNAME = SystemPropertyUtil.get("os.name", "").toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");

String workdir = SystemPropertyUtil.get("io.netty.native.workdir");
if (workdir != null) {
File f = new File(workdir);
Expand All @@ -64,101 +60,14 @@ public final class NativeLibraryLoader {
WORKDIR = f;
logger.debug("-Dio.netty.native.workdir: " + WORKDIR);
} else {
WORKDIR = tmpdir();
WORKDIR = PlatformDependent.tmpdir();
logger.debug("-Dio.netty.native.workdir: " + WORKDIR + " (io.netty.tmpdir)");
}

DELETE_NATIVE_LIB_AFTER_LOADING = SystemPropertyUtil.getBoolean(
"io.netty.native.deleteLibAfterLoading", true);
}

private static File tmpdir() {
File f;
try {
f = toDirectory(SystemPropertyUtil.get("io.netty.tmpdir"));
if (f != null) {
logger.debug("-Dio.netty.tmpdir: " + f);
return f;
}

f = toDirectory(SystemPropertyUtil.get("java.io.tmpdir"));
if (f != null) {
logger.debug("-Dio.netty.tmpdir: " + f + " (java.io.tmpdir)");
return f;
}

// This shouldn't happen, but just in case ..
if (isWindows()) {
f = toDirectory(System.getenv("TEMP"));
if (f != null) {
logger.debug("-Dio.netty.tmpdir: " + f + " (%TEMP%)");
return f;
}

String userprofile = System.getenv("USERPROFILE");
if (userprofile != null) {
f = toDirectory(userprofile + "\\AppData\\Local\\Temp");
if (f != null) {
logger.debug("-Dio.netty.tmpdir: " + f + " (%USERPROFILE%\\AppData\\Local\\Temp)");
return f;
}

f = toDirectory(userprofile + "\\Local Settings\\Temp");
if (f != null) {
logger.debug("-Dio.netty.tmpdir: " + f + " (%USERPROFILE%\\Local Settings\\Temp)");
return f;
}
}
} else {
f = toDirectory(System.getenv("TMPDIR"));
if (f != null) {
logger.debug("-Dio.netty.tmpdir: " + f + " ($TMPDIR)");
return f;
}
}
} catch (Exception ignored) {
// Environment variable inaccessible
}

// Last resort.
if (isWindows()) {
f = new File("C:\\Windows\\Temp");
} else {
f = new File("/tmp");
}

logger.warn("Failed to get the temporary directory; falling back to: " + f);
return f;
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private static File toDirectory(String path) {
if (path == null) {
return null;
}

File f = new File(path);
f.mkdirs();

if (!f.isDirectory()) {
return null;
}

try {
return f.getAbsoluteFile();
} catch (Exception ignored) {
return f;
}
}

private static boolean isWindows() {
return OSNAME.startsWith("windows");
}

private static boolean isOSX() {
return OSNAME.startsWith("macosx") || OSNAME.startsWith("osx");
}

/**
* Loads the first available library in the collection with the specified
* {@link ClassLoader}.
Expand Down Expand Up @@ -208,7 +117,7 @@ public static void load(String originalName, ClassLoader loader) {
String path = NATIVE_RESOURCE_HOME + libname;

URL url = loader.getResource(path);
if (url == null && isOSX()) {
if (url == null && PlatformDependent.isOsx()) {
if (path.endsWith(".jnilib")) {
url = loader.getResource(NATIVE_RESOURCE_HOME + "lib" + name + ".dynlib");
} else {
Expand Down
19 changes: 19 additions & 0 deletions common/src/main/java/io/netty/util/internal/PlatformDependent.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public final class PlatformDependent {
"\\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 MAYBE_SUPER_USER;

private static final boolean CAN_ENABLE_TCP_NODELAY_BY_DEFAULT = !isAndroid();
Expand Down Expand Up @@ -207,6 +209,13 @@ public static boolean isWindows() {
return IS_WINDOWS;
}

/**
* Return {@code true} if the JVM is running on OSX / MacOS
*/
public static boolean isOsx() {
return IS_OSX;
}

/**
* Return {@code true} if the current user may be a super-user. Be aware that this is just an hint and so it may
* return false-positives.
Expand Down Expand Up @@ -923,6 +932,16 @@ private static boolean isWindows0() {
return windows;
}

private static boolean isOsx0() {
String osname = SystemPropertyUtil.get("os.name", "").toLowerCase(Locale.US);
boolean osx = osname.startsWith("macosx") || osname.startsWith("osx");

if (osx) {
logger.debug("Platform: MacOS");
}
return osx;
}

private static boolean maybeSuperUser0() {
String username = SystemPropertyUtil.get("user.name");
if (isWindows()) {
Expand Down

0 comments on commit bdb0a39

Please sign in to comment.