From 8c8cd1a2ea06100416372043ef1e6b84617e8135 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 20 May 2003 16:29:36 +0000 Subject: [PATCH] Allow the temporary cache directory to be explicitly specified. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2591 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/resource/ResourceBundle.java | 56 +++++++++++-------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/java/com/threerings/resource/ResourceBundle.java b/src/java/com/threerings/resource/ResourceBundle.java index fd9e0c8ff..4009b854e 100644 --- a/src/java/com/threerings/resource/ResourceBundle.java +++ b/src/java/com/threerings/resource/ResourceBundle.java @@ -1,5 +1,5 @@ // -// $Id: ResourceBundle.java,v 1.14 2003/05/14 20:46:25 mdb Exp $ +// $Id: ResourceBundle.java,v 1.15 2003/05/20 16:29:36 mdb Exp $ package com.threerings.resource; @@ -111,7 +111,7 @@ public class ResourceBundle // compute the path to our temporary file String tpath = StringUtil.md5hex(_source.getPath() + "%" + path); - File tfile = new File(_tmpdir, tpath); + File tfile = new File(getCacheDir(), tpath); if (tfile.exists() && (tfile.lastModified() > _sourceLastMod)) { // System.out.println("Using cached " + _source.getPath() + // ":" + path); @@ -208,9 +208,39 @@ public class ResourceBundle */ public static File getCacheDir () { + if (_tmpdir == null) { + String tmpdir = System.getProperty("java.io.tmpdir"); + if (tmpdir == null) { + Log.info("No system defined temp directory. Faking it."); + tmpdir = System.getProperty("user.home"); + } + setCacheDir(new File(tmpdir, ".narcache")); + } return _tmpdir; } + /** + * Specifies the directory in which our temporary resource files + * should be stored. + */ + public static void setCacheDir (File tmpdir) + { + String rando = Long.toHexString((long)(Math.random() * Long.MAX_VALUE)); + _tmpdir = new File(tmpdir, rando); + if (!_tmpdir.exists()) { + Log.info("Creating narya temp cache directory '" + _tmpdir + "'."); + _tmpdir.mkdirs(); + } + + // add a hook to blow away the temp directory when we exit + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run () { + Log.info("Clearing narya temp cache '" + _tmpdir + "'."); + FileUtil.recursiveDelete(_tmpdir); + } + }); + } + /** The file from which we construct our jar file. */ protected File _source; @@ -226,26 +256,4 @@ public class ResourceBundle /** A directory in which we temporarily unpack our resource files. */ protected static File _tmpdir; - - static { - String tmpdir = System.getProperty("java.io.tmpdir"); - if (tmpdir == null) { - Log.info("No system defined temp directory. Faking it."); - tmpdir = System.getProperty("user.home"); - } - long rando = (long)(Math.random() * Long.MAX_VALUE); - _tmpdir = new File(tmpdir, ".narcache" + File.separator + rando); - if (!_tmpdir.exists()) { - Log.info("Creating narya temp cache directory '" + _tmpdir + "'."); - _tmpdir.mkdirs(); - } - - // add a hook to blow away the temp directory when we exit - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run () { - Log.info("Clearing narya temp cache '" + _tmpdir + "'."); - FileUtil.recursiveDelete(_tmpdir); - } - }); - } }