Ejemplo n.º 1
1
  private NativeLibrary openLibraryImpl(final boolean global) throws URISyntaxException {
    final ClassLoader cl = getClass().getClassLoader();
    System.err.println("CL " + cl);

    String libBaseName = null;
    final Class<?> clazz = this.getClass();
    Uri libUri = null;
    try {
      libUri = Uri.valueOf(clazz.getResource("/libtest1.so"));
    } catch (final URISyntaxException e2) {
      // not found .. OK
    }
    if (null != libUri) {
      libBaseName = "libtest1.so";
    } else {
      try {
        libUri = Uri.valueOf(clazz.getResource("/test1.dll"));
        if (null != libUri) {
          libBaseName = "test1.dll";
        }
      } catch (final URISyntaxException e) {
        // not found
      }
    }
    System.err.println("Untrusted Library (URL): " + libUri);

    if (null != libUri) {
      Uri libDir1 = libUri.getContainedUri();
      System.err.println("libDir1.1: " + libDir1);
      libDir1 = libDir1.getParent();
      System.err.println("libDir1.2: " + libDir1);
      System.err.println("Untrusted Library Dir1 (abs): " + libDir1);
      final Uri absLib = libDir1.concat(Uri.Encoded.cast("natives/" + libBaseName));
      Exception se0 = null;
      NativeLibrary nlib = null;
      try {
        nlib = NativeLibrary.open(absLib.toFile().getPath(), cl);
        System.err.println("NativeLibrary: " + nlib);
      } catch (final SecurityException e) {
        se0 = e;
        if (usesSecurityManager) {
          System.err.println("Expected exception for loading native library");
          System.err.println("Message: " + se0.getMessage());
        } else {
          System.err.println("Unexpected exception for loading native library");
          se0.printStackTrace();
        }
      }
      if (!usesSecurityManager) {
        Assert.assertNull("SecurityException thrown on loading native library", se0);
      } else {
        Assert.assertNotNull("SecurityException not thrown on loading native library", se0);
      }
      return nlib;
    } else {
      System.err.println("No library found");
      return null;
    }
  }
Ejemplo n.º 2
0
  /**
   * Locates the {@link JarUtil#getJarFileUri(Uri) Jar file Uri} of a given resource relative to a
   * given class's Jar's Uri.
   *
   * <pre>
   *   class's jar url path + cutOffInclSubDir + relResPath,
   * </pre>
   *
   * Example #1
   *
   * <pre>
   *   classFromJavaJar = com.lighting.Test (in: file:/storage/TestLighting.jar)
   *   cutOffInclSubDir = lights/
   *   relResPath       = LightAssets.jar
   *   Result           : file:/storage/lights/LightAssets.jar
   * </pre>
   *
   * Example #2
   *
   * <pre>
   *   classFromJavaJar = com.lighting.Test (in: file:/storage/lights/TestLighting.jar)
   *   cutOffInclSubDir = lights/
   *   relResPath       = LightAssets.jar
   *   Result           : file:/storage/lights/LightAssets.jar
   * </pre>
   *
   * TODO: Enhance documentation!
   *
   * @param classFromJavaJar Used to get the root Uri for the class's Jar Uri.
   * @param cutOffInclSubDir The <i>cut off</i> included sub-directory prepending the relative
   *     resource path. If the root Uri includes cutOffInclSubDir, it is no more added to the
   *     result.
   * @param relResPath The relative resource path. (Uri encoded)
   * @return The resulting resource Uri, which is not tested.
   * @throws IllegalArgumentException
   * @throws IOException
   * @throws URISyntaxException
   */
  public static Uri getRelativeOf(
      final Class<?> classFromJavaJar,
      final Uri.Encoded cutOffInclSubDir,
      final Uri.Encoded relResPath)
      throws IllegalArgumentException, IOException, URISyntaxException {
    final ClassLoader cl = classFromJavaJar.getClassLoader();
    final Uri classJarUri = JarUtil.getJarUri(classFromJavaJar.getName(), cl);
    if (DEBUG) {
      System.err.println(
          "JarUtil.getRelativeOf: "
              + "(classFromJavaJar "
              + classFromJavaJar
              + ", classJarUri "
              + classJarUri
              + ", cutOffInclSubDir "
              + cutOffInclSubDir
              + ", relResPath "
              + relResPath
              + "): ");
    }

    final Uri jarSubUri = classJarUri.getContainedUri();
    if (null == jarSubUri) {
      throw new IllegalArgumentException("JarSubUri is null of: " + classJarUri);
    }
    final Uri.Encoded jarUriRoot = jarSubUri.getDirectory().getEncoded();
    if (DEBUG) {
      System.err.println(
          "JarUtil.getRelativeOf: " + "uri " + jarSubUri.toString() + " -> " + jarUriRoot);
    }

    final Uri.Encoded resUri;
    if (jarUriRoot.endsWith(cutOffInclSubDir.get())) {
      resUri = jarUriRoot.concat(relResPath);
    } else {
      resUri = jarUriRoot.concat(cutOffInclSubDir).concat(relResPath);
    }
    if (DEBUG) {
      System.err.println("JarUtil.getRelativeOf: " + "...  -> " + resUri);
    }

    final Uri resJarUri = JarUtil.getJarFileUri(resUri);
    if (DEBUG) {
      System.err.println("JarUtil.getRelativeOf: " + "fin " + resJarUri);
    }
    return resJarUri;
  }