From 9a71703904ebfec343fb2c7266343d37a2e4c3db Mon Sep 17 00:00:00 2001
From: Sven Gothel
- * Impact: Less overhead and more robustness.
- *
+ * The native library JAR file's URL is derived as follows:
+ *
+ *
+ * Where:
+ * GLProfile.class
-> http://lala/
-> http://lala/'nativeJarBaseName'-'os.and.arch'.jar
+ *
*
* @param classFromJavaJar GLProfile
- * @param nativeJarBaseName jogl-all
+ * @param nativeJarBasename jogl-all
* @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) {
+ public static final boolean addNativeJarLibs(Class> classFromJavaJar, String nativeJarBasename) {
if(TempJarCache.isInitialized()) {
- final String nativeJarName = nativeJarBaseName+"-natives-"+PlatformPropsImpl.os_and_arch+".jar";
- final ClassLoader cl = classFromJavaJar.getClassLoader();
+ final StringBuilder msg = new StringBuilder();
try {
- URL jarUrlRoot = JarUtil.getURLDirname( JarUtil.getJarSubURL( classFromJavaJar.getName(), cl ) );
- if(DEBUG) {
- System.err.println("JNILibLoaderBase: addNativeJarLibs: "+nativeJarBaseName+": url-root "+jarUrlRoot);
- }
- URL nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);
- if(DEBUG) {
- System.err.println("JNILibLoaderBase: addNativeJarLibs: "+nativeJarBaseName+": nativeJarURL "+nativeJarURL);
- }
- TempJarCache.addNativeLibs(classFromJavaJar, nativeJarURL, cl);
- return true;
+ final URL classJarURL = JarUtil.getJarURL(classFromJavaJar.getName(), classFromJavaJar.getClassLoader());
+ return addNativeJarLibsImpl(classFromJavaJar, classJarURL, nativeJarBasename, msg);
} catch (Exception e0) {
// IllegalArgumentException, IOException
- System.err.println("Catched: "+e0.getMessage());
+ System.err.println("Catched "+e0.getClass().getSimpleName()+": "+e0.getMessage()+", while "+msg.toString());
if(DEBUG) {
e0.printStackTrace();
}
- }
+ }
+ } else if(DEBUG) {
+ System.err.println("JNILibLoaderBase: addNativeJarLibs1: disabled due to uninitialized TempJarCache");
}
return false;
}
/**
- * @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,
+ * Loads and adds a JAR file's native library to the TempJarCache.classFromJavaJar
+ * The native library JAR file's URL is derived as follows:
+ *
+ *
+ * Where:
+ * GLProfile.class
-> http://lala/gluegen-rt.jar
-> http://lala/gluegen-rt
-> http://lala/gluegen-rt-natives-'os.and.arch'.jar
+ *
+ *
+ * Examples:classesFromJavaJars
+ *
+ * JOCL:
+ *
+ // only: jocl.jar -> jocl-natives-'os.and.arch'.jar
+ addNativeJarLibs(new Class>[] { JOCLJNILibLoader.class }, null, null );
+ *
+ *
+ * Newt Only:
+ *
+ // either: [jogl-all.jar, jogl-all-noawt.jar, jogl-all-mobile.jar] -> jogl-all-natives-
+ *
+ * JOGL:
+ *
+ final ClassLoader cl = GLProfile.class.getClassLoader();
+ // either: [jogl-all.jar, jogl-all-noawt.jar, jogl-all-mobile.jar] -> jogl-all-natives-
+ *
+ * @param classesFromJavaJars For each given Class, load the native library JAR.
+ * @param singleJarMarker Optional string marker like "-all" to identify the single 'all-in-one' JAR file
+ * after which processing of the class array shall stop.
+ * @param stripBasenameSuffixes Optional substrings to be stripped of the base URL
+ *
+ * @return true if either the 'all-in-one' native JAR or all native JARs loaded successful or were loaded already,
* false in case of an error
*/
- public static boolean addNativeJarLibs(Class> classFromJavaJar, String allNativeJarBaseName, String[] atomicNativeJarBaseNames) {
- boolean res = false;
+ public static boolean addNativeJarLibs(Class>[] classesFromJavaJars, String singleJarMarker, String[] stripBasenameSuffixes) {
+ if(DEBUG) {
+ System.err.println("JNILibLoaderBase: addNativeJarLibs0(classesFromJavaJars "+Arrays.asList(classesFromJavaJars)+", singleJarMarker "+singleJarMarker+", stripBasenameSuffixes "+Arrays.asList(stripBasenameSuffixes));
+ }
+ boolean ok = false;
if(TempJarCache.isInitialized()) {
- final ClassLoader cl = classFromJavaJar.getClassLoader();
- try {
- final String jarName = JarUtil.getJarBasename(classFromJavaJar.getName(), cl);
- if(jarName!=null) {
- if(!res && null != allNativeJarBaseName) {
- // all-in-one variant 1st
- res = JNILibLoaderBase.addNativeJarLibs(classFromJavaJar, allNativeJarBaseName);
- }
- if(!res && null != atomicNativeJarBaseNames) {
- // atomic variant
- res = true;
- for(int i=0; res && ifalse
. */
public static final boolean AWT_AVAILABLE;
+ private static final URL platformClassJarURL;
+
static {
PlatformPropsImpl.initSingleton(); // just documenting the order of static initialization
-
+
+ {
+ URL _platformClassJarURL;
+ try {
+ _platformClassJarURL = JarUtil.getJarURL(Platform.class.getName(), Platform.class.getClassLoader());
+ } catch (Exception e) {
+ _platformClassJarURL = null;
+ }
+ platformClassJarURL = _platformClassJarURL;
+ }
+
USE_TEMP_JAR_CACHE = (OS_TYPE != OSType.ANDROID) && isRunningFromJarURL() &&
Debug.getBooleanProperty(useTempJarCachePropName, true, true);
@@ -209,35 +221,32 @@ public class Platform extends PlatformPropsImpl {
private Platform() {}
/**
- * Preemptively avoids initializing and using {@link TempJarCache} in case we are not running
- * from a Jar URL, ie. plain class files. Used to set {@link USE_TEMP_JAR_CACHE}.
- *
"com.jogamp.common.GlueGenVersion"
- * URL jar:sub_protocol:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class"
+ * The Class's Jar URL jar:sub_protocol:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
* Jar basename gluegen-rt.jar
will be returned.
* * sub_protocol may be "file", "http", etc.. *
* - * @param clazzBinName "com.jogamp.common.GlueGenVersion" + * @param classJarURL as retrieved w/ {@link #getJarURL(String, ClassLoader) getJarURL("com.jogamp.common.GlueGenVersion", cl)}, + * i.e.jar:sub_protocol:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
+ * @return gluegen-rt.jar
+ * @throws IllegalArgumentException if the URL doesn't match the expected formatting or is null
+ * @see {@link IOUtil#getClassURL(String, ClassLoader)}
+ */
+ public static String getJarBasename(URL classJarURL) throws IllegalArgumentException {
+ if(null == classJarURL) {
+ throw new IllegalArgumentException("URL is null");
+ }
+ String urlS = classJarURL.toExternalForm();
+ urlS = urlS.substring(4, urlS.length()); // exclude 'jar:'
+
+ // from
+ // file:/some/path/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
+ // to
+ // file:/some/path/gluegen-rt.jar
+ int idx = urlS.lastIndexOf('!');
+ if (0 <= idx) {
+ urlS = urlS.substring(0, idx); // exclude '!/'
+ } else {
+ throw new IllegalArgumentException("JAR URL does not contain jar url terminator '!', in <"+classJarURL.toExternalForm()+">, got <"+urlS+">");
+ }
+
+ // from
+ // file:/some/path/gluegen-rt.jar
+ // to
+ // gluegen-rt.jar
+ idx = urlS.lastIndexOf('/');
+ if(0 > idx) {
+ // no abs-path, check for protocol terminator ':'
+ idx = urlS.lastIndexOf(':');
+ if(0 > idx) {
+ throw new IllegalArgumentException("JAR URL does not contain protocol terminator ':', in <"+classJarURL.toExternalForm()+">, got <"+urlS+">");
+ }
+ }
+ urlS = urlS.substring(idx+1); // just the jar name
+
+ if(0 >= urlS.lastIndexOf(".jar")) {
+ throw new IllegalArgumentException("No Jar name in <"+classJarURL.toExternalForm()+">, got <"+urlS+">");
+ }
+ if(DEBUG) {
+ System.out.println("getJarName res: "+urlS);
+ }
+ return urlS;
+ }
+
+ /**
+ * The Class's com.jogamp.common.GlueGenVersion
+ * URL jar:sub_protocol:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
+ * Jar basename gluegen-rt.jar
will be returned.
+ * + * sub_protocol may be "file", "http", etc.. + *
+ * + * @param clazzBinNamecom.jogamp.common.GlueGenVersion
* @param cl
- * @return "gluegen-rt.jar"
+ * @return gluegen-rt.jar
* @throws IllegalArgumentException if the URL doesn't match the expected formatting
- * @throws IOException
+ * @throws IOException if the class's Jar file could not been found by the ClassLoader
* @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
- // file:/some/path/gluegen-rt.jar!/com/jogamp/common/util/cache/TempJarCache.class
- // to
- // file:/some/path/gluegen-rt.jar
- int idx = urlS.lastIndexOf('!');
- if (0 <= idx) {
- urlS = urlS.substring(0, idx); // exclude '!/'
- } else {
- throw new IllegalArgumentException("JAR URL does not contain jar url terminator '!', in <"+url.toExternalForm()+">, got <"+urlS+">");
- }
-
- // from
- // file:/some/path/gluegen-rt.jar
- // to
- // gluegen-rt.jar
- idx = urlS.lastIndexOf('/');
- if(0 > idx) {
- // no abs-path, check for protocol terminator ':'
- idx = urlS.lastIndexOf(':');
- if(0 > idx) {
- throw new IllegalArgumentException("JAR URL does not contain protocol terminator ':', in <"+url.toExternalForm()+">, got <"+urlS+">");
- }
- }
- urlS = urlS.substring(idx+1); // just the jar name
-
- if(0 >= urlS.lastIndexOf(".jar")) {
- throw new IllegalArgumentException("No Jar name in <"+url.toExternalForm()+">, got <"+urlS+">");
- }
- if(DEBUG) {
- System.out.println("getJarName res: "+urlS);
- }
- return urlS;
+ return getJarBasename(getJarURL(clazzBinName, cl));
+ }
+
+ /**
+ * The Class's Jar URL jar:sub_protocol:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
+ * Jar file's sub URL sub_protocol:/some/path/gluegen-rt.jar
will be returned.
+ * + * sub_protocol may be "file", "http", etc.. + *
+ * + * @param classJarURL as retrieved w/ {@link #getJarURL(String, ClassLoader) getJarURL("com.jogamp.common.GlueGenVersion", cl)}, + * i.e.jar:sub_protocol:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
+ * @param cl
+ * @return sub_protocol:/some/path/gluegen-rt.jar
+ * @throws IllegalArgumentException if the URL doesn't match the expected formatting or is null
+ * @throws MalformedURLException if the computed URL specifies an unknown protocol
+ * @see {@link IOUtil#getClassURL(String, ClassLoader)}
+ */
+ public static URL getJarSubURL(URL classJarURL) throws IllegalArgumentException, MalformedURLException {
+ if(null == classJarURL) {
+ throw new IllegalArgumentException("URL is null");
}
- return null;
+ String urlS = classJarURL.toExternalForm();
+ urlS = urlS.substring(4, urlS.length()); // exclude 'jar:'
+
+ // from
+ // file:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
+ // to
+ // file:/some/path/gluegen-rt.jar
+ int idx = urlS.lastIndexOf('!');
+ if (0 <= idx) {
+ urlS = urlS.substring(0, idx); // exclude '!/'
+ } else {
+ throw new IllegalArgumentException("JAR URL does not contain jar url terminator '!', url <"+urlS+">");
+ }
+
+ if(0 >= urlS.lastIndexOf(".jar")) {
+ throw new IllegalArgumentException("No Jar name in <"+classJarURL.toExternalForm()+">, got <"+urlS+">");
+ }
+ if(DEBUG) {
+ System.out.println("getJarSubURL res: "+urlS);
+ }
+ return new URL(urlS);
}
/**
- * The Class's "com.jogamp.common.GlueGenVersion"
- * URL jar:sub_protocol:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class"
+ * The Class's com.jogamp.common.GlueGenVersion
+ * URL jar:sub_protocol:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
* Jar file's sub URL sub_protocol:/some/path/gluegen-rt.jar
will be returned.
* * sub_protocol may be "file", "http", etc.. *
* - * @param clazzBinName "com.jogamp.common.GlueGenVersion" + * @param clazzBinNamecom.jogamp.common.GlueGenVersion
* @param cl
- * @return "sub_protocol:/some/path/gluegen-rt.jar"
+ * @return sub_protocol:/some/path/gluegen-rt.jar
* @throws IllegalArgumentException if the URL doesn't match the expected formatting
- * @throws IOException
+ * @throws IOException if the class's Jar file could not been found by the ClassLoader
* @see {@link IOUtil#getClassURL(String, ClassLoader)}
*/
public static URL getJarSubURL(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
- // file:/some/path/gluegen-rt.jar!/com/jogamp/common/GlueGenVersion.class
- // to
- // file:/some/path/gluegen-rt.jar
- int idx = urlS.lastIndexOf('!');
- if (0 <= idx) {
- urlS = urlS.substring(0, idx); // exclude '!/'
- } else {
- throw new IllegalArgumentException("JAR URL does not contain jar url terminator '!', url <"+urlS+">");
- }
-
- if(0 >= urlS.lastIndexOf(".jar")) {
- throw new IllegalArgumentException("No Jar name in <"+url.toExternalForm()+">, got <"+urlS+">");
- }
- if(DEBUG) {
- System.out.println("getJarSubURL res: "+urlS);
- }
- return new URL(urlS);
- }
- return null;
+ return getJarSubURL(getJarURL(clazzBinName, cl));
}
/**
@@ -213,20 +250,20 @@ public class JarUtil {
* @param clazzBinName "com.jogamp.common.GlueGenVersion"
* @param cl
* @return "jar:sub_protocol:/some/path/gluegen-rt.jar!/"
- * @throws IllegalArgumentException if the URL doesn't match the expected formatting
- * @throws IOException
+ * @throws IllegalArgumentException if the URL doesn't match the expected formatting or null arguments
+ * @throws IOException if the class's Jar file could not been found by the ClassLoader
* @see {@link IOUtil#getClassURL(String, ClassLoader)}
*/
public static URL getJarFileURL(String clazzBinName, ClassLoader cl) throws IllegalArgumentException, IOException {
+ if(null == clazzBinName || null == cl) {
+ throw new IllegalArgumentException("null arguments: clazzBinName "+clazzBinName+", cl "+cl);
+ }
URL url = getJarSubURL(clazzBinName, cl);
- if(null != url) {
- url = new URL("jar:"+url.toExternalForm()+"!/");
- if(DEBUG) {
- System.out.println("getJarFileURL res: "+url);
- }
- return url;
+ url = new URL("jar:"+url.toExternalForm()+"!/");
+ if(DEBUG) {
+ System.out.println("getJarFileURL res: "+url);
}
- return null;
+ return url;
}
/**
@@ -238,10 +275,13 @@ public class JarUtil {
*
* @param aURL "protocol:/some/path/gluegen-rt.jar"
* @return "protocol:/some/path/"
- * @throws IllegalArgumentException if the URL doesn't match the expected formatting
- * @throws IOException
+ * @throws IllegalArgumentException if the URL doesn't match the expected formatting, or is null
+ * @throws MalformedURLException
*/
- public static URL getURLDirname(URL aURL) throws IllegalArgumentException, IOException {
+ public static URL getURLDirname(URL aURL) throws IllegalArgumentException, MalformedURLException {
+ if(null == aURL) {
+ throw new IllegalArgumentException("URL is null");
+ }
String urlS = aURL.toExternalForm();
if(DEBUG) {
System.out.println("getURLDirname "+aURL+", extForm: "+urlS);
@@ -270,11 +310,12 @@ public class JarUtil {
* @param baseUrl file:/some/path/
* @param jarFileName gluegen-rt.jar
* @return jar:file:/some/path/gluegen-rt.jar!/
- * @throws IOException
+ * @throws MalformedURLException
+ * @throws IllegalArgumentException null arguments
*/
- public static URL getJarFileURL(URL baseUrl, String jarFileName) throws IOException {
- if(null == jarFileName) {
- throw new IllegalArgumentException("jarFileName is null");
+ public static URL getJarFileURL(URL baseUrl, String jarFileName) throws IOException, MalformedURLException {
+ if(null == baseUrl || null == jarFileName) {
+ throw new IllegalArgumentException("null arguments: baseUrl "+baseUrl+", jarFileName "+jarFileName);
}
return new URL("jar:"+baseUrl.toExternalForm()+jarFileName+"!/");
}
@@ -282,9 +323,10 @@ public class JarUtil {
/**
* @param jarSubUrl file:/some/path/gluegen-rt.jar
* @return jar:file:/some/path/gluegen-rt.jar!/
- * @throws IOException
+ * @throws MalformedURLException
+ * @throws IllegalArgumentException null arguments
*/
- public static URL getJarFileURL(URL jarSubUrl) throws IOException {
+ public static URL getJarFileURL(URL jarSubUrl) throws MalformedURLException, IllegalArgumentException {
if(null == jarSubUrl) {
throw new IllegalArgumentException("jarSubUrl is null");
}
@@ -295,9 +337,10 @@ public class JarUtil {
* @param jarFileURL 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 IOException
+ * @throws MalformedURLException
+ * @throws IllegalArgumentException null arguments
*/
- public static URL getJarEntryURL(URL jarFileURL, String jarEntry) throws IOException {
+ public static URL getJarEntryURL(URL jarFileURL, String jarEntry) throws MalformedURLException, IllegalArgumentException {
if(null == jarEntry) {
throw new IllegalArgumentException("jarEntry is null");
}
@@ -308,33 +351,35 @@ public class JarUtil {
* @param clazzBinName com.jogamp.common.util.cache.TempJarCache
* @param cl domain
* @return JarFile containing the named class within the given ClassLoader
- * @throws IOException
+ * @throws IOException if the class's Jar file could not been found by the ClassLoader
+ * @throws IllegalArgumentException null arguments
* @see {@link #getJarFileURL(String, ClassLoader)}
*/
- public static JarFile getJarFile(String clazzBinName, ClassLoader cl) throws IOException {
- return getJarFile(getJarFileURL(clazzBinName, cl), cl);
+ public static JarFile getJarFile(String clazzBinName, ClassLoader cl) throws IOException, IllegalArgumentException {
+ return getJarFile(getJarFileURL(clazzBinName, cl));
}
/**
* @param jarFileURL jar:file:/some/path/gluegen-rt.jar!/
- * @param cl domain
* @return JarFile as named by URL within the given ClassLoader
- * @throws IOException
+ * @throws IllegalArgumentException null arguments
+ * @throws IOException if the Jar file could not been found
*/
- public static JarFile getJarFile(URL jarFileUrl, ClassLoader cl) throws IOException {
+ public static JarFile getJarFile(URL jarFileUrl) throws IOException, IllegalArgumentException {
+ if(null == jarFileUrl) {
+ throw new IllegalArgumentException("null jarFileUrl");
+ }
if(DEBUG) {
System.out.println("getJarFile: "+jarFileUrl);
}
- if(null != jarFileUrl) {
- URLConnection urlc = jarFileUrl.openConnection();
- if(urlc instanceof JarURLConnection) {
- JarURLConnection jarConnection = (JarURLConnection)jarFileUrl.openConnection();
- JarFile jarFile = jarConnection.getJarFile();
- if(DEBUG) {
- System.out.println("getJarFile res: "+jarFile.getName());
- }
- return jarFile;
- }
+ URLConnection urlc = jarFileUrl.openConnection();
+ if(urlc instanceof JarURLConnection) {
+ JarURLConnection jarConnection = (JarURLConnection)jarFileUrl.openConnection();
+ JarFile jarFile = jarConnection.getJarFile();
+ if(DEBUG) {
+ System.out.println("getJarFile res: "+jarFile.getName());
+ }
+ return jarFile;
}
if(DEBUG) {
System.out.println("getJarFile res: NULL");
diff --git a/src/java/com/jogamp/common/util/cache/TempJarCache.java b/src/java/com/jogamp/common/util/cache/TempJarCache.java
index e4a77fe..6f24c68 100644
--- a/src/java/com/jogamp/common/util/cache/TempJarCache.java
+++ b/src/java/com/jogamp/common/util/cache/TempJarCache.java
@@ -39,9 +39,7 @@ import java.net.URL;
import java.security.cert.Certificate;
import java.util.Enumeration;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Map;
-import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@@ -62,10 +60,24 @@ public class TempJarCache {
// unpacked library file in nativeTmpDir.
private static MapjarURL
could not be loaded or a previous load attempt failed
* @throws SecurityException
*/
- public static final void addNativeLibs(Class> certClass, URL jarURL, ClassLoader cl) throws IOException, SecurityException {
- if(!containsNativeLibs(jarURL)) {
- final JarFile jarFile = JarUtil.getJarFile(jarURL, cl);
+ public synchronized static final void addNativeLibs(Class> certClass, URL jarURL) throws IOException, SecurityException {
+ final LoadState nativeLibJarsLS = nativeLibJars.get(jarURL);
+ if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) ) {
+ nativeLibJars.put(jarURL, LoadState.LOOKED_UP);
+ final JarFile jarFile = JarUtil.getJarFile(jarURL);
if(DEBUG) {
System.err.println("TempJarCache: addNativeLibs: "+jarURL+": nativeJar "+jarFile.getName());
}
validateCertificates(certClass, jarFile);
JarUtil.extract(tmpFileCache.getTempDir(), nativeLibMap, jarFile,
true, false, false);
- nativeLibJars.add(jarURL);
+ nativeLibJars.put(jarURL, LoadState.LOADED);
+ } else if( !testLoadState(nativeLibJarsLS, LoadState.LOADED) ) {
+ throw new IOException("TempJarCache: addNativeLibs: "+jarURL+", previous load attempt failed");
}
}
@@ -217,20 +232,23 @@ public class TempJarCache {
*
* @param certClass if class is certified, the JarFile entries needs to have the same certificate
* @param jarFile
- *
- * @throws IOException
+ * @throws IOException if the jarURL
could not be loaded or a previous load attempt failed
* @throws SecurityException
*/
- public static final void addClasses(Class> certClass, URL jarURL, ClassLoader cl) throws IOException, SecurityException {
- if(!containsClasses(jarURL)) {
- final JarFile jarFile = JarUtil.getJarFile(jarURL, cl);
+ public synchronized static final void addClasses(Class> certClass, URL jarURL) throws IOException, SecurityException {
+ final LoadState classFileJarsLS = classFileJars.get(jarURL);
+ if( !testLoadState(classFileJarsLS, LoadState.LOOKED_UP) ) {
+ classFileJars.put(jarURL, LoadState.LOOKED_UP);
+ final JarFile jarFile = JarUtil.getJarFile(jarURL);
if(DEBUG) {
System.err.println("TempJarCache: addClasses: "+jarURL+": nativeJar "+jarFile.getName());
}
validateCertificates(certClass, jarFile);
JarUtil.extract(tmpFileCache.getTempDir(), null, jarFile,
false, true, false);
- classFileJars.add(jarURL);
+ classFileJars.put(jarURL, LoadState.LOADED);
+ } else if( !testLoadState(classFileJarsLS, LoadState.LOADED) ) {
+ throw new IOException("TempJarCache: addClasses: "+jarURL+", previous load attempt failed");
}
}
@@ -239,21 +257,24 @@ public class TempJarCache {
*
* @param certClass if class is certified, the JarFile entries needs to have the same certificate
* @param jarFile
- *
* @return
- * @throws IOException
+ * @throws IOException if the jarURL
could not be loaded or a previous load attempt failed
* @throws SecurityException
*/
- public static final void addResources(Class> certClass, URL jarURL, ClassLoader cl) throws IOException, SecurityException {
- if(!containsResources(jarURL)) {
- final JarFile jarFile = JarUtil.getJarFile(jarURL, cl);
+ public synchronized static final void addResources(Class> certClass, URL jarURL) throws IOException, SecurityException {
+ final LoadState resourceFileJarsLS = resourceFileJars.get(jarURL);
+ if( !testLoadState(resourceFileJarsLS, LoadState.LOOKED_UP) ) {
+ resourceFileJars.put(jarURL, LoadState.LOOKED_UP);
+ final JarFile jarFile = JarUtil.getJarFile(jarURL);
if(DEBUG) {
System.err.println("TempJarCache: addResources: "+jarURL+": nativeJar "+jarFile.getName());
}
validateCertificates(certClass, jarFile);
JarUtil.extract(tmpFileCache.getTempDir(), null, jarFile,
false, false, true);
- resourceFileJars.add(jarURL);
+ resourceFileJars.put(jarURL, LoadState.LOADED);
+ } else if( !testLoadState(resourceFileJarsLS, LoadState.LOADED) ) {
+ throw new IOException("TempJarCache: addResources: "+jarURL+", previous load attempt failed");
}
}
@@ -266,41 +287,62 @@ public class TempJarCache {
*
* @param certClass if class is certified, the JarFile entries needs to have the same certificate
* @param jarFile
- *
- * @throws IOException
+ * @throws IOException if the jarURL
could not be loaded or a previous load attempt failed
* @throws SecurityException
*/
- public static final void addAll(Class> certClass, URL jarURL, ClassLoader cl) throws IOException, SecurityException {
+ public synchronized static final void addAll(Class> certClass, URL jarURL) throws IOException, SecurityException {
checkInitialized();
if(null == jarURL) {
throw new IllegalArgumentException("jarURL is null");
}
- if(!nativeLibJars.contains(jarURL) ||
- !classFileJars.contains(jarURL) ||
- !resourceFileJars.contains(jarURL)) {
- final JarFile jarFile = JarUtil.getJarFile(jarURL, cl);
+ final LoadState nativeLibJarsLS = nativeLibJars.get(jarURL);
+ final LoadState classFileJarsLS = classFileJars.get(jarURL);
+ final LoadState resourceFileJarsLS = resourceFileJars.get(jarURL);
+ if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) ||
+ !testLoadState(classFileJarsLS, LoadState.LOOKED_UP) ||
+ !testLoadState(resourceFileJarsLS, LoadState.LOOKED_UP) ) {
+
+ final boolean extractNativeLibraries = !testLoadState(nativeLibJarsLS, LoadState.LOADED);
+ final boolean extractClassFiles = !testLoadState(classFileJarsLS, LoadState.LOADED);
+ final boolean extractOtherFiles = !testLoadState(resourceFileJarsLS, LoadState.LOOKED_UP);
+
+ // mark looked-up (those who are not loaded)
+ if(extractNativeLibraries) {
+ nativeLibJars.put(jarURL, LoadState.LOOKED_UP);
+ }
+ if(extractClassFiles) {
+ classFileJars.put(jarURL, LoadState.LOOKED_UP);
+ }
+ if(extractOtherFiles) {
+ resourceFileJars.put(jarURL, LoadState.LOOKED_UP);
+ }
+
+ final JarFile jarFile = JarUtil.getJarFile(jarURL);
if(DEBUG) {
System.err.println("TempJarCache: addAll: "+jarURL+": nativeJar "+jarFile.getName());
}
- final boolean extractNativeLibraries = !nativeLibJars.contains(jarURL);
- final boolean extractClassFiles = !classFileJars.contains(jarURL);
- final boolean extractOtherFiles = !resourceFileJars.contains(jarURL);
validateCertificates(certClass, jarFile);
JarUtil.extract(tmpFileCache.getTempDir(), nativeLibMap, jarFile,
extractNativeLibraries, extractClassFiles, extractOtherFiles);
+
+ // mark loaded (those were just loaded)
if(extractNativeLibraries) {
- nativeLibJars.add(jarURL);
+ nativeLibJars.put(jarURL, LoadState.LOADED);
}
if(extractClassFiles) {
- classFileJars.add(jarURL);
+ classFileJars.put(jarURL, LoadState.LOADED);
}
if(extractOtherFiles) {
- resourceFileJars.add(jarURL);
+ resourceFileJars.put(jarURL, LoadState.LOADED);
}
+ } else if( !testLoadState(nativeLibJarsLS, LoadState.LOADED) ||
+ !testLoadState(classFileJarsLS, LoadState.LOADED) ||
+ !testLoadState(resourceFileJarsLS, LoadState.LOADED) ) {
+ throw new IOException("TempJarCache: addAll: "+jarURL+", previous load attempt failed");
}
}
- public static final String findLibrary(String libName) {
+ public synchronized static final String findLibrary(String libName) {
checkInitialized();
// try with mapped library basename first
String path = nativeLibMap.get(libName);
@@ -331,7 +373,7 @@ public class TempJarCache {
return null;
} */
- public static final String findResource(String name) {
+ public synchronized static final String findResource(String name) {
checkInitialized();
final File f = new File(tmpFileCache.getTempDir(), name);
if(f.exists()) {
@@ -340,7 +382,7 @@ public class TempJarCache {
return null;
}
- public static final URL getResource(String name) throws MalformedURLException {
+ public synchronized static final URL getResource(String name) throws MalformedURLException {
checkInitialized();
final File f = new File(tmpFileCache.getTempDir(), name);
if(f.exists()) {
@@ -360,13 +402,20 @@ public class TempJarCache {
* @throws IOException
* @throws SecurityException
*/
- public static final void bootstrapNativeLib(Class> certClass, String libBaseName, URL jarURL, ClassLoader cl)
+ public synchronized static final void bootstrapNativeLib(Class> certClass, String libBaseName, URL jarURL)
throws IOException, SecurityException {
checkInitialized();
- if(!nativeLibJars.contains(jarURL) && !nativeLibMap.containsKey(libBaseName) ) {
- final JarFile jarFile = JarUtil.getJarFile(jarURL, cl);
+ boolean ok = false;
+ int countEntries = 0;
+ final LoadState nativeLibJarsLS = nativeLibJars.get(jarURL);
+ if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) && !nativeLibMap.containsKey(libBaseName) ) {
if(DEBUG) {
- System.err.println("TempJarCache: bootstrapNativeLib: "+jarURL+": nativeJar "+jarFile.getName()+" - libBaseName: "+libBaseName);
+ System.err.println("TempJarCache: bootstrapNativeLib(certClass: "+certClass+", libBaseName "+libBaseName+", jarURL "+jarURL+")");
+ }
+ nativeLibJars.put(jarURL, LoadState.LOOKED_UP);
+ final JarFile jarFile = JarUtil.getJarFile(jarURL);
+ if(DEBUG) {
+ System.err.println("TempJarCache: bootstrapNativeLib: nativeJar "+jarFile.getName());
}
validateCertificates(certClass, jarFile);
final Enumeration