From e2505d1d9e2e49554057a8cd5fb71b0ac0e3ba63 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sun, 6 Dec 2015 17:03:08 -0800 Subject: Use separate project for native libraries --- .../src/main/scala/ch/jodersky/jni/NativeLoader.scala | 19 +++++++++++-------- .../src/main/scala/ch/jodersky/jni/Platform.scala | 6 ++++-- 2 files changed, 15 insertions(+), 10 deletions(-) (limited to 'jni-library/src/main/scala/ch/jodersky/jni') diff --git a/jni-library/src/main/scala/ch/jodersky/jni/NativeLoader.scala b/jni-library/src/main/scala/ch/jodersky/jni/NativeLoader.scala index 46d4b2d..261e52b 100644 --- a/jni-library/src/main/scala/ch/jodersky/jni/NativeLoader.scala +++ b/jni-library/src/main/scala/ch/jodersky/jni/NativeLoader.scala @@ -1,11 +1,10 @@ package ch.jodersky.jni import java.io.{File, FileOutputStream, InputStream, OutputStream} -import scala.io.Source /** - * Provides enhanced native library loading functionality. - */ + * Provides enhanced native library loading functionality. + */ object NativeLoader { /** Name of the shared library file that is contained in a jar. */ @@ -13,7 +12,7 @@ object NativeLoader { final val BufferSize = 4096 - /** Extract a resource from this class loader to a temporary file. */ + /** Extracts a resource from this class loader to a temporary file. */ private def extract(path: String): Option[File] = { var in: Option[InputStream] = None var out: Option[OutputStream] = None @@ -45,12 +44,16 @@ object NativeLoader { msg ) + /** + * Gets the absolute, full path of a resource on the classpath, given a libraryPath + * and platform. + */ def fullLibraryPath(libraryPath: String, platform: Platform) = { - libraryPath + "/native/" + platform.id + "/" + LibraryName + libraryPath + "/native/" + platform.id + "/" + LibraryName } private def loadFromJar(libraryPath: String): Unit = { - val platform = Platform.current.getOrElse{ + val platform = Platform.current.getOrElse { loadError("Cannot determine current platform.") } @@ -63,10 +66,10 @@ object NativeLoader { } /** - * Load a native library from the available library path or fall back + * Loads a native library from the available library path or fall back * to extracting and loading a native library from available resources. */ - def load(library: String, libraryPath: String): Unit = try { + def load(libraryPath: String, library: String): Unit = try { System.loadLibrary(library) } catch { case ex: UnsatisfiedLinkError => loadFromJar(libraryPath) diff --git a/jni-library/src/main/scala/ch/jodersky/jni/Platform.scala b/jni-library/src/main/scala/ch/jodersky/jni/Platform.scala index db1662d..cff9e95 100644 --- a/jni-library/src/main/scala/ch/jodersky/jni/Platform.scala +++ b/jni-library/src/main/scala/ch/jodersky/jni/Platform.scala @@ -20,15 +20,16 @@ case class Platform private (arch: String, kernel: String) { object Platform { + /** The unknown platform. */ final val Unknown = Platform("unknown", "unknown") - /** Create a platform with spaces stripped and case normalized. */ + /** Creates a platform with spaces stripped and case normalized. */ def normalized(arch: String, kernel: String): Platform = { def normalize(str: String) = str.toLowerCase.filter(!_.isWhitespace) Platform(normalize(arch), normalize(kernel)) } - /** Run 'uname' to determine current platform. Returns None if uname does not exist. */ + /** Runs 'uname' to determine current platform. Returns None if uname does not exist. */ def uname: Option[Platform] = { val lineOpt = try { Some(Process("uname -sm").lines.head) @@ -45,6 +46,7 @@ object Platform { } } + /** Determines platform the current JVM is running on. */ def current = uname } -- cgit v1.2.3