Made sourceIsReady() more robust and it now returns whether or not it

successfully unpacked its jar file so that we can report bundle download
borkage and retry in the face of potentially transient fuckolas.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2764 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-08-09 05:42:13 +00:00
parent 927881306d
commit 36f738949d
@@ -1,5 +1,5 @@
// //
// $Id: ResourceBundle.java,v 1.19 2003/07/14 22:18:18 ray Exp $ // $Id: ResourceBundle.java,v 1.20 2003/08/09 05:42:13 mdb Exp $
package com.threerings.resource; package com.threerings.resource;
@@ -82,25 +82,24 @@ public class ResourceBundle
/** /**
* Called by the resource manager once it has ensured that our * Called by the resource manager once it has ensured that our
* resource jar file is up to date and ready for reading. * resource jar file is up to date and ready for reading.
*
* @return true if we successfully unpacked our resources, false if we
* encountered errors in doing so.
*/ */
public void sourceIsReady () public boolean sourceIsReady ()
{ {
// make a note of our source's last modification time // make a note of our source's last modification time
_sourceLastMod = _source.lastModified(); _sourceLastMod = _source.lastModified();
// if we are unpacking files, the time to do so is now // if we are unpacking files, the time to do so is now
if (_unpacked != null && _unpacked.lastModified() != _sourceLastMod) { if (_unpacked != null && _unpacked.lastModified() != _sourceLastMod) {
boolean resolved = false;
try { try {
resolved = !resolveJarFile(); resolveJarFile();
} catch (IOException ioe) { } catch (IOException ioe) {
Log.warning("Failure resolving jar file '" + _source + Log.warning("Failure resolving jar file '" + _source +
"': " + ioe + "."); "': " + ioe + ".");
} closeJar();
if (!resolved) { return false;
String errmsg = "Source ready but failed to resolve jar " +
"[source=" + _source + "]";
throw new IllegalStateException(errmsg);
} }
Log.info("Unpacking into " + _cache + "..."); Log.info("Unpacking into " + _cache + "...");
@@ -108,14 +107,14 @@ public class ResourceBundle
if (!_cache.mkdir()) { if (!_cache.mkdir()) {
Log.warning("Failed to create bundle cache directory '" + Log.warning("Failed to create bundle cache directory '" +
_cache + "'."); _cache + "'.");
closeJar();
// we are hopelessly fucked // we are hopelessly fucked
return; return false;
} }
} else { } else {
FileUtil.recursiveClean(_cache); FileUtil.recursiveClean(_cache);
} }
boolean failure = false;
Enumeration entries = _jarSource.entries(); Enumeration entries = _jarSource.entries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry)entries.nextElement(); JarEntry entry = (JarEntry)entries.nextElement();
@@ -128,7 +127,6 @@ public class ResourceBundle
if (!efile.exists() && !efile.mkdir()) { if (!efile.exists() && !efile.mkdir()) {
Log.warning("Failed to create bundle entry path '" + Log.warning("Failed to create bundle entry path '" +
efile + "'."); efile + "'.");
failure = true;
} }
continue; continue;
} }
@@ -139,10 +137,10 @@ public class ResourceBundle
if (!parent.exists() && !parent.mkdirs()) { if (!parent.exists() && !parent.mkdirs()) {
Log.warning("Failed to create bundle entry parent '" + Log.warning("Failed to create bundle entry parent '" +
parent + "'."); parent + "'.");
failure = true;
continue; continue;
} }
boolean failure = false;
BufferedOutputStream fout = null; BufferedOutputStream fout = null;
InputStream jin = null; InputStream jin = null;
try { try {
@@ -157,22 +155,77 @@ public class ResourceBundle
StreamUtil.close(jin); StreamUtil.close(jin);
StreamUtil.close(fout); StreamUtil.close(fout);
} }
}
// if everything unpacked smoothly, create our unpack stamp // if something went awry, delete everything in the hopes
if (!failure) { // that next time things will work
try { if (failure) {
_unpacked.createNewFile(); wipeBundle();
if (!_unpacked.setLastModified(_sourceLastMod)) { return false;
Log.warning("Failed to set last mod on stamp file '" +
_unpacked + "'.");
}
} catch (IOException ioe) {
Log.warning("Failure creating stamp file '" + _unpacked +
"': " + ioe + ".");
} }
} }
// close the jar file now that it's all unpacked
closeJar();
// if everything unpacked smoothly, create our unpack stamp
try {
_unpacked.createNewFile();
if (!_unpacked.setLastModified(_sourceLastMod)) {
Log.warning("Failed to set last mod on stamp file '" +
_unpacked + "'.");
}
} catch (IOException ioe) {
Log.warning("Failure creating stamp file '" + _unpacked +
"': " + ioe + ".");
// no need to stick a fork in things at this point
}
} }
return true;
}
/**
* Closes our (possibly opened) jar file.
*/
protected void closeJar ()
{
try {
if (_jarSource != null) {
_jarSource.close();
}
} catch (Exception ioe) {
Log.warning("Failed to close jar file [path=" + _source +
", error=" + ioe + "].");
}
}
/**
* Clears out everything associated with this resource bundle in the
* hopes that we can download it afresh and everything will work the
* next time around.
*/
protected void wipeBundle ()
{
// clear out our cache directory
if (_cache != null) {
FileUtil.recursiveClean(_cache);
}
// delete our unpack stamp file
if (_unpacked != null) {
_unpacked.delete();
}
// close and delete our source jar file
if (_source != null) {
closeJar();
_source.delete();
}
// also clear out any .vers file that the downloader might be
// maintaining if this is a versioned resource bundle
File vfile = new File(FileUtil.resuffix(_source, ".jar", ".vers"));
vfile.delete();
} }
/** /**