From 61d3b2f9b60cbea2e9454b86cc559347943a578e Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 20 Sep 2011 05:49:36 +0200 Subject: Temp Cache: More control over the lifecycle, explicit TempJarCache initialization/usage only --- .../com/jogamp/common/util/cache/TempCacheReg.java | 37 +++++++++++ .../jogamp/common/util/cache/TempFileCache.java | 4 +- .../com/jogamp/common/util/cache/TempJarCache.java | 71 ++++++++++++++-------- 3 files changed, 85 insertions(+), 27 deletions(-) create mode 100644 src/java/com/jogamp/common/util/cache/TempCacheReg.java (limited to 'src/java/com/jogamp/common') 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(); - nativeLibJars = new HashSet(); - classFileJars = new HashSet(); - resourceFileJars = new HashSet(); - } - } + 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(); + nativeLibJars = new HashSet(); + classFileJars = new HashSet(); + resourceFileJars = new HashSet(); + } + } + } + } + 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(); -- cgit v1.2.3