aboutsummaryrefslogtreecommitdiff
path: root/jni-library/src/main/scala/ch/jodersky/jni/NativeLoader.scala
blob: 46d4b2d2c9903e98c05214b416d1679c9fe8f59b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package ch.jodersky.jni

import java.io.{File, FileOutputStream, InputStream, OutputStream}
import scala.io.Source

/**
  * Provides enhanced native library loading functionality. 
  */
object NativeLoader {

  /** Name of the shared library file that is contained in a jar. */
  final val LibraryName = "library"

  final val BufferSize = 4096

  /** Extract 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

    try {
      in = Option(NativeLoader.getClass.getResourceAsStream(path))
      if (in.isEmpty) return None

      val file = File.createTempFile(path, "")
      out = Some(new FileOutputStream(file))

      val buffer = new Array[Byte](BufferSize)
      var length = -1;
      do {
        length = in.get.read(buffer)
        if (length != -1) out.get.write(buffer, 0, length)
      } while (length != -1)

      Some(file)
    } finally {
      in.foreach(_.close)
      out.foreach(_.close)
    }
  }

  private def loadError(msg: String): Nothing = throw new UnsatisfiedLinkError(
    "Error during native library extraction " +
      "(this can happen if your platform is not supported): " +
      msg
  )

  def fullLibraryPath(libraryPath: String, platform: Platform) = {
    libraryPath + "/native/" + platform.id  + "/" + LibraryName
  }

  private def loadFromJar(libraryPath: String): Unit = {
    val platform = Platform.current.getOrElse{
      loadError("Cannot determine current platform.")
    }

    val resource = fullLibraryPath(libraryPath, platform)

    val file = extract(resource) getOrElse loadError(
      s"Shared library $resource not found."
    )
    System.load(file.getAbsolutePath())
  }

  /**
   * Load 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 {
    System.loadLibrary(library)
  } catch {
    case ex: UnsatisfiedLinkError => loadFromJar(libraryPath)
  }

}