Example #1
1
  /**
   * Similar to {@link System#mapLibraryName}, except that it maps to standard shared library
   * formats rather than specifically JNI formats.
   *
   * @param libName base (undecorated) name of library
   */
  static String mapSharedLibraryName(String libName) {
    if (Platform.isMac()) {
      if (libName.startsWith("lib")
          && (libName.endsWith(".dylib") || libName.endsWith(".jnilib"))) {
        return libName;
      }
      String name = System.mapLibraryName(libName);
      // On MacOSX, System.mapLibraryName() returns the .jnilib extension
      // (the suffix for JNI libraries); ordinarily shared libraries have
      // a .dylib suffix
      if (name.endsWith(".jnilib")) {
        return name.substring(0, name.lastIndexOf(".jnilib")) + ".dylib";
      }
      return name;
    } else if (Platform.isLinux()) {
      if (isVersionedName(libName) || libName.endsWith(".so")) {
        // A specific version was requested - use as is for search
        return libName;
      }
    } else if (Platform.isAIX()) { // can be libx.a, libx.a(shr.o), libx.so
      if (libName.startsWith("lib")) {
        return libName;
      }
    } else if (Platform.isWindows()) {
      if (libName.endsWith(".drv") || libName.endsWith(".dll")) {
        return libName;
      }
    }

    return System.mapLibraryName(libName);
  }
Example #2
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;
    }
  }