Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
93 changes: 59 additions & 34 deletions src/main/java/software/amazon/awssdk/crt/CRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,74 @@ public final class CRT {

public static final String CRT_LIB_NAME = "aws-crt-jni";
public static final int AWS_CRT_SUCCESS = 0;
private static final CrtPlatform s_platform;
private static CrtPlatform s_platform;
private static final Throwable s_loadFailure;

static {
// Scan for and invoke any platform specific initialization
s_platform = findPlatformImpl();
jvmInit();
// The static initializer must never throw. If native load or init fails, capture
// the cause into s_loadFailure so callers (via checkLoaded()) can surface it as a
// catchable CrtRuntimeException, instead of triggering ExceptionInInitializerError
// and subsequent NoClassDefFoundError for every CRT type the customer touches.
Throwable loadFailure = null;
try {
// If the lib is already present/loaded or is in java.library.path, just use it
System.loadLibrary(CRT_LIB_NAME);
} catch (UnsatisfiedLinkError e) {
String graalVMImageCode = System.getProperty("org.graalvm.nativeimage.imagecode");
if (graalVMImageCode != null && graalVMImageCode == "runtime") {
throw new CrtRuntimeException(
"Failed to load '" + System.mapLibraryName(CRT_LIB_NAME) +
"'. Make sure this file is in the same directory as the GraalVM native image. ");
} else {
// otherwise, load from the jar this class is in
loadLibraryFromJar();
// Scan for and invoke any platform specific initialization
s_platform = findPlatformImpl();
jvmInit();
try {
// If the lib is already present/loaded or is in java.library.path, just use it
System.loadLibrary(CRT_LIB_NAME);
} catch (UnsatisfiedLinkError e) {
String graalVMImageCode = System.getProperty("org.graalvm.nativeimage.imagecode");
if (graalVMImageCode != null && graalVMImageCode == "runtime") {
throw new CrtRuntimeException(
"Failed to load '" + System.mapLibraryName(CRT_LIB_NAME) +
"'. Make sure this file is in the same directory as the GraalVM native image. ");
} else {
// otherwise, load from the jar this class is in
loadLibraryFromJar();
}
}
}

// Initialize the CRT
int memoryTracingLevel = 0;
try {
memoryTracingLevel = Integer.parseInt(System.getProperty("aws.crt.memory.tracing"));
} catch (Exception ex) {
}
boolean debugWait = System.getProperty("aws.crt.debugwait") != null;
boolean strictShutdown = System.getProperty("aws.crt.strictshutdown") != null;
awsCrtInit(memoryTracingLevel, debugWait, strictShutdown);
// Initialize the CRT
int memoryTracingLevel = 0;
try {
memoryTracingLevel = Integer.parseInt(System.getProperty("aws.crt.memory.tracing"));
} catch (Exception ex) {
}
boolean debugWait = System.getProperty("aws.crt.debugwait") != null;
boolean strictShutdown = System.getProperty("aws.crt.strictshutdown") != null;
awsCrtInit(memoryTracingLevel, debugWait, strictShutdown);

Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
Runtime.getRuntime().addShutdownHook(new Thread()
{
CRT.releaseShutdownRef();
public void run()
{
CRT.releaseShutdownRef();
}
});

try {
Log.initLoggingFromSystemProperties();
} catch (IllegalArgumentException e) {
;
}
});
} catch (Throwable t) {
loadFailure = t;
}
s_loadFailure = loadFailure;
}

try {
Log.initLoggingFromSystemProperties();
} catch (IllegalArgumentException e) {
;
/**
* Throws a {@link CrtRuntimeException} if the CRT native library failed to load or initialize.
* Otherwise returns normally. Customers can call this directly to probe availability, or rely on
* {@link CrtResource} subclasses (e.g. {@code S3Client}, {@code EventLoopGroup}) to invoke it
* during construction.
*/
public static void checkLoaded() {
if (s_loadFailure != null) {
CrtRuntimeException ex = new CrtRuntimeException("Failed to load AWS CRT native library");
ex.initCause(s_loadFailure);
throw ex;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/software/amazon/awssdk/crt/CrtResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public String toString() {
* Default constructor
*/
public CrtResource() {
// If the native lib failed to load, surface a single, catchable CrtRuntimeException
// before any subclass touches a native method (which would throw UnsatisfiedLinkError).
CRT.checkLoaded();

if (debugNativeObjects) {
String canonicalName = this.getClass().getCanonicalName();

Expand Down
Loading