Ejemplo n.º 1
0
  /**
   * Find the library <tt>libname</tt> as a resource, copy it to a tempfile and load it using
   * System.load(). The name of the library has to be the base name, it is mapped to the
   * corresponding system name using System.mapLibraryName(). For example, the library "foo" is
   * called "libfoo.so" under Linux and "foo.dll" under Windows, but you just have to pass "foo" the
   * loadLibrary().
   *
   * <p>
   *
   * <p>I'm not quite sure if this doesn't open all kinds of security holes. Any ideas?
   *
   * <p>
   *
   * <p>This function reports some more information to the "org.jblas" logger at the FINE level.
   *
   * @param libname basename of the library
   * @throws UnsatisfiedLinkError if library cannot be founds
   */
  public void loadLibrary(String libname, boolean withFlavor) {
    // preload flavor libraries
    String flavor = null;
    if (withFlavor) {
      logger.debug("Preloading ArchFlavor library.");
      flavor = ArchFlavor.archFlavor();
      if (flavor != null && flavor.equals("sse2")) {
        throw new UnsupportedArchitectureException(
            "Support for SSE2 processors stopped with version 1.2.2. Sorry.");
      }
    }
    logger.debug("Found flavor = '" + flavor + "'");

    libname = System.mapLibraryName(libname);

    /*
     * JDK 7 changed the ending for Mac OS from "jnilib" to "dylib".
     *
     * If that is the case, remap the filename.
     */
    String loadLibname = libname;
    if (libname.endsWith("dylib")) {
      loadLibname = libname.replace(".dylib", ".jnilib");
      logger.config("Replaced .dylib with .jnilib");
    }

    logger.debug("Attempting to load \"" + loadLibname + "\".");

    String[] paths = {
      fatJarLibraryPath("static", flavor), fatJarLibraryPath("dynamic", flavor),
    };

    InputStream is = findLibrary(paths, loadLibname);

    // Haven't found the lib anywhere? Throw a reception.
    if (is == null) {
      throw new UnsatisfiedLinkError("Couldn't find the resource " + loadLibname + ".");
    }

    logger.config("Loading " + loadLibname + " from " + libpath + ", copying to " + libname + ".");
    loadLibraryFromStream(libname, is);
  }