From 867c8603a4dc3a677bbe7a152b55051e8ff4a280 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Tue, 1 Jul 2008 15:17:35 +0000
Subject: [PATCH] add: method loadNativeLibrary()

SVN: 6953
---
 .../cisd/common/utilities/FileUtilities.java  | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java
index c6fd50fb8a2..b3583c537f9 100644
--- a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java
+++ b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java
@@ -944,6 +944,36 @@ public final class FileUtilities
                 OSUtilities.getComputerPlatform(), libraryName), libraryName, ".so");
     }
 
+    /**
+     * Loads the native library <var>libraryName</var> from a Java resource that follows tha naming
+     * convention described in {@link #tryCopyNativeLibraryToTempFile(String)}.
+     * 
+     * @return <code>true</code> if the library has been loaded successfully and
+     *         <code>false</code> otherwise.
+     */
+    public final static boolean loadNativeLibraryFromResource(final String libraryName)
+    {
+        final String filename = FileUtilities.tryCopyNativeLibraryToTempFile(libraryName);
+
+        if (filename != null)
+        {
+            final File linkLib = new File(filename);
+            if (linkLib.exists() && linkLib.canRead() && linkLib.isFile())
+            {
+                try
+                {
+                    System.load(filename);
+                    return true;
+                } catch (final Throwable err)
+                {
+                    System.err.printf("Native library '%s' failed to load:\n", filename);
+                    err.printStackTrace();
+                }
+            }
+        }
+        return false;
+    }
+
     /**
      * Tries to copy the resource with the given name to a temporary file.
      * 
-- 
GitLab