Skip to content

Commit

Permalink
🦄 refactor: Revise OS utils for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 10, 2024
1 parent 54f404f commit ec909a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
27 changes: 2 additions & 25 deletions src/main/java/com/caoccao/javet/swc4j/Swc4jLibLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.caoccao.javet.swc4j.interfaces.ISwc4jLogger;
import com.caoccao.javet.swc4j.utils.ArrayUtils;
import com.caoccao.javet.swc4j.utils.OSUtils;
import com.caoccao.javet.swc4j.utils.StringUtils;
import com.caoccao.javet.swc4j.utils.Swc4jDefaultLogger;

import java.io.File;
Expand All @@ -34,10 +33,6 @@
* @since 0.1.0
*/
final class Swc4jLibLoader {
private static final String ANDROID_ABI_ARM = "armeabi-v7a";
private static final String ANDROID_ABI_ARM64 = "arm64-v8a";
private static final String ANDROID_ABI_X86 = "x86";
private static final String ANDROID_ABI_X86_64 = "x86_64";
private static final String ARCH_ARM = "arm";
private static final String ARCH_ARM64 = "arm64";
private static final String ARCH_X86 = "x86";
Expand All @@ -49,7 +44,6 @@ final class Swc4jLibLoader {
private static final String LIB_FILE_EXTENSION_MACOS = "dylib";
private static final String LIB_FILE_EXTENSION_WINDOWS = "dll";
private static final String LIB_FILE_NAME_FORMAT = "libswc4j-{0}-{1}.v.{2}.{3}";
private static final String LIB_FILE_NAME_PREFIX = "lib";
private static final String LIB_NAME = "swc4j";
private static final String LIB_VERSION = "0.1.0";
private static final ISwc4jLogger LOGGER = new Swc4jDefaultLogger(Swc4jLibLoader.class.getName());
Expand Down Expand Up @@ -105,21 +99,6 @@ private void deployLibFile(String resourceFileName, File libFile) {
}
}

private String getAndroidABI() {
if (OSUtils.IS_ANDROID) {
if (OSUtils.IS_ARM) {
return ANDROID_ABI_ARM;
} else if (OSUtils.IS_ARM64) {
return ANDROID_ABI_ARM64;
} else if (OSUtils.IS_X86) {
return ANDROID_ABI_X86;
} else if (OSUtils.IS_X86_64) {
return ANDROID_ABI_X86_64;
}
}
return null;
}

private String getFileExtension() {
if (OSUtils.IS_WINDOWS) {
return LIB_FILE_EXTENSION_WINDOWS;
Expand All @@ -136,7 +115,7 @@ private String getFileExtension() {
private String getLibFileName() throws Swc4jLibException {
String fileExtension = getFileExtension();
String osName = getOSName();
if (fileExtension == null || osName == null || OSUtils.IS_ANDROID) {
if (fileExtension == null || osName == null) {
throw Swc4jLibException.osNotSupported(OSUtils.OS_NAME);
}
String osArch = getOSArch();
Expand Down Expand Up @@ -186,9 +165,7 @@ private String getOSName() {
}

private String getResourceFileName() throws Swc4jLibException {
String resourceFileName = MessageFormat.format(RESOURCE_NAME_FORMAT, OSUtils.IS_ANDROID
? StringUtils.join("/", LIB_FILE_NAME_PREFIX, getAndroidABI(), getLibFileName())
: getLibFileName());
String resourceFileName = MessageFormat.format(RESOURCE_NAME_FORMAT, getLibFileName());
if (Swc4jNative.class.getResource(resourceFileName) == null) {
throw Swc4jLibException.libNotFound(resourceFileName);
}
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/com/caoccao/javet/swc4j/utils/OSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.caoccao.javet.swc4j.utils;

import java.lang.management.ManagementFactory;

/**
* The type OS utils.
*
Expand Down Expand Up @@ -45,17 +43,22 @@ public final class OSUtils {
public static final String WORKING_DIRECTORY = System.getProperty("user.dir");

static {
/* if defined ANDROID
PROCESS_ID = 1L;
/* end if */
/* if not defined ANDROID */
String processName = ManagementFactory.getRuntimeMXBean().getName();
int positionOfSeparator = processName.indexOf("@");
if (positionOfSeparator > 0) {
processName = processName.substring(0, positionOfSeparator);
long processId = 1L;
if (!IS_ANDROID) {
try {
Class<?> classManagementFactory = Class.forName("java.lang.management.ManagementFactory");
Class<?> classRuntimeMXBean = Class.forName("java.lang.management.RuntimeMXBean");
Object runtimeMXBean = classManagementFactory.getMethod("getRuntimeMXBean").invoke(null);
String processName = (String) classRuntimeMXBean.getMethod("getName").invoke(runtimeMXBean);
int positionOfSeparator = processName.indexOf("@");
if (positionOfSeparator > 0) {
processName = processName.substring(0, positionOfSeparator);
}
processId = Long.parseLong(processName);
} catch (Throwable ignore) {
}
}
PROCESS_ID = Long.parseLong(processName);
/* end if */
PROCESS_ID = processId;
}

private OSUtils() {
Expand Down

0 comments on commit ec909a9

Please sign in to comment.