use of org.apache.commons.vfs2.FileNotFoundException in project artisynth_core by artisynth.
the class FileCacher method cache.
public File cache(URIx uri, File cacheFile, FileTransferMonitor monitor) throws FileSystemException {
// For atomic operation, first download to temporary directory
File tmpCacheFile = new File(cacheFile.getAbsolutePath() + TMP_EXTENSION);
URIx cacheURI = new URIx(cacheFile.getAbsoluteFile());
URIx tmpCacheURI = new URIx(tmpCacheFile.getAbsoluteFile());
FileObject localTempFile = manager.resolveFile(tmpCacheURI.toString(true));
FileObject localCacheFile = manager.resolveFile(cacheURI.toString(true));
// will resolve next
FileObject remoteFile = null;
// loop through authenticators until we either succeed or cancel
boolean cancel = false;
while (remoteFile == null && cancel == false) {
remoteFile = resolveRemote(uri);
}
if (remoteFile == null || !remoteFile.exists()) {
throw new FileSystemException("Cannot find remote file <" + uri.toString() + ">", new FileNotFoundException("<" + uri.toString() + ">"));
}
// monitor the file transfer progress
if (monitor != null) {
monitor.monitor(localTempFile, remoteFile, -1, cacheFile.getName());
monitor.start();
monitor.fireStartEvent(localTempFile);
}
// transfer content
try {
if (remoteFile.isFile()) {
localTempFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
} else if (remoteFile.isFolder()) {
// final FileObject fileSystem = manager.createFileSystem(remoteFile);
localTempFile.copyFrom(remoteFile, new AllFileSelector());
// fileSystem.close();
}
if (monitor != null) {
monitor.fireCompleteEvent(localTempFile);
}
} catch (Exception e) {
// try to delete local file
localTempFile.delete();
throw new RuntimeException("Failed to complete transfer of " + remoteFile.getURL() + " to " + localTempFile.getURL(), e);
} finally {
// close files if we need to
localTempFile.close();
remoteFile.close();
if (monitor != null) {
monitor.release(localTempFile);
monitor.stop();
}
}
// now that the copy is complete, do a rename operation
try {
if (tmpCacheFile.isDirectory()) {
SafeFileUtils.moveDirectory(tmpCacheFile, cacheFile);
} else {
SafeFileUtils.moveFile(tmpCacheFile, cacheFile);
}
} catch (Exception e) {
// delete if possible
localCacheFile.delete();
throw new RuntimeException("Failed to atomically move " + "to " + localCacheFile.getURL(), e);
}
return cacheFile;
}
Aggregations