name.pachler.nio.file.impl
Class NativeLibLoader

java.lang.Object
  extended by name.pachler.nio.file.impl.NativeLibLoader

public class NativeLibLoader
extends java.lang.Object

This class loads native libraries packaged in the JAR file that this class resides in. The NativeLibLoader supports a number of preconfigured native libraries, which it looks up by their name to find, extract and load their binary file. For each platform there exists a PlatformArchLibSet, which defines a list of native libraries available for that platform in LibraryImplementation instances. When a library "foo" is requested through the loadLibrary() method, all LibraryImplementation instances for the current platform's PlatformArchLibSet is searched. If a matching LibraryImplementation is found, first the library extraction and loading is attempted with a default library name. If that fails, extraction and loading is attempted with a unique temporary file name for the library. the steps are the following:

  1. It is attempted to open and lock file indicated by the library path for reading. If successful, the integrity if the file is checked (if it is the same as the one stored in the JAR file. If successful the library is loaded.
  2. If the step above fails, it is attempted to open and lock the default library file for writing. If successful, the library file is extracted to the path and the library is loaded from there.
If all of that fails, an attempt is to load the library with System.loadLibrary(), which requires the library to be installed in the system's native library path. This is the old (but common) way to load native libraries in Java, but requires the library to be installed separately in a native library directory accessible by the JVM.

The Default Library Path

The default library path is something like "/tmp/bar-1-0-mylib.so". Because it remains the same on multiple invocations of the same program, one program might find a library file from a previously running program that hasn't been cleaned up yet. The library file might even be there from a program that is still running, creating the potential for race conditions, so the implementation is careful to avoid clashes in such cases (such as two JVMs trying to extract the same library to the same file at the same time). The default library path is formed as {tempdir}/{productname}-{major}-{minor}-{binaryname}, where {productname} is the product name that's configured in NativeLibLoader's PRODUCTNAME static variable, {major} and {minor} are the major and minor versions configured for the LibraryImplementation, and {binaryname} is the name of the binary as it is stored in the JAR (without the path), like mylib.so.
So for a library "foo" stored in the JAR as "Windows/mylib.dll" with version 1.0, for PRODUCTNAME being "bar", the default library path would be "c:\Documents and Settings\myuser\temp\bar-1-0-mylib.dll"


Constructor Summary
NativeLibLoader()
           
 
Method Summary
static void loadLibrary(java.lang.String name)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NativeLibLoader

public NativeLibLoader()
Method Detail

loadLibrary

public static void loadLibrary(java.lang.String name)