diff options
author | Sven Gothel <[email protected]> | 2011-09-20 05:49:36 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-09-20 05:49:36 +0200 |
commit | 61d3b2f9b60cbea2e9454b86cc559347943a578e (patch) | |
tree | e2c395dffa279b29646e7be6dcc40513b8b82145 | |
parent | b12fe7f98a9b523129c310d46698f430d17a8138 (diff) |
Temp Cache: More control over the lifecycle, explicit TempJarCache initialization/usage only
4 files changed, 111 insertions, 29 deletions
diff --git a/src/java/com/jogamp/common/util/cache/TempCacheReg.java b/src/java/com/jogamp/common/util/cache/TempCacheReg.java new file mode 100644 index 0000000..47ef584 --- /dev/null +++ b/src/java/com/jogamp/common/util/cache/TempCacheReg.java @@ -0,0 +1,37 @@ +/** + * Copyright 2011 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.common.util.cache; + +public class TempCacheReg { + public static boolean isTempFileCacheUsed() { + return null != System.getProperty(TempFileCache.tmpRootPropName); + } + public static boolean isTempJarCacheUsed() { + return TempJarCache.isInitialized(); + } +} diff --git a/src/java/com/jogamp/common/util/cache/TempFileCache.java b/src/java/com/jogamp/common/util/cache/TempFileCache.java index 2145239..ef5a2a2 100644 --- a/src/java/com/jogamp/common/util/cache/TempFileCache.java +++ b/src/java/com/jogamp/common/util/cache/TempFileCache.java @@ -45,7 +45,7 @@ public class TempFileCache { // Get the value of the tmproot system property // Lifecycle: For all JVMs and ClassLoader - private static final String tmpRootPropName = "jnlp.jogamp.tmp.cache.root"; + /* package */ static final String tmpRootPropName = "jnlp.jogamp.tmp.cache.root"; // Flag indicating that we got a fatal error in the static initializer. private static boolean staticInitError = false; @@ -224,7 +224,7 @@ public class TempFileCache { deleteOldTempDirs(); } }; - reaperThread.setName("AppletLauncher-Reaper"); + reaperThread.setName("TempFileCache-Reaper"); reaperThread.start(); } else { // Make sure that the property is not set to an illegal value diff --git a/src/java/com/jogamp/common/util/cache/TempJarCache.java b/src/java/com/jogamp/common/util/cache/TempJarCache.java index efdf113..9da7d0c 100644 --- a/src/java/com/jogamp/common/util/cache/TempJarCache.java +++ b/src/java/com/jogamp/common/util/cache/TempJarCache.java @@ -35,7 +35,6 @@ import java.util.Map; import java.util.Set; import java.util.jar.JarFile; - import com.jogamp.common.os.NativeLibrary; import com.jogamp.common.util.JarUtil; @@ -55,44 +54,59 @@ public class TempJarCache { private static TempFileCache tmpFileCache; private static boolean staticInitError = false; - - static { - staticInitError = !TempFileCache.initSingleton(); - - if(!staticInitError) { - tmpFileCache = new TempFileCache(); - staticInitError = !tmpFileCache.isValid(); - } - - if(!staticInitError) { - // Initialize the collections of resources - nativeLibMap = new HashMap<String, String>(); - nativeLibJars = new HashSet<JarFile>(); - classFileJars = new HashSet<JarFile>(); - resourceFileJars = new HashSet<JarFile>(); - } - } + private static volatile boolean isInit = false; /** - * Documented way to kick off static initialization + * Documented way to kick off static initialization. + * * @return true is static initialization was successful */ public static boolean initSingleton() { - return isValid(); + if (!isInit) { // volatile: ok + synchronized (TempJarCache.class) { + if (!isInit) { + isInit = true; + staticInitError = !TempFileCache.initSingleton(); + + if(!staticInitError) { + tmpFileCache = new TempFileCache(); + staticInitError = !tmpFileCache.isValid(); + } + + if(!staticInitError) { + // Initialize the collections of resources + nativeLibMap = new HashMap<String, String>(); + nativeLibJars = new HashSet<JarFile>(); + classFileJars = new HashSet<JarFile>(); + resourceFileJars = new HashSet<JarFile>(); + } + } + } + } + return !staticInitError; } /** - * @return true is static initialization was successful + * + * @return true if this class has been initialized, ie. used, otherwise not. */ - public static boolean isValid() { - return !staticInitError; + public static boolean isInitialized() { + return isInit; + } + + /* package */ static void checkInitialized() { + if(!isInit) { + throw new RuntimeException("initSingleton() has to be called first."); + } } public static TempFileCache getTempFileCache() { + checkInitialized(); return tmpFileCache; } public static boolean contains(JarFile jarFile) throws IOException { + checkInitialized(); return nativeLibJars.contains(jarFile); } @@ -104,6 +118,7 @@ public class TempJarCache { * @throws IOException */ public static boolean addNativeLibs(JarFile jarFile) throws IOException { + checkInitialized(); if(!nativeLibJars.contains(jarFile)) { JarUtil.extract(tmpFileCache.getTempDir(), nativeLibMap, jarFile, true, false, false); @@ -123,7 +138,8 @@ public class TempJarCache { * @return * @throws IOException */ - public static boolean addClasses(JarFile jarFile) throws IOException { + public static boolean addClasses(JarFile jarFile) throws IOException { + checkInitialized(); if(!classFileJars.contains(jarFile)) { JarUtil.extract(tmpFileCache.getTempDir(), null, jarFile, false, true, false); @@ -141,6 +157,7 @@ public class TempJarCache { * @throws IOException */ public static boolean addResources(JarFile jarFile) throws IOException { + checkInitialized(); if(!resourceFileJars.contains(jarFile)) { JarUtil.extract(tmpFileCache.getTempDir(), null, jarFile, false, false, true); @@ -161,7 +178,8 @@ public class TempJarCache { * @return * @throws IOException */ - public static boolean addAll(JarFile jarFile) throws IOException { + public static boolean addAll(JarFile jarFile) throws IOException { + checkInitialized(); if(!nativeLibJars.contains(jarFile) || !classFileJars.contains(jarFile) || !resourceFileJars.contains(jarFile)) { @@ -185,6 +203,7 @@ public class TempJarCache { } public static String findLibrary(String libName) { + checkInitialized(); // try with mapped library basename first String path = nativeLibMap.get(libName); if(null == path) { @@ -202,6 +221,7 @@ public class TempJarCache { /** TODO class access pending * needs Classloader.defineClass(..) access, ie. own derivation - will do when needed .. public static Class<?> findClass(String name, ClassLoader cl) throws IOException, ClassFormatError { + checkInitialized(); final File f = new File(nativeTmpFileCache.getTempDir(), IOUtil.getClassFileName(name)); if(f.exists()) { Class.forName(fname, initialize, loader) @@ -214,6 +234,7 @@ public class TempJarCache { } */ public static String findResource(String name) { + checkInitialized(); final File f = new File(tmpFileCache.getTempDir(), name); if(f.exists()) { return f.getAbsolutePath(); diff --git a/src/junit/com/jogamp/common/util/TestTempJarCache.java b/src/junit/com/jogamp/common/util/TestTempJarCache.java index a0e0379..fca839b 100644 --- a/src/junit/com/jogamp/common/util/TestTempJarCache.java +++ b/src/junit/com/jogamp/common/util/TestTempJarCache.java @@ -43,6 +43,7 @@ import org.junit.Test; import com.jogamp.common.GlueGenVersion; import com.jogamp.common.os.NativeLibrary; import com.jogamp.common.os.Platform; +import com.jogamp.common.util.cache.TempCacheReg; import com.jogamp.common.util.cache.TempFileCache; import com.jogamp.common.util.cache.TempJarCache; @@ -108,10 +109,14 @@ public class TestTempJarCache { @BeforeClass public static void init() { + // may already been initialized by other test + // Assert.assertFalse(TempCacheReg.isTempFileCacheUsed()); + Assert.assertTrue(TempFileCache.initSingleton()); + Assert.assertTrue(TempCacheReg.isTempFileCacheUsed()); + fileCache = new TempFileCache(); Assert.assertTrue(fileCache.isValid()); System.err.println("tmp dir: "+fileCache.getTempDir()); - } @Test @@ -149,7 +154,13 @@ public class TestTempJarCache { @Test public void testTempJarCache01LoadAllTestManifestAndClass() throws IOException { - Assert.assertTrue(TempJarCache.isValid()); + // may already been initialized by other test + // Assert.assertFalse(TempCacheReg.isTempJarCacheUsed()); + // Assert.assertFalse(TempJarCache.isInitialized()); + Assert.assertTrue(TempJarCache.initSingleton()); + Assert.assertTrue(TempCacheReg.isTempJarCacheUsed()); + Assert.assertTrue(TempJarCache.isInitialized()); + TempJarCache.addAll(JarUtil.getJarFile(GlueGenVersion.class.getName(), getClass().getClassLoader())); File f0 = new File(TempJarCache.getTempFileCache().getTempDir(), "META-INF/MANIFEST.MF"); @@ -204,6 +215,19 @@ public class TestTempJarCache { ClassLoader cl2 = new TestClassLoader(urls, null); ClassLoader cl3 = new TestClassLoader(urls, null); + Assert.assertFalse(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", null, null, cl2) + ).booleanValue()); + Assert.assertFalse(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", null, null, cl3) + ).booleanValue()); + Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "initSingleton", null, null, cl2) + ).booleanValue()); + Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "initSingleton", null, null, cl3) + ).booleanValue()); + Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", null, null, cl2) + ).booleanValue()); + Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", null, null, cl3) + ).booleanValue()); + Object fileCache2 = ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "getTempFileCache", null, null, cl2); Object fileCache3 = ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "getTempFileCache", null, null, cl3); |