From 9a211fe6cfb4150d97b50325c1778b7f5d3419af Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Thu, 5 Jan 2012 08:55:06 +0100
Subject: Further fix for bug 537 - Catch IllegalArgumentException in
 Platform.loadGlueGenRTImpl(), Reuse JarUtil (same methodology) to determine
 whether we run from JarURL

---
 src/java/com/jogamp/common/os/Platform.java        | 30 +++++-----
 src/java/com/jogamp/common/util/JarUtil.java       | 66 +++++++++++++++++-----
 .../com/jogamp/common/util/cache/TempJarCache.java |  1 -
 3 files changed, 65 insertions(+), 32 deletions(-)

(limited to 'src/java')

diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java
index 55210ce..bd8523a 100644
--- a/src/java/com/jogamp/common/os/Platform.java
+++ b/src/java/com/jogamp/common/os/Platform.java
@@ -28,7 +28,6 @@
  
 package com.jogamp.common.os;
 
-import java.io.IOException;
 import java.net.URL;
 import java.nio.ByteBuffer;
 import java.nio.IntBuffer;
@@ -36,7 +35,6 @@ import java.nio.ShortBuffer;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.concurrent.TimeUnit;
-import java.util.jar.JarFile;
 
 import com.jogamp.common.nio.Buffers;
 import com.jogamp.common.util.JarUtil;
@@ -204,7 +202,7 @@ public class Platform {
         
         os_and_arch = getOSAndArch(OS_TYPE, CPU_ARCH);
         
-        USE_TEMP_JAR_CACHE = (OS_TYPE != OSType.ANDROID) && !isRunningFromClassFile() &&
+        USE_TEMP_JAR_CACHE = (OS_TYPE != OSType.ANDROID) && isRunningFromJarURL() &&
             AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
                 public Boolean run() {
                     return Boolean.valueOf(Debug.getBooleanProperty(true, useTempJarCachePropName, true, AccessController.getContext()));
@@ -233,20 +231,17 @@ public class Platform {
 
     private Platform() {}
 
-    /* Used to disable JAR caching if we're not running from a JAR.
-     *
-     * If you build JOGL in an IDE like Eclipse, it's possible for other projects in your
-     * workspace to depend directly on the JOGL class files rather than on the built JAR files.
-     * This allows you to turn off JAR file creation in your JOGL Ant build to speed up the
-     * build, and allows Eclipse to resolve dependencies to auto-built class files before
-     * the Ant build even runs (which is not until after file save).
+    /**
+     * Preemptively avoids initializing and using {@link TempJarCache} in case we are <b>not</b> running 
+     * from a Jar URL, ie. plain class files. Used to set {@link USE_TEMP_JAR_CACHE}.
+     * <p> 
+     * Impact: Less overhead and more robustness.
+     * </p> 
      *
-     * @return true if we're running from a class file, false if we're running some other
-     * way (like from a JAR file).
+     * @return true if we're running from a Jar URL, otherwise false
      */
-    private static boolean isRunningFromClassFile() {
-        URL url = Platform.class.getResource( "Platform.class" );
-        return( url.getProtocol().equalsIgnoreCase( "file" ) );
+    private static boolean isRunningFromJarURL() {        
+        return JarUtil.hasJarURL(Platform.class.getName(), Platform.class.getClassLoader());
     }
     
     private static boolean queryIsLittleEndianImpl() {
@@ -326,8 +321,9 @@ public class Platform {
                                         JarUtil.getJarSubURL(Platform.class.getName(), cl) );
                     final URL nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);
                     TempJarCache.bootstrapNativeLib(Platform.class, libBaseName, nativeJarURL, cl);
-                  } catch (IOException ioe) {
-                    ioe.printStackTrace();
+                  } catch (Exception e0) {
+                    // IllegalArgumentException, IOException
+                    System.err.println("Catched: "+e0.getMessage());
                   }
               }
               DynamicLibraryBundle.GlueJNILibLoader.loadLibrary(libBaseName, false);
diff --git a/src/java/com/jogamp/common/util/JarUtil.java b/src/java/com/jogamp/common/util/JarUtil.java
index 4beda94..bd63a56 100644
--- a/src/java/com/jogamp/common/util/JarUtil.java
+++ b/src/java/com/jogamp/common/util/JarUtil.java
@@ -51,31 +51,76 @@ import jogamp.common.Debug;
 public class JarUtil {
     private static final boolean DEBUG = Debug.debug("JarUtil");
 
+    /**
+     * Returns <code>true</code> if the Class's <code>"com.jogamp.common.GlueGenVersion"</code>
+     * is loaded from a JarFile and hence has a Jar URL like 
+     * URL <code>jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class"</code>.
+     * <p>
+     * <i>sub_protocol</i> may be "file", "http", etc..
+     * </p>
+     * 
+     * @param clazzBinName "com.jogamp.common.GlueGenVersion" 
+     * @param cl
+     * @return true if the class is loaded from a Jar file, otherwise false.
+     * @see {@link #getJarURL(String, ClassLoader)}
+     */
+    public static boolean hasJarURL(String clazzBinName, ClassLoader cl) {
+        try {
+            final URL url = getJarURL(clazzBinName, cl);
+            return null != url;        
+        } catch (Exception e) { /* ignore */ }
+        return false;
+    }
+    
     /**
      * The Class's <code>"com.jogamp.common.GlueGenVersion"</code> 
      * URL <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.
+     * will be returned.
      * <p>
      * <i>sub_protocol</i> may be "file", "http", etc..
      * </p>
      * 
      * @param clazzBinName "com.jogamp.common.GlueGenVersion" 
      * @param cl
-     * @return "gluegen-rt.jar"
+     * @return "jar:<i>sub_protocol</i>:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class"
      * @throws IllegalArgumentException if the URL doesn't match the expected formatting 
      * @throws IOException
      * @see {@link IOUtil#getClassURL(String, ClassLoader)}
      */
-    public static String getJarBasename(String clazzBinName, ClassLoader cl) throws IllegalArgumentException, IOException {
+    public static URL getJarURL(String clazzBinName, ClassLoader cl) throws IllegalArgumentException, IOException {
         URL url = IOUtil.getClassURL(clazzBinName, cl);
         if(null != url) {
             String urlS = url.toExternalForm();
             if(DEBUG) {
-                System.out.println("getJarName "+url+", extForm: "+urlS);
+                System.out.println("getJarURL "+url+", extForm: "+urlS);
             }
             if(!urlS.startsWith("jar:")) {
                 throw new IllegalArgumentException("JAR URL doesn't start with 'jar:', got <"+urlS+">");
-            }                    
+            }
+        }
+        return url;
+    }
+    
+    
+    /**
+     * The Class's <code>"com.jogamp.common.GlueGenVersion"</code> 
+     * URL <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..
+     * </p>
+     * 
+     * @param clazzBinName "com.jogamp.common.GlueGenVersion" 
+     * @param cl
+     * @return "gluegen-rt.jar"
+     * @throws IllegalArgumentException if the URL doesn't match the expected formatting 
+     * @throws IOException
+     * @see {@link IOUtil#getClassURL(String, ClassLoader)}
+     */
+    public static String getJarBasename(String clazzBinName, ClassLoader cl) throws IllegalArgumentException, IOException {
+        URL url = getJarURL(clazzBinName, cl);
+        if(null != url) {
+            String urlS = url.toExternalForm();            
             urlS = urlS.substring(4, urlS.length()); // exclude 'jar:'
             
             // from 
@@ -130,15 +175,9 @@ public class JarUtil {
      * @see {@link IOUtil#getClassURL(String, ClassLoader)}
      */
     public static URL getJarSubURL(String clazzBinName, ClassLoader cl) throws IllegalArgumentException, IOException {
-        URL url = IOUtil.getClassURL(clazzBinName, cl);
+        URL url = getJarURL(clazzBinName, cl);
         if(null != url) {
             String urlS = url.toExternalForm();
-            if(DEBUG) {
-                System.out.println("getJarSubURL "+url+", extForm: "+urlS);
-            }
-            if(!urlS.startsWith("jar:")) {
-                throw new IllegalArgumentException("JAR URL doesn't start with 'jar:', got <"+urlS+">");
-            }                    
             urlS = urlS.substring(4, urlS.length()); // exclude 'jar:'
             
             // from 
@@ -266,7 +305,6 @@ public class JarUtil {
     }
     
     /**
-     * 
      * @param clazzBinName com.jogamp.common.util.cache.TempJarCache 
      * @param cl domain 
      * @return JarFile containing the named class within the given ClassLoader
@@ -274,7 +312,7 @@ public class JarUtil {
      * @see {@link #getJarFileURL(String, ClassLoader)}
      */
     public static JarFile getJarFile(String clazzBinName, ClassLoader cl) throws IOException {
-            return getJarFile(getJarFileURL(clazzBinName, cl), cl);
+        return getJarFile(getJarFileURL(clazzBinName, cl), cl);
     }
 
     /**
diff --git a/src/java/com/jogamp/common/util/cache/TempJarCache.java b/src/java/com/jogamp/common/util/cache/TempJarCache.java
index 431971c..dd3e306 100644
--- a/src/java/com/jogamp/common/util/cache/TempJarCache.java
+++ b/src/java/com/jogamp/common/util/cache/TempJarCache.java
@@ -133,7 +133,6 @@ public class TempJarCache {
     } */
     
     /**
-     * 
      * @return true if this class has been properly initialized, ie. is in use, otherwise false.
      */
     public static boolean isInitialized() {
-- 
cgit v1.2.3