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;
    }
  }
示例#2
0
 /**
  * @param jarFileUri jar:file:/some/path/gluegen-rt.jar!/
  * @return JarFile as named by Uri within the given ClassLoader
  * @throws IllegalArgumentException null arguments
  * @throws IOException if the Jar file could not been found
  * @throws URISyntaxException
  */
 public static JarFile getJarFile(final Uri jarFileUri)
     throws IOException, IllegalArgumentException, URISyntaxException {
   if (null == jarFileUri) {
     throw new IllegalArgumentException("null jarFileUri");
   }
   if (DEBUG) {
     System.err.println("getJarFile.0: " + jarFileUri.toString());
   }
   final URL jarFileURL = jarFileUri.toURL();
   if (DEBUG) {
     System.err.println("getJarFile.1: " + jarFileURL.toString());
   }
   final URLConnection urlc = jarFileURL.openConnection();
   if (urlc instanceof JarURLConnection) {
     final JarURLConnection jarConnection = (JarURLConnection) jarFileURL.openConnection();
     final JarFile jarFile = jarConnection.getJarFile();
     if (DEBUG) {
       System.err.println("getJarFile res: " + jarFile.getName());
     }
     return jarFile;
   }
   if (DEBUG) {
     System.err.println("getJarFile res: NULL");
   }
   return null;
 }
示例#3
0
 /**
  * @param jarSubUri file:/some/path/gluegen-rt.jar
  * @return jar:file:/some/path/gluegen-rt.jar!/
  * @throws IllegalArgumentException null arguments
  * @throws URISyntaxException
  */
 public static Uri getJarFileUri(final Uri jarSubUri)
     throws IllegalArgumentException, URISyntaxException {
   if (null == jarSubUri) {
     throw new IllegalArgumentException("jarSubUri is null");
   }
   return Uri.cast(Uri.JAR_SCHEME + Uri.SCHEME_SEPARATOR + jarSubUri.toString() + "!/");
 }
示例#4
0
 /**
  * @param jarFileUri jar:file:/some/path/gluegen-rt.jar!/
  * @param jarEntry com/jogamp/common/GlueGenVersion.class
  * @return jar:file:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
  * @throws IllegalArgumentException null arguments
  * @throws URISyntaxException
  */
 public static Uri getJarEntryUri(final Uri jarFileUri, final Uri.Encoded jarEntry)
     throws IllegalArgumentException, URISyntaxException {
   if (null == jarEntry) {
     throw new IllegalArgumentException("jarEntry is null");
   }
   return Uri.cast(jarFileUri.toString() + jarEntry);
 }
示例#5
0
 /**
  * @param baseUri file:/some/path/
  * @param jarFileName gluegen-rt.jar (Uri encoded)
  * @return jar:file:/some/path/gluegen-rt.jar!/
  * @throws URISyntaxException
  * @throws IllegalArgumentException null arguments
  */
 public static Uri getJarFileUri(final Uri baseUri, final Uri.Encoded jarFileName)
     throws IllegalArgumentException, URISyntaxException {
   if (null == baseUri || null == jarFileName) {
     throw new IllegalArgumentException(
         "null arguments: baseUri " + baseUri + ", jarFileName " + jarFileName);
   }
   return Uri.cast(
       Uri.JAR_SCHEME + Uri.SCHEME_SEPARATOR + baseUri.toString() + jarFileName + "!/");
 }
示例#6
0
 /**
  * The Class's <code>"com.jogamp.common.GlueGenVersion"</code> Uri <code>
  * jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class"
  * </code> Jar file Uri <code>jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/</code> will be
  * returned.
  *
  * <p><i>sub_protocol</i> may be "file", "http", etc..
  *
  * @param clazzBinName "com.jogamp.common.GlueGenVersion"
  * @param cl
  * @return "jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/"
  * @throws IllegalArgumentException if the Uri doesn't match the expected formatting or null
  *     arguments
  * @throws IOException if the class's Jar file could not been found by the ClassLoader
  * @throws URISyntaxException if the Uri could not be translated into a RFC 2396 Uri
  * @see {@link IOUtil#getClassURL(String, ClassLoader)}
  */
 public static Uri getJarFileUri(final String clazzBinName, final ClassLoader cl)
     throws IllegalArgumentException, IOException, URISyntaxException {
   if (null == clazzBinName || null == cl) {
     throw new IllegalArgumentException(
         "null arguments: clazzBinName " + clazzBinName + ", cl " + cl);
   }
   final Uri jarSubUri = getJarUri(clazzBinName, cl).getContainedUri();
   final Uri uri = Uri.cast(Uri.JAR_SCHEME + Uri.SCHEME_SEPARATOR + jarSubUri.toString() + "!/");
   if (DEBUG) {
     System.err.println("getJarFileUri res: " + uri);
   }
   return uri;
 }
示例#7
0
  /**
   * The Class's Jar Uri <code>
   * jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
   * </code> Jar file's entry <code>/com/jogamp/common/GlueGenVersion.class</code> will be returned.
   *
   * @param classJarUri as retrieved w/ {@link #getJarUri(String, ClassLoader)
   *     getJarUri("com.jogamp.common.GlueGenVersion", cl)}, i.e. <code>
   *     jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
   *     </code>
   * @return <code>/com/jogamp/common/GlueGenVersion.class</code>
   * @see {@link IOUtil#getClassURL(String, ClassLoader)}
   */
  public static Uri.Encoded getJarEntry(final Uri classJarUri) {
    if (null == classJarUri) {
      throw new IllegalArgumentException("Uri is null");
    }
    if (!classJarUri.isJarScheme()) {
      throw new IllegalArgumentException(
          "Uri is not a using scheme " + Uri.JAR_SCHEME + ": <" + classJarUri + ">");
    }
    final Uri.Encoded uriSSP = classJarUri.schemeSpecificPart;

    // from
    //   file:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
    // to
    //   /com/jogamp/common/GlueGenVersion.class
    final int idx = uriSSP.lastIndexOf(Uri.JAR_SCHEME_SEPARATOR);
    if (0 <= idx) {
      final Uri.Encoded res = uriSSP.substring(idx + 1); // right of '!'
      // Uri TODO ? final String res = Uri.decode(uriSSP.substring(idx+1)); // right of '!'
      if (DEBUG) {
        System.err.println(
            "getJarEntry res: " + classJarUri + " -> " + uriSSP + " -> " + idx + " -> " + res);
      }
      return res;
    } else {
      throw new IllegalArgumentException(
          "JAR Uri does not contain jar uri terminator '!', uri <" + classJarUri + ">");
    }
  }
示例#8
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;
  }
示例#9
0
 /**
  * The Class's <code>"com.jogamp.common.GlueGenVersion"</code> Uri <code>
  * jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class"
  * </code> will be returned.
  *
  * <p><i>sub_protocol</i> may be "file", "http", etc..
  *
  * @param clazzBinName "com.jogamp.common.GlueGenVersion"
  * @param cl ClassLoader to locate the JarFile
  * @return
  *     "jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class"
  * @throws IllegalArgumentException if the Uri doesn't match the expected formatting or null
  *     arguments
  * @throws IOException if the class's Jar file could not been found by the ClassLoader
  * @throws URISyntaxException if the Uri could not be translated into a RFC 2396 Uri
  * @see {@link IOUtil#getClassURL(String, ClassLoader)}
  */
 public static Uri getJarUri(final String clazzBinName, final ClassLoader cl)
     throws IllegalArgumentException, IOException, URISyntaxException {
   if (null == clazzBinName || null == cl) {
     throw new IllegalArgumentException(
         "null arguments: clazzBinName " + clazzBinName + ", cl " + cl);
   }
   final Uri uri;
   final URL url;
   {
     url = IOUtil.getClassURL(clazzBinName, cl);
     final String scheme = url.getProtocol();
     if (null != resolver
         && !scheme.equals(Uri.JAR_SCHEME)
         && !scheme.equals(Uri.FILE_SCHEME)
         && !scheme.equals(Uri.HTTP_SCHEME)
         && !scheme.equals(Uri.HTTPS_SCHEME)) {
       final URL _url = resolver.resolve(url);
       uri = Uri.valueOf(_url);
       if (DEBUG) {
         System.err.println("getJarUri Resolver: " + url + "\n\t-> " + _url + "\n\t-> " + uri);
       }
     } else {
       uri = Uri.valueOf(url);
       if (DEBUG) {
         System.err.println("getJarUri Default " + url + "\n\t-> " + uri);
       }
     }
   }
   if (!uri.isJarScheme()) {
     throw new IllegalArgumentException(
         "Uri is not using scheme " + Uri.JAR_SCHEME + ": <" + uri + ">");
   }
   if (DEBUG) {
     System.err.println("getJarUri res: " + clazzBinName + " -> " + url + " -> " + uri);
   }
   return uri;
 }
示例#10
0
  /**
   * The Class's Jar Uri <code>
   * jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
   * </code> Jar basename <code>gluegen-rt.jar</code> will be returned.
   *
   * <p><i>sub_protocol</i> may be "file", "http", etc..
   *
   * @param classJarUri as retrieved w/ {@link #getJarUri(String, ClassLoader)
   *     getJarUri("com.jogamp.common.GlueGenVersion", cl)}, i.e. <code>
   *     jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
   *     </code>
   * @return <code>gluegen-rt.jar</code>
   * @throws IllegalArgumentException if the Uri doesn't match the expected formatting or is null
   * @see {@link IOUtil#getClassURL(String, ClassLoader)}
   */
  public static Uri.Encoded getJarBasename(final Uri classJarUri) throws IllegalArgumentException {
    if (null == classJarUri) {
      throw new IllegalArgumentException("Uri is null");
    }
    if (!classJarUri.isJarScheme()) {
      throw new IllegalArgumentException(
          "Uri is not using scheme " + Uri.JAR_SCHEME + ": <" + classJarUri + ">");
    }
    Uri.Encoded ssp = classJarUri.schemeSpecificPart;

    // from
    //   file:/some/path/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
    // to
    //   file:/some/path/gluegen-rt.jar
    int idx = ssp.lastIndexOf(Uri.JAR_SCHEME_SEPARATOR);
    if (0 <= idx) {
      ssp = ssp.substring(0, idx); // exclude '!/'
    } else {
      throw new IllegalArgumentException(
          "Uri does not contain jar uri terminator '!', in <" + classJarUri + ">");
    }

    // from
    //   file:/some/path/gluegen-rt.jar
    // to
    //   gluegen-rt.jar
    idx = ssp.lastIndexOf('/');
    if (0 > idx) {
      // no abs-path, check for protocol terminator ':'
      idx = ssp.lastIndexOf(':');
      if (0 > idx) {
        throw new IllegalArgumentException(
            "Uri does not contain protocol terminator ':', in <" + classJarUri + ">");
      }
    }
    ssp = ssp.substring(idx + 1); // just the jar name

    if (0 >= ssp.lastIndexOf(".jar")) {
      throw new IllegalArgumentException("No Jar name in <" + classJarUri + ">");
    }
    if (DEBUG) {
      System.err.println("getJarName res: " + ssp);
    }
    return ssp;
  }