diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index cbd4554df..f459b5d97 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -120,7 +120,7 @@ dependencies { annotationProcessor("org.projectlombok:lombok:1.18.46") // --- Book & Image Processing --- - val pdfium4jVersion = if (useLocalLibs) "+" else "0.14.0" + val pdfium4jVersion = if (useLocalLibs) "+" else "0.15.0" implementation("org.grimmory:pdfium4j:$pdfium4jVersion") runtimeOnly("org.grimmory:pdfium4j:$pdfium4jVersion:${pdfiumNativesClassifier()}") diff --git a/backend/src/main/java/org/booklore/nativelib/NativeLibraries.java b/backend/src/main/java/org/booklore/nativelib/NativeLibraries.java new file mode 100644 index 000000000..f23fe1305 --- /dev/null +++ b/backend/src/main/java/org/booklore/nativelib/NativeLibraries.java @@ -0,0 +1,155 @@ +package org.booklore.nativelib; + +import lombok.extern.slf4j.Slf4j; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * JVM-wide single source of truth for native-library availability. + * + *
Initialization is intentionally serialized via the holder idiom so native
+ * library loading is forced exactly once under the JVM class-init lock.
+ */
+@Slf4j
+public final class NativeLibraries {
+
+ public enum Library {
+ PDFIUM,
+ LIBARCHIVE,
+ EPUB4J_NATIVE
+ }
+
+ private static final Map Constructing this component eagerly triggers JVM-wide native loading
+ * through the serialized holder path once per process.
+ */
+@Component
+public class NativeLibraryManager {
+
+ private final NativeLibraries libs = NativeLibraries.get();
+
+ public boolean isPdfiumAvailable() {
+ return libs.isPdfiumAvailable();
+ }
+
+ public boolean isLibArchiveAvailable() {
+ return libs.isLibArchiveAvailable();
+ }
+
+ public boolean isEpubNativeAvailable() {
+ return libs.isEpubNativeAvailable();
+ }
+
+ public boolean isAvailable(NativeLibraries.Library library) {
+ return libs.isAvailable(library);
+ }
+}
diff --git a/backend/src/main/java/org/booklore/service/ArchiveService.java b/backend/src/main/java/org/booklore/service/ArchiveService.java
index 305278272..d03e645f4 100644
--- a/backend/src/main/java/org/booklore/service/ArchiveService.java
+++ b/backend/src/main/java/org/booklore/service/ArchiveService.java
@@ -3,9 +3,9 @@ package org.booklore.service;
import com.github.gotson.nightcompress.Archive;
import com.github.gotson.nightcompress.ArchiveEntry;
import com.github.gotson.nightcompress.LibArchiveException;
-import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.booklore.exception.ApiError;
+import org.booklore.nativelib.NativeLibraries;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
@@ -27,34 +27,15 @@ public class ArchiveService {
private final ReentrantLock[] lockStripes = IntStream.range(0, LOCK_STRIPE_COUNT)
.mapToObj(ignored -> new ReentrantLock())
.toArray(ReentrantLock[]::new);
- private volatile boolean available = safeCheckAvailable();
- private static boolean safeCheckAvailable() {
- try {
- return Archive.isAvailable();
- } catch (Throwable e) {
- return false;
- }
- }
+ // Route through the JVM-wide serialized native loader.
+ private final boolean available = NativeLibraries.get().isLibArchiveAvailable();
private ReentrantLock getFileLock(Path path) {
int hash = path.toAbsolutePath().normalize().toString().hashCode();
return lockStripes[Math.floorMod(hash, LOCK_STRIPE_COUNT)];
}
- @PostConstruct
- public void initLibArchive() {
- // Log the availability result that was already determined at construction
- // time. The @PostConstruct runs on the Spring startup thread which also
- // ensures the native library is fully loaded before any concurrent HTTP
- // request thread can call into it.
- if (available) {
- log.info("LibArchive loaded successfully");
- } else {
- log.error("LibArchive is not available – CBX/archive features will not work");
- }
- }
-
private void requireAvailable() throws IOException {
if (!available) {
throw new IOException("LibArchive is not available – cannot process archive");
@@ -62,7 +43,7 @@ public class ArchiveService {
}
public static boolean isAvailable() {
- return Archive.isAvailable();
+ return NativeLibraries.get().isLibArchiveAvailable();
}
public record Entry(String name, long size) {}
diff --git a/backend/src/test/java/org/booklore/test/EpubNativeAvailableCondition.java b/backend/src/test/java/org/booklore/test/EpubNativeAvailableCondition.java
new file mode 100644
index 000000000..6b53ad884
--- /dev/null
+++ b/backend/src/test/java/org/booklore/test/EpubNativeAvailableCondition.java
@@ -0,0 +1,26 @@
+package org.booklore.test;
+
+import org.booklore.nativelib.NativeLibraries;
+import org.junit.jupiter.api.extension.ConditionEvaluationResult;
+import org.junit.jupiter.api.extension.ExecutionCondition;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+/**
+ * Enables tests only when epub4j-native binaries are available on the current
+ * platform.
+ *
+ * Delegates to the JVM-wide native-library loader singleton.
+ */
+public class EpubNativeAvailableCondition implements ExecutionCondition {
+
+ private static final ConditionEvaluationResult ENABLED =
+ ConditionEvaluationResult.enabled("epub4j-native library is available");
+
+ private static final ConditionEvaluationResult DISABLED =
+ ConditionEvaluationResult.disabled("epub4j-native library not available on this platform");
+
+ @Override
+ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
+ return NativeLibraries.get().isEpubNativeAvailable() ? ENABLED : DISABLED;
+ }
+}
diff --git a/backend/src/test/java/org/booklore/test/PdfiumAvailableCondition.java b/backend/src/test/java/org/booklore/test/PdfiumAvailableCondition.java
index 70dd12615..f331b9979 100644
--- a/backend/src/test/java/org/booklore/test/PdfiumAvailableCondition.java
+++ b/backend/src/test/java/org/booklore/test/PdfiumAvailableCondition.java
@@ -1,31 +1,26 @@
package org.booklore.test;
-import org.grimmory.pdfium4j.PdfiumLibrary;
+import org.booklore.nativelib.NativeLibraries;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
+/**
+ * Enables tests only when PDFium native binaries are available on the current
+ * platform.
+ *
+ * Delegates to the JVM-wide native-library loader singleton.
+ */
public class PdfiumAvailableCondition implements ExecutionCondition {
private static final ConditionEvaluationResult ENABLED =
ConditionEvaluationResult.enabled("PDFium native library is available");
- private static final Boolean AVAILABLE = probe();
-
- private static Boolean probe() {
- try {
- PdfiumLibrary.initialize();
- return true;
- } catch (Exception | UnsatisfiedLinkError e) {
- return false;
- }
- }
+ private static final ConditionEvaluationResult DISABLED =
+ ConditionEvaluationResult.disabled("PDFium native library not available on this platform");
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
- if (Boolean.TRUE.equals(AVAILABLE)) {
- return ENABLED;
- }
- return ConditionEvaluationResult.disabled("PDFium native library not available on this platform");
+ return NativeLibraries.get().isPdfiumAvailable() ? ENABLED : DISABLED;
}
}