aboutsummaryrefslogtreecommitdiff
path: root/flow/src
diff options
context:
space:
mode:
Diffstat (limited to 'flow/src')
-rw-r--r--flow/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java2
-rw-r--r--flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala40
2 files changed, 41 insertions, 1 deletions
diff --git a/flow/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java b/flow/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java
index 22306e0..c159058 100644
--- a/flow/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java
+++ b/flow/src/main/java/com/github/jodersky/flow/internal/NativeSerial.java
@@ -25,7 +25,7 @@ import com.github.jodersky.flow.PortInterruptedException;
final class NativeSerial {
static {
- System.loadLibrary("flow3");
+ NativeLoader.load("flow3");
}
final static int PARITY_NONE = 0;
diff --git a/flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala b/flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala
new file mode 100644
index 0000000..8fe1e3b
--- /dev/null
+++ b/flow/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala
@@ -0,0 +1,40 @@
+package com.github.jodersky.flow.internal
+
+import java.io.File
+import scalax.file.Path
+import scalax.io.Resource
+import java.security.CodeSource
+import java.util.zip.ZipEntry
+import java.util.zip.ZipInputStream
+import java.net.URL
+
+/** Handles loading of the current platform's native library for flow. */
+object NativeLoader {
+
+ private def os = System.getProperty("os.name").toLowerCase.filter(_ != ' ')
+
+ private def arch = System.getProperty("os.arch").toLowerCase
+
+ private def extractNative(nativeLibrary: String): Option[File] = {
+ val fqlib = System.mapLibraryName(nativeLibrary) //fully qualified library name
+
+ val in = NativeLoader.getClass().getResourceAsStream(s"/native/${os}-${arch}/${fqlib}")
+ if (in == null) return None
+
+ val temp = Path.createTempFile(nativeLibrary)
+ Resource.fromInputStream(in).copyDataTo(temp)
+ temp.fileOption
+ }
+
+ private def loadFromJar(nativeLibrary: String) = extractNative(nativeLibrary) match {
+ case Some(file) => System.load(file.getAbsolutePath)
+ case None => throw new UnsatisfiedLinkError("Cannot extract flow's native library, the native library may not exist for your specific architecture/OS combination.")
+ }
+
+ def load(nativeLibrary: String) = try {
+ System.loadLibrary(nativeLibrary)
+ } catch {
+ case ex: UnsatisfiedLinkError => loadFromJar(nativeLibrary)
+ }
+
+} \ No newline at end of file