Esempio n. 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);
  }
Esempio n. 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;
    }
  }
Esempio n. 3
0
 private static String nativeLibraryName(String baseName) {
   Platform platform = Platform.getPlatform();
   if (platform.isSolaris() || platform.isLinux()) {
     return "lib" + baseName + ".so";
   } else if (platform.isMac()) {
     return "lib" + baseName + ".dylib";
   } else if (platform.isWindows()) {
     return baseName; // + ".dll";
   } else {
     return baseName;
   }
 }
Esempio n. 4
0
  static {
    String webstartPath = Native.getWebStartLibraryPath("jnidispatch");
    if (webstartPath != null) {
      librarySearchPath.add(webstartPath);
    }
    if (System.getProperty("jna.platform.library.path") == null && !Platform.isWindows()) {
      // Add default path lookups for unix-like systems
      String platformPath = "";
      String sep = "";
      String archPath = "";

      //
      // Search first for an arch specific path if one exists, but always
      // include the generic paths if they exist.
      // NOTES (wmeissner):
      // Some older linux amd64 distros did not have /usr/lib64, and
      // 32bit distros only have /usr/lib.  FreeBSD also only has
      // /usr/lib by default, with /usr/lib32 for 32bit compat.
      // Solaris seems to have both, but defaults to 32bit userland even
      // on 64bit machines, so we have to explicitly search the 64bit
      // one when running a 64bit JVM.
      //
      if (Platform.isLinux()
          || Platform.isSolaris()
          || Platform.isFreeBSD()
          || Platform.iskFreeBSD()) {
        // Linux & FreeBSD use /usr/lib32, solaris uses /usr/lib/32
        archPath = (Platform.isSolaris() ? "/" : "") + Pointer.SIZE * 8;
      }
      String[] paths = {
        "/usr/lib" + archPath, "/lib" + archPath, "/usr/lib", "/lib",
      };
      // Multi-arch support on Ubuntu (and other
      // multi-arch distributions)
      // paths is scanned against real directory
      // so for platforms which are not multi-arch
      // this should continue to work.
      if (Platform.isLinux() || Platform.iskFreeBSD() || Platform.isGNU()) {
        String multiArchPath = getMultiArchPath();

        // Assemble path with all possible options
        paths =
            new String[] {
              "/usr/lib/" + multiArchPath,
              "/lib/" + multiArchPath,
              "/usr/lib" + archPath,
              "/lib" + archPath,
              "/usr/lib",
              "/lib",
            };
      }
      for (int i = 0; i < paths.length; i++) {
        File dir = new File(paths[i]);
        if (dir.exists() && dir.isDirectory()) {
          platformPath += sep + paths[i];
          sep = File.pathSeparator;
        }
      }
      if (!"".equals(platformPath)) {
        System.setProperty("jna.platform.library.path", platformPath);
      }
    }
    librarySearchPath.addAll(initPaths("jna.platform.library.path"));
  }
Esempio n. 5
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);
  }
 @Test
 public void should_return_false_if_OS_is_not_Linux() {
   Platform.initialize(windowsXP(), toolkitProvider);
   assertThat(Platform.isLinux()).isFalse();
 }
 @Test
 public void should_return_true_if_OS_is_Linux() {
   Platform.initialize(linux(), toolkitProvider);
   assertThat(Platform.isLinux()).isTrue();
 }