Skip to content

Commit

Permalink
feat: add temp dir property for native library
Browse files Browse the repository at this point in the history
  • Loading branch information
equanz committed Dec 4, 2024
1 parent b372e90 commit 7131e51
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
public class NativeUtils {

public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.US);
public static final String TEMP_WORKDIR_PROPERTY_NAME = "org.apache.bookkeeper.native.workdir";

private static final String TEMP_DIR_NAME = "native";

/**
* loads given library from the this jar. ie: this jar contains: /lib/pulsar-checksum.jnilib
Expand All @@ -51,8 +54,21 @@ public static void loadLibraryFromJar(String path) throws Exception {
String[] parts = path.split("/");
String filename = (parts.length > 0) ? parts[parts.length - 1] : null;

Path dir = Files.createTempDirectory("native");
// create the temp dir
final Path dir;
final String tempWorkDirName = System.getProperty(TEMP_WORKDIR_PROPERTY_NAME);
if (tempWorkDirName == null || tempWorkDirName.isEmpty()) {
dir = Files.createTempDirectory(TEMP_DIR_NAME);
} else {
final File tempWorkDir = new File(tempWorkDirName);
if (!tempWorkDir.exists() || !tempWorkDir.isDirectory()) {
throw new FileNotFoundException("The tempWorkDir doesn't exist: " + tempWorkDirName);
}
dir = Files.createTempDirectory(tempWorkDir.toPath(), TEMP_DIR_NAME);
}
dir.toFile().deleteOnExit();

// create the temp file
File temp = new File(dir.toString(), filename);
temp.deleteOnExit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
*/
@UtilityClass
public class NativeUtils {

public static final String TEMP_WORKDIR_PROPERTY_NAME = "org.apache.bookkeeper.native.workdir";

private static final String TEMP_DIR_NAME = "native";

/**
* loads given library from the this jar. ie: this jar contains: /lib/pulsar-checksum.jnilib
*
Expand All @@ -56,8 +61,21 @@ public static void loadLibraryFromJar(String path) throws Exception {
String filename = parts[parts.length - 1];
checkArgument(path.startsWith("/"), "absolute path must start with /");

Path dir = Files.createTempDirectory("native");
// create the temp dir
final Path dir;
final String tempWorkDirName = System.getProperty(TEMP_WORKDIR_PROPERTY_NAME);
if (tempWorkDirName == null || tempWorkDirName.isEmpty()) {
dir = Files.createTempDirectory(TEMP_DIR_NAME);
} else {
final File tempWorkDir = new File(tempWorkDirName);
if (!tempWorkDir.exists() || !tempWorkDir.isDirectory()) {
throw new FileNotFoundException("The tempWorkDir doesn't exist: " + tempWorkDirName);
}
dir = Files.createTempDirectory(tempWorkDir.toPath(), TEMP_DIR_NAME);
}
dir.toFile().deleteOnExit();

// create the temp file
File temp = new File(dir.toString(), filename);
temp.deleteOnExit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import lombok.NonNull;
import lombok.experimental.UtilityClass;

Expand All @@ -33,6 +34,11 @@
*/
@UtilityClass
class NativeUtils {

public static final String TEMP_WORKDIR_PROPERTY_NAME = "org.apache.bookkeeper.native.workdir";

private static final String TEMP_DIR_NAME = "native";

/**
* loads given library from the this jar. ie: this jar contains: /lib/pulsar-checksum.jnilib
*
Expand All @@ -52,9 +58,22 @@ static void loadLibraryFromJar(String path) throws Exception {

String filename = parts[parts.length - 1];

File dir = Files.createTempDirectory("native").toFile();
dir.deleteOnExit();
File temp = new File(dir, filename);
// create the temp dir
final Path dir;
final String tempWorkDirName = System.getProperty(TEMP_WORKDIR_PROPERTY_NAME);
if (tempWorkDirName == null || tempWorkDirName.isEmpty()) {
dir = Files.createTempDirectory(TEMP_DIR_NAME);
} else {
final File tempWorkDir = new File(tempWorkDirName);
if (!tempWorkDir.exists() || !tempWorkDir.isDirectory()) {
throw new FileNotFoundException("The tempWorkDir doesn't exist: " + tempWorkDirName);
}
dir = Files.createTempDirectory(tempWorkDir.toPath(), TEMP_DIR_NAME);
}
dir.toFile().deleteOnExit();

// create the temp file
File temp = new File(dir.toString(), filename);
temp.deleteOnExit();

byte[] buffer = new byte[1024];
Expand Down

0 comments on commit 7131e51

Please sign in to comment.