jnr.ffi.LibraryLoader Java Examples

The following examples show how to use jnr.ffi.LibraryLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: incubator-tuweni   Author: apache   File: Sodium.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Load and initialize the native libsodium shared library.
 *
 * <p>
 * If this method returns successfully (without throwing a {@link LinkageError}), then all future calls to methods
 * provided by this class will use the loaded library.
 *
 * @param path The path to the shared library.
 * @throws LinkageError If the library cannot be found, dependent libraries are missing, or cannot be initialized.
 */
public static void loadLibrary(Path path) {
  requireNonNull(path);
  if (!Files.exists(path)) {
    throw new IllegalArgumentException("Non-existent path");
  }

  Path dir = path.getParent();
  Path library = path.getFileName();

  LibSodium lib =
      LibraryLoader.create(LibSodium.class).search(dir.toFile().getAbsolutePath()).load(library.toString());
  initializeLibrary(lib);

  synchronized (LibSodium.class) {
    Sodium.libSodium = lib;
  }
}
 
Example #2
Source Project: incubator-tuweni   Author: apache   File: Sodium.java    License: Apache License 2.0 6 votes vote down vote up
private static LibSodium libSodium() {
  if (libSodium == null) {
    synchronized (LibSodium.class) {
      if (libSodium == null) {
        LibSodium lib = LibraryLoader
            .create(LibSodium.class)
            .search("/usr/local/lib")
            .search("/opt/local/lib")
            .search("/usr/lib")
            .search("/lib")
            .load(LIBRARY_NAME);
        libSodium = initializeLibrary(lib);
      }
    }
  }
  return libSodium;
}
 
Example #3
Source Project: cava   Author: ConsenSys   File: Sodium.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Load and initialize the native libsodium shared library.
 *
 * <p>
 * If this method returns successfully (without throwing a {@link LinkageError}), then all future calls to methods
 * provided by this class will use the loaded library.
 *
 * @param path The path to the shared library.
 * @throws LinkageError If the library cannot be found, dependent libraries are missing, or cannot be initialized.
 */
public static void loadLibrary(Path path) {
  requireNonNull(path);
  if (!Files.exists(path)) {
    throw new IllegalArgumentException("Non-existent path");
  }

  Path dir = path.getParent();
  Path library = path.getFileName();

  LibSodium lib =
      LibraryLoader.create(LibSodium.class).search(dir.toFile().getAbsolutePath()).load(library.toString());
  initializeLibrary(lib);

  synchronized (LibSodium.class) {
    Sodium.libSodium = lib;
  }
}
 
Example #4
Source Project: cava   Author: ConsenSys   File: Sodium.java    License: Apache License 2.0 6 votes vote down vote up
private static LibSodium libSodium() {
  if (libSodium == null) {
    synchronized (LibSodium.class) {
      if (libSodium == null) {
        LibSodium lib = LibraryLoader
            .create(LibSodium.class)
            .search("/usr/local/lib")
            .search("/opt/local/lib")
            .search("/usr/lib")
            .search("/lib")
            .load(LIBRARY_NAME);
        libSodium = initializeLibrary(lib);
      }
    }
  }
  return libSodium;
}
 
Example #5
Source Project: Chronicle-Salt   Author: OpenHFT   File: Sodium.java    License: Apache License 2.0 6 votes vote down vote up
static Sodium init() {
    String libraryName = "sodium";
    if (Platform.getNativePlatform().getOS() == Platform.OS.WINDOWS) {
        libraryName = "libsodium";
    }

    Sodium sodium = null;
    try {
        sodium = LibraryLoader.create(Sodium.class).search("lib").search("/usr/local/lib").search("/opt/local/lib").load(libraryName);

    } catch (Error e) {
        if (Platform.getNativePlatform().getOS() == Platform.OS.WINDOWS)
            System.err.println("Unable to load libsodium, make sure the Visual C++ Downloadable is installed\n"
                    + "https://support.microsoft.com/en-gb/help/2977003/the-latest-supported-visual-c-downloads");
        throw e;
    }

    checkValid(sodium.sodium_init(), "sodium_init()");
    return sodium;
}
 
Example #6
Source Project: incubator-tuweni   Author: apache   File: Sodium.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Search for, then load and initialize the native libsodium shared library.
 *
 * <p>
 * The library will be searched for in all the provided locations, using the provided library name. If this method
 * returns successfully (without throwing a {@link LinkageError}), then all future calls to methods provided by this
 * class will use the loaded library.
 *
 * @param libraryName The name of the library (e.g. {@code "sodium"}).
 * @param paths A set of directories to search for the library in.
 * @throws LinkageError If the library cannot be found, dependent libraries are missing, or cannot be initialized.
 */
public static void searchLibrary(String libraryName, Path... paths) {
  LibraryLoader<LibSodium> loader = LibraryLoader.create(LibSodium.class);
  for (Path path : paths) {
    loader = loader.search(path.toFile().getAbsolutePath());
  }
  LibSodium lib = loader.load(libraryName);
  initializeLibrary(lib);

  synchronized (LibSodium.class) {
    Sodium.libSodium = lib;
  }
}
 
Example #7
Source Project: cava   Author: ConsenSys   File: Sodium.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Search for, then load and initialize the native libsodium shared library.
 *
 * <p>
 * The library will be searched for in all the provided locations, using the provided library name. If this method
 * returns successfully (without throwing a {@link LinkageError}), then all future calls to methods provided by this
 * class will use the loaded library.
 *
 * @param libraryName The name of the library (e.g. {@code "sodium"}).
 * @param paths A set of directories to search for the library in.
 * @throws LinkageError If the library cannot be found, dependent libraries are missing, or cannot be initialized.
 */
public static void searchLibrary(String libraryName, Path... paths) {
  LibraryLoader<LibSodium> loader = LibraryLoader.create(LibSodium.class);
  for (Path path : paths) {
    loader = loader.search(path.toFile().getAbsolutePath());
  }
  LibSodium lib = loader.load(libraryName);
  initializeLibrary(lib);

  synchronized (LibSodium.class) {
    Sodium.libSodium = lib;
  }
}