aboutsummaryrefslogtreecommitdiffstats
path: root/container-onnxruntime/src/main/java/ai/vespa/onnxruntime/OnnxBundleActivator.java
diff options
context:
space:
mode:
authorArne Juul <arnej@yahooinc.com>2022-12-01 21:35:01 +0000
committerArne Juul <arnej@yahooinc.com>2022-12-02 14:46:10 +0000
commit2348208f249f0f56eb73a4c4066422e6c88516d8 (patch)
treed6e1af4c63e2b993421469aefb31bb47f71d5f2c /container-onnxruntime/src/main/java/ai/vespa/onnxruntime/OnnxBundleActivator.java
parentd9ab932c59cf1d22f22c19b96619024a76aecc32 (diff)
rename to just "container-onnxruntime"
Diffstat (limited to 'container-onnxruntime/src/main/java/ai/vespa/onnxruntime/OnnxBundleActivator.java')
-rw-r--r--container-onnxruntime/src/main/java/ai/vespa/onnxruntime/OnnxBundleActivator.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/container-onnxruntime/src/main/java/ai/vespa/onnxruntime/OnnxBundleActivator.java b/container-onnxruntime/src/main/java/ai/vespa/onnxruntime/OnnxBundleActivator.java
new file mode 100644
index 00000000000..6995c28b124
--- /dev/null
+++ b/container-onnxruntime/src/main/java/ai/vespa/onnxruntime/OnnxBundleActivator.java
@@ -0,0 +1,52 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package ai.vespa.onnxruntime;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+import java.util.logging.Logger;
+
+/**
+ * @author arnej
+ * Loads native libraries when the bundle is activated.
+ * Use system properties to ensure onnxruntime won't try
+ * to load them itself.
+ **/
+public class OnnxBundleActivator implements BundleActivator {
+
+ private static final String SKIP_PREFIX = "onnxruntime.native.";
+ private static final String SKIP_SUFFIX = ".skip";
+ private static final String SKIP_VALUE = "true";
+ private static final String[] LIBRARY_NAMES = { "onnxruntime", "onnxruntime4j_jni" };
+ private static final Logger log = Logger.getLogger(OnnxBundleActivator.class.getName());
+
+ @Override
+ public void start(BundleContext ctx) {
+ for (String libName : LIBRARY_NAMES) {
+ String skipProp = SKIP_PREFIX + libName + SKIP_SUFFIX;
+ if (SKIP_VALUE.equals(System.getProperty(skipProp))) {
+ log.info("already loaded native library "+libName+", skipping");
+ } else {
+ System.setProperty(skipProp, SKIP_VALUE);
+ log.info("loading native library: "+libName);
+ try {
+ System.loadLibrary(libName);
+ log.fine("loaded native library OK: "+libName);
+ } catch (Exception e) {
+ log.warning("Could not load native library '"+libName+"' because: "+e.getMessage());
+ }
+ }
+ }
+ }
+
+ @Override
+ public void stop(BundleContext ctx) {
+ // not sure how to test that loading and unloading multiple times actually works,
+ // but this should in theory do the necessary thing.
+ for (String libName : LIBRARY_NAMES) {
+ String skipProp = SKIP_PREFIX + libName + SKIP_SUFFIX;
+ System.clearProperty(skipProp);
+ log.info("will unload native library: "+libName);
+ }
+ }
+}