diff options
author | Jakob Odersky <jodersky@gmail.com> | 2013-06-30 11:56:29 +0200 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2013-06-30 11:56:29 +0200 |
commit | 94a0ee545ab71c6779f15a5192745bedddfdb4d4 (patch) | |
tree | 208ed050a4e90490dac231d42981833f8b33b092 /flow-main | |
parent | 719978035732a55261b753bbc33570d3c1f53785 (diff) | |
download | akka-serial-94a0ee545ab71c6779f15a5192745bedddfdb4d4.tar.gz akka-serial-94a0ee545ab71c6779f15a5192745bedddfdb4d4.tar.bz2 akka-serial-94a0ee545ab71c6779f15a5192745bedddfdb4d4.zip |
solve cyclic dependency issue
Diffstat (limited to 'flow-main')
-rw-r--r-- | flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala b/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala index aebbe3f..23314f1 100644 --- a/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala +++ b/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala @@ -2,35 +2,37 @@ package com.github.jodersky.flow.internal import java.io.File import java.io.FileOutputStream +import scalax.file.Path +import scalax.io.Resource +import scala.util.Try /**Loads the current system's native library for flow. */ object NativeLoader { - def load = { + def extract(): Option[File] = { val os = System.getProperty("os.name").toLowerCase val arch = System.getProperty("os.arch").toLowerCase + val fqlib = System.mapLibraryName("flow") //fully qualified library name - val in = NativeLoader.getClass().getResourceAsStream("/native/" + os + "/" + arch + "/" + "libflow.so") - val temp = File.createTempFile("flow" + os + arch, ".so"); - temp.deleteOnExit() - val out = new FileOutputStream(temp); + val in = NativeLoader.getClass().getResourceAsStream(s"/native/${os}/${arch}/${fqlib}") + if (in == null) return None - try { - var read: Int = 0; ; - val buffer = new Array[Byte](4096); - do { - read = in.read(buffer) - if (read != -1) { - out.write(buffer, 0, read); - } - } while (read != -1) - } finally { - in.close() - out.close - } + val temp = Path.createTempFile() + Resource.fromInputStream(in).copyDataTo(temp) + temp.fileOption + } - System.load(temp.getAbsolutePath()) + def loadFromJar() = extract() match { + case Some(file) => System.load(file.getAbsolutePath) + case None => throw new UnsatisfiedLinkError("cannot extract native library, the native library may not exist for your specific system/architecture combination") + } + def load = { + try { + System.loadLibrary("flow") + } catch { + case ex: UnsatisfiedLinkError => loadFromJar() + } } }
\ No newline at end of file |