From 7e6cf46ed2e0e9772f79e06437596056efa8c682 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 1 Dec 2011 16:18:34 +0100 Subject: Refine TempJarCache/JNILibLoaderBase ; Add TempFileCache destroy() for instance. JNILibLoaderBase's 'addNativeJarLibs(Class classFromJavaJar, String allNativeJarBaseName, String[] atomicNativeJarBaseNames)' now just attempts to load the 'all' variant, and will continue w/ atomics if not successful (ie not available). It skips the validation of a 'allJavaJarPrefix', ie validating the 'classFromJavaJar holding JAR file, which allows GLUEGEN/JOGL classes to be contained in JAR files other than the original. --- .../com/jogamp/common/jvm/JNILibLoaderBase.java | 34 ++++++----- .../jogamp/common/util/cache/TempFileCache.java | 21 ++++++- .../com/jogamp/common/util/cache/TempJarCache.java | 66 +++++++++++++++------- 3 files changed, 88 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java index 31e777a..c0addfc 100644 --- a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java +++ b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java @@ -144,7 +144,7 @@ public class JNILibLoaderBase { * * @param classFromJavaJar GLProfile * @param nativeJarBaseName jogl-all - * @return + * @return true if the native JAR file loaded successful or were loaded already, false in case of an error */ public static final boolean addNativeJarLibs(Class classFromJavaJar, String nativeJarBaseName) { if(TempJarCache.isInitialized()) { @@ -163,35 +163,42 @@ public class JNILibLoaderBase { if(DEBUG) { System.err.println("JNILibLoaderBase: addNativeJarLibs: "+nativeJarBaseName+": nativeJar "+nativeJar.getName()); } - return TempJarCache.addNativeLibs(classFromJavaJar, nativeJar); + TempJarCache.addNativeLibs(classFromJavaJar, nativeJar); + return true; } catch (IOException ioe) { ioe.printStackTrace(); + } catch (Exception e0) { + e0.printStackTrace(); } } return false; } /** - * @param classFromJavaJar GLProfile - * @param allJavaJarPrefix "jogl.all" - * @param allNativeJarBaseName "jogl-all" - * @param atomicNativeJarBaseNames [ "nativewindow", "jogl", "newt" ] + * @param classFromJavaJar A class file to determine the base URL of the native JAR files, eg.: GLProfile.class + * @param allNativeJarBaseName Attempt to use the 'all' native JAR variant first, if exists. Eg. "jogl-all" + * @param atomicNativeJarBaseNames Fallback to use all the atomic native JAR files, eg. [ "nativewindow", "jogl", "newt" ] + * @return true if either the 'all' native JAR or all of the atomic native JARs loaded successful or were loaded already, + * false in case of an error */ - public static void addNativeJarLibs(Class classFromJavaJar, String allJavaJarPrefix, String allNativeJarBaseName, String[] atomicNativeJarBaseNames) { + public static boolean addNativeJarLibs(Class classFromJavaJar, String allNativeJarBaseName, String[] atomicNativeJarBaseNames) { + boolean res = false; if(TempJarCache.isInitialized()) { final ClassLoader cl = classFromJavaJar.getClassLoader(); try { final String jarName = JarUtil.getJarBasename(classFromJavaJar.getName(), cl); if(jarName!=null) { - if( null != allJavaJarPrefix && jarName.startsWith(allJavaJarPrefix) ) { - // all-in-one variant - JNILibLoaderBase.addNativeJarLibs(classFromJavaJar, allNativeJarBaseName); - } else if(null != atomicNativeJarBaseNames) { + if(!res && null != allNativeJarBaseName) { + // all-in-one variant 1st + res = JNILibLoaderBase.addNativeJarLibs(classFromJavaJar, allNativeJarBaseName); + } + if(!res && null != atomicNativeJarBaseNames) { // atomic variant - for(int i=0; i0) { - JNILibLoaderBase.addNativeJarLibs(classFromJavaJar, atomicNativeJarBaseName); + res = JNILibLoaderBase.addNativeJarLibs(classFromJavaJar, atomicNativeJarBaseName); } } } @@ -200,6 +207,7 @@ public class JNILibLoaderBase { ioe.printStackTrace(); } } + return res; } protected static synchronized boolean loadLibrary(String libname, boolean ignoreError) { diff --git a/src/java/com/jogamp/common/util/cache/TempFileCache.java b/src/java/com/jogamp/common/util/cache/TempFileCache.java index 4210a24..c3b24aa 100644 --- a/src/java/com/jogamp/common/util/cache/TempFileCache.java +++ b/src/java/com/jogamp/common/util/cache/TempFileCache.java @@ -393,10 +393,10 @@ public class TempFileCache { } } } - path.delete(); } + /** Create the individualTmpDir. */ public TempFileCache () { if (DEBUG) { System.err.println("TempFileCache: new TempFileCache() --------------------- (static ok: "+(!staticInitError)+")"); @@ -416,6 +416,25 @@ public class TempFileCache { } } + /** Delete the individualTmpDir recursively and remove it's reference. */ + public void destroy() { + if (DEBUG) { + System.err.println("TempFileCache: destroy() --------------------- (static ok: "+(!staticInitError)+")"); + System.err.println("TempFileCache: Thread: "+Thread.currentThread().getName()+", CL 0x"+Integer.toHexString(TempFileCache.class.getClassLoader().hashCode())+", this 0x"+Integer.toHexString(hashCode())); + } + if(!staticInitError) { + try { + removeAll(individualTmpDir); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + individualTmpDir = null; + if (DEBUG) { + System.err.println("TempFileCache: destroy() END"); + } + } + /** * @return true is static and object initialization was successful */ diff --git a/src/java/com/jogamp/common/util/cache/TempJarCache.java b/src/java/com/jogamp/common/util/cache/TempJarCache.java index 2286d6c..f0512dc 100644 --- a/src/java/com/jogamp/common/util/cache/TempJarCache.java +++ b/src/java/com/jogamp/common/util/cache/TempJarCache.java @@ -97,6 +97,37 @@ public class TempJarCache { return !staticInitError; } + /** + * This is not recommended since the JNI libraries may still be + * in use by the ClassLoader they are loaded via {@link System#load(String)}. + *

+ *

+ * In JogAmp, JNI native libraries loaded and registered by {@link JNILibLoaderBase} + * derivations, where the native JARs might be loaded via {@link JNILibLoaderBase#addNativeJarLibs(Class, String) }. + *

+ public static void shutdown() { + if (isInit) { // volatile: ok + synchronized (TempJarCache.class) { + if (isInit) { + isInit = false; + if(!staticInitError) { + nativeLibMap.clear(); + nativeLibMap = null; + nativeLibJars.clear(); + nativeLibJars = null; + classFileJars.clear(); + classFileJars = null; + resourceFileJars.clear(); + resourceFileJars = null; + + tmpFileCache.destroy(); + tmpFileCache = null; + } + } + } + } + } */ + /** * * @return true if this class has been properly initialized, ie. is in use, otherwise false. @@ -116,31 +147,38 @@ public class TempJarCache { return tmpFileCache; } - public static boolean contains(JarFile jarFile) throws IOException { + public static boolean containsNativeLibs(JarFile jarFile) throws IOException { checkInitialized(); return nativeLibJars.contains(jarFile); } + public static boolean containsClasses(JarFile jarFile) throws IOException { + checkInitialized(); + return classFileJars.contains(jarFile); + } + + public static boolean containsResources(JarFile jarFile) throws IOException { + checkInitialized(); + return resourceFileJars.contains(jarFile); + } + /** * Adds native libraries, if not yet added. * * @param certClass if class is certified, the JarFile entries needs to have the same certificate * @param jarFile * - * @return * @throws IOException * @throws SecurityException */ - public static final boolean addNativeLibs(Class certClass, JarFile jarFile) throws IOException, SecurityException { + public static final void addNativeLibs(Class certClass, JarFile jarFile) throws IOException, SecurityException { checkInitialized(); if(!nativeLibJars.contains(jarFile)) { validateCertificates(certClass, jarFile); JarUtil.extract(tmpFileCache.getTempDir(), nativeLibMap, jarFile, true, false, false); nativeLibJars.add(jarFile); - return true; } - return false; } /** @@ -152,20 +190,17 @@ public class TempJarCache { * @param certClass if class is certified, the JarFile entries needs to have the same certificate * @param jarFile * - * @return * @throws IOException * @throws SecurityException */ - public static final boolean addClasses(Class certClass, JarFile jarFile) throws IOException, SecurityException { + public static final void addClasses(Class certClass, JarFile jarFile) throws IOException, SecurityException { checkInitialized(); if(!classFileJars.contains(jarFile)) { validateCertificates(certClass, jarFile); JarUtil.extract(tmpFileCache.getTempDir(), null, jarFile, false, true, false); classFileJars.add(jarFile); - return true; } - return false; } /** @@ -178,16 +213,14 @@ public class TempJarCache { * @throws IOException * @throws SecurityException */ - public static final boolean addResources(Class certClass, JarFile jarFile) throws IOException, SecurityException { + public static final void addResources(Class certClass, JarFile jarFile) throws IOException, SecurityException { checkInitialized(); if(!resourceFileJars.contains(jarFile)) { validateCertificates(certClass, jarFile); JarUtil.extract(tmpFileCache.getTempDir(), null, jarFile, false, false, true); resourceFileJars.add(jarFile); - return true; } - return false; } /** @@ -200,11 +233,10 @@ public class TempJarCache { * @param certClass if class is certified, the JarFile entries needs to have the same certificate * @param jarFile * - * @return * @throws IOException * @throws SecurityException */ - public static final boolean addAll(Class certClass, JarFile jarFile) throws IOException, SecurityException { + public static final void addAll(Class certClass, JarFile jarFile) throws IOException, SecurityException { checkInitialized(); if(!nativeLibJars.contains(jarFile) || !classFileJars.contains(jarFile) || @@ -224,9 +256,7 @@ public class TempJarCache { if(extractOtherFiles) { resourceFileJars.add(jarFile); } - return true; } - return false; } public static final String findLibrary(String libName) { @@ -289,7 +319,7 @@ public class TempJarCache { * @throws IOException * @throws SecurityException */ - public static final boolean bootstrapNativeLib(Class certClass, String libBaseName, JarFile jarFile) + public static final void bootstrapNativeLib(Class certClass, String libBaseName, JarFile jarFile) throws IOException, SecurityException { checkInitialized(); if(!nativeLibJars.contains(jarFile) && !nativeLibMap.containsKey(libBaseName) ) { @@ -319,12 +349,10 @@ public class TempJarCache { if (numBytes>0) { nativeLibMap.put(libBaseName, destFile.getAbsolutePath()); nativeLibJars.add(jarFile); - return true; } } } } - return false; } private static void validateCertificates(Class certClass, JarFile jarFile) throws IOException, SecurityException { -- cgit v1.2.3