Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeBinaryLoader: proper logging through JUL #60

Merged
merged 5 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/
package com.jme3.alloc.util;

import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import com.jme3.alloc.util.loader.NativeBinaryLoader;
Expand All @@ -44,7 +43,7 @@
public final class NativeBufferUtils {

static {
loadNativeBinary();
NativeBinaryLoader.quickLoadLibrary();
}

private NativeBufferUtils() {
Expand Down Expand Up @@ -116,16 +115,4 @@ private NativeBufferUtils() {
* @return a 32-bit or 64-bit integer (depending on the architecture) representing the memory address of the specified buffer
*/
public static native long getMemoryAdress(final ByteBuffer buffer);

private static void loadNativeBinary() {
if (!NativeBinaryLoader.isAutoLoad()) {
return;
}
try {
/* extracts and loads the system specific library */
NativeBinaryLoader.loadLibrary();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/
package com.jme3.alloc.util;

import java.io.IOException;
import com.jme3.alloc.util.loader.NativeBinaryLoader;

/**
Expand All @@ -42,12 +41,7 @@
public final class NativeErrno {

static {
try {
/* extracts and loads the system specific library if it's not been loaded before */
NativeBinaryLoader.loadLibrary();
} catch (IOException e) {
e.printStackTrace();
}
NativeBinaryLoader.quickLoadLibrary();
}
pavly-gerges marked this conversation as resolved.
Show resolved Hide resolved

private NativeErrno() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.UnsatisfiedLinkError;
import com.jme3.alloc.util.NativeBufferUtils;
import com.jme3.alloc.util.NativeErrno;

/**
* Helper utility for loading native binaries.
Expand All @@ -46,13 +49,30 @@
*/
public final class NativeBinaryLoader {

private static final Logger LOGGER = Logger.getLogger(NativeBinaryLoader.class.getName());
private static final ReentrantLock LOCK = new ReentrantLock();
private static final int EOF = -1;
private static boolean autoLoad = true;

private NativeBinaryLoader() {
}

/**
* Extracts and loads the variant specific binary from the output jar, handling the error messages,
* guarded by the {@link NativeBinaryLoader#isAutoLoad()}.
*/
public static void quickLoadLibrary() {
pavly-gerges marked this conversation as resolved.
Show resolved Hide resolved
if (!NativeBinaryLoader.isAutoLoad()) {
return;
}
try {
/* extracts and loads the system specific library */
NativeBinaryLoader.loadLibrary();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Binary Not Found!", e);
}
}

/**
* Extracts and loads the system and the architecture specific library from the output jar to the [user.dir].
*
Expand All @@ -75,18 +95,19 @@ public static void loadLibrary() throws IOException {
* Default value is [true].
*
* @param isAutoLoad true to auto-extract and load the native binary dynamically, false otherwise.
* @see NativeBufferUtils#loadNativeBinary()
* @see NativeBinaryLoader#quickLoadLibrary()
*/
public static void setAutoLoad(boolean isAutoLoad) {
NativeBinaryLoader.autoLoad = isAutoLoad;
}

/**
* Tests whether the native-binary will be auto-extracted and loaded when the
* class initializer of {@link NativeBufferUtils} is called. Default value is [true].
* class initializer of {@link NativeBufferUtils} or {@link NativeErrno} is called.
* Default value is [true].
*
* @return true if the native-binary is to be auto-extracted and loaded dynamically, false otherwise.
* @see NativeBufferUtils#loadNativeBinary()
* @see NativeBinaryLoader#quickLoadLibrary()
*/
public static boolean isAutoLoad() {
return autoLoad;
Expand Down