Exemplo n.º 1
0
  /**
   * Returns an instance of NativeLibrary for the specified name. The library is loaded if not
   * already loaded. If already loaded, the existing instance is returned.
   *
   * <p>More than one name may map to the same NativeLibrary instance; only a single instance will
   * be provided for any given unique file path.
   *
   * @param libraryName The library name to load. This can be short form (e.g. "c"), an explicit
   *     version (e.g. "libc.so.6" or "QuickTime.framework/Versions/Current/QuickTime"), or the full
   *     (absolute) path to the library (e.g. "/lib/libc.so.6").
   * @param options native library options for the given library (see {@link Library}).
   */
  public static final NativeLibrary getInstance(String libraryName, Map options) {
    options = new HashMap(options);
    if (options.get(Library.OPTION_CALLING_CONVENTION) == null) {
      options.put(Library.OPTION_CALLING_CONVENTION, new Integer(Function.C_CONVENTION));
    }

    // Use current process to load libraries we know are already
    // loaded by the VM to ensure we get the correct version
    if ((Platform.isLinux() || Platform.isAIX()) && Platform.C_LIBRARY_NAME.equals(libraryName)) {
      libraryName = null;
    }
    synchronized (libraries) {
      WeakReference ref = (WeakReference) libraries.get(libraryName + options);
      NativeLibrary library = ref != null ? (NativeLibrary) ref.get() : null;

      if (library == null) {
        if (libraryName == null) {
          library =
              new NativeLibrary("<process>", null, Native.open(null, openFlags(options)), options);
        } else {
          library = loadLibrary(libraryName, options);
        }
        ref = new WeakReference(library);
        libraries.put(library.getName() + options, ref);
        File file = library.getFile();
        if (file != null) {
          libraries.put(file.getAbsolutePath() + options, ref);
          libraries.put(file.getName() + options, ref);
        }
      }
      return library;
    }
  }
Exemplo n.º 2
0
  private static NativeLibrary loadLibrary(String libraryName, Map options) {
    boolean isAbsolutePath = new File(libraryName).isAbsolute();
    List searchPath = new LinkedList();
    int openFlags = openFlags(options);

    // Append web start path, if available.  Note that this does not
    // attempt any library name variations
    String webstartPath = Native.getWebStartLibraryPath(libraryName);
    if (webstartPath != null) {
      searchPath.add(webstartPath);
    }

    //
    // Prepend any custom search paths specifically for this library
    //
    List customPaths = (List) searchPaths.get(libraryName);
    if (customPaths != null) {
      synchronized (customPaths) {
        searchPath.addAll(0, customPaths);
      }
    }

    searchPath.addAll(initPaths("jna.library.path"));
    String libraryPath = findLibraryPath(libraryName, searchPath);
    long handle = 0;
    //
    // Only search user specified paths first.  This will also fall back
    // to dlopen/LoadLibrary() since findLibraryPath returns the mapped
    // name if it cannot find the library.
    //
    try {
      handle = Native.open(libraryPath, openFlags);
    } catch (UnsatisfiedLinkError e) {
      // Add the system paths back for all fallback searching
      searchPath.addAll(librarySearchPath);
    }
    try {
      if (handle == 0) {
        libraryPath = findLibraryPath(libraryName, searchPath);
        handle = Native.open(libraryPath, openFlags);
        if (handle == 0) {
          throw new UnsatisfiedLinkError("Failed to load library '" + libraryName + "'");
        }
      }
    } catch (UnsatisfiedLinkError e) {
      // For android, try to "preload" the library using
      // System.loadLibrary(), which looks into the private /data/data
      // path, not found in any properties
      if (Platform.isAndroid()) {
        try {
          System.loadLibrary(libraryName);
          handle = Native.open(libraryPath, openFlags);
        } catch (UnsatisfiedLinkError e2) {
          e = e2;
        }
      } else if (Platform.isLinux()) {
        //
        // Failed to load the library normally - try to match libfoo.so.*
        //
        libraryPath = matchLibrary(libraryName, searchPath);
        if (libraryPath != null) {
          try {
            handle = Native.open(libraryPath, openFlags);
          } catch (UnsatisfiedLinkError e2) {
            e = e2;
          }
        }
      }
      // Search framework libraries on OS X
      else if (Platform.isMac() && !libraryName.endsWith(".dylib")) {
        libraryPath = matchFramework(libraryName);
        if (libraryPath != null) {
          try {
            handle = Native.open(libraryPath, openFlags);
          } catch (UnsatisfiedLinkError e2) {
            e = e2;
          }
        }
      }
      // Try the same library with a "lib" prefix
      else if (Platform.isWindows() && !isAbsolutePath) {
        libraryPath = findLibraryPath("lib" + libraryName, searchPath);
        try {
          handle = Native.open(libraryPath, openFlags);
        } catch (UnsatisfiedLinkError e2) {
          e = e2;
        }
      }
      // As a last resort, try to extract the library from the class
      // path, using the current context class loader.
      if (handle == 0) {
        try {
          File embedded =
              Native.extractFromResourcePath(
                  libraryName, (ClassLoader) options.get(Library.OPTION_CLASSLOADER));
          handle = Native.open(embedded.getAbsolutePath());
          // Don't leave temporary files around
          if (Native.isUnpacked(embedded)) {
            Native.deleteLibrary(embedded);
          }
        } catch (IOException e2) {
          e = new UnsatisfiedLinkError(e2.getMessage());
        }
      }

      if (handle == 0) {
        throw new UnsatisfiedLinkError(
            "Unable to load library '" + libraryName + "': " + e.getMessage());
      }
    }
    return new NativeLibrary(libraryName, libraryPath, handle, options);
  }