Example #1
0
 // XFAIL on android
 public void testGetProcess() {
   if (Platform.isAndroid()) {
     fail("dlopen(NULL) segfaults on Android");
   }
   NativeLibrary process = NativeLibrary.getProcess();
   // Access a common C library function
   process.getFunction("printf");
 }
Example #2
0
  static {
    PlatformSupport _instance = null;
    if (Platform.isAndroid()) {
      try {
        _instance = (PlatformSupport) Class.forName("org.bridj.AndroidSupport").newInstance();
        ;
      } catch (Exception ex) {
        throw new RuntimeException(
            "Failed to instantiate the Android support class... Was the BridJ jar tampered with / trimmed too much ?",
            ex);
      }
    }

    if (_instance == null) {
      _instance = new PlatformSupport();
    }

    instance = _instance;
  }
Example #3
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);
  }